当前位置:网站首页>MySQL进阶之表的增删改查
MySQL进阶之表的增删改查
2022-04-23 20:39:00 【小简(JanYork)】
修改表名
ALTER TABLE student RENAME TO stu;
TO
可以省略。
ALTER TABLE 旧表名 RENAME 新表名;
此语句可以修改表的名称,其实一般我们在终端中使用,有可视化工具完全没必要用命令。
修改字段
ALTER TABLE stu CHANGE email mail VARCHAR(50) NOT NULL ;
-- ALTER TABLE 表名 CHANGE 字段名 新字段名 数据类型 [属性(可不写)] ;
这样,我就将stu
表里面的email
改成了mail
,数据类型为VARCHAR(50)
。
说一下,我这里
DataGrip
运行后会及时更新,如果你使用其他工具,有可能要手动刷新才能有反应!!!
增加字段
ALTER TABLE stu ADD demo VARCHAR(10) NOT NULL ;
-- ALTER TABLE 表名 ADD 要加的字段名 数据类型 [属性(可不写)] ;
删除字段
ALTER TABLE stu DROP demo;
-- ALTER TABLE 表名 DROP 字段名;
添加主键约束
-- ALTER TABLE 表名 ADD CONSTRAINT 自定义主键名 PRIMARY KEY 表名(字段);
ALTER TABLE stu ADD CONSTRAINT PK_STU PRIMARY KEY stu(studentNo);
主键名称,它可以随便取名,但是最好按规范来,
pk+下划线+表名,如:
PK_STU`。
添加外键约束
-- ALTER TABLE 外键表名 ADD CONSTRAINT 外键名 FOREIGN KEY (外键字段名) REFERENCES 主键表名(主键字段名);
ALTER TABLE result ADD CONSTRAINT FK_RESULT_STU FOREIGN KEY (studentNo) REFERENCES stu(studentNo);
看好我这个表,主键在
stu
表里面,外键表是result
,不要搞反了。外键名规范是FK_表1_表2
。
小知识
我们发现,我们上面的语句常用ALTER TABLE
这个关键字。
我们可以常识查看帮助文档。
如果英语不咋地,就老老实实搜浏览器吧。
版权声明
本文为[小简(JanYork)]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_60750453/article/details/124326477
边栏推荐
猜你喜欢
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
[PTA] l1-002 printing hourglass
LeetCode 116. 填充每个节点的下一个右侧节点指针
JS arrow function user and processing method of converting arrow function into ordinary function
Unity animation creates sequence frame code and generates animationclip
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
[stack and queue topics] - sliding window
go slice
Recommend an open source free drawing software draw IO exportable vector graph
Plato Farm元宇宙IEO上线四大,链上交易颇高
随机推荐
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行
LeetCode 1346、检查整数及其两倍数是否存在
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
Unity animation creates sequence frame code and generates animationclip
Resolve the error - error identifier 'attr_ id‘ is not in camel case camelcase
Use of node template engine
I JS deep copy and shallow copy
Resolve the eslint warning -- ignore the warning that there is no space between the method name and ()
Recommend an open source free drawing software draw IO exportable vector graph
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
[PTA] l1-002 printing hourglass
Go限制深度遍历目录下文件
【PTA】L2-011 玩转二叉树
16MySQL之DCL 中 COMMIT和ROllBACK
Leetcode 1337. Row K with the weakest combat effectiveness in the matrix
bounding box iou
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
Scripy tutorial - (2) write a simple crawler
Cmake project under vs2019: calculating binocular parallax using elas method