当前位置:网站首页>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
边栏推荐
- Plato Farm元宇宙IEO上线四大,链上交易颇高
- 打新债中签以后怎么办,网上开户安全吗
- Vulnhub DC: 1 penetration notes
- 高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
- Leetcode 1346. Check whether integers and their multiples exist
- 【栈和队列专题】—— 滑动窗口
- How many hacking methods do you know?
- GO語言開發天天生鮮項目第三天 案例-新聞發布系統二
- Commit and rollback in DCL of 16 MySQL
- Customize timeline component styles
猜你喜欢

Commit and rollback in DCL of 16 MySQL

Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem

Unity Odin ProgressBar add value column

go slice

Latex formula

BMP JPEG 图片转换为矢量图像 ContourTrace

Identification of bolt points in aerial photography based on perception

How can matlab obtain the truncated image in trainingimagelabeler

On IRP from the perspective of source code
![[stack and queue topics] - sliding window](/img/65/a2ce87f1401d7a28d4cce0cc85175f.png)
[stack and queue topics] - sliding window
随机推荐
Latest investigation and progress of building intelligence based on sati
Async function ------ ES6
Leetcode 20. Valid parentheses
Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
BMP JPEG 图片转换为矢量图像 ContourTrace
star
16MySQL之DCL 中 COMMIT和ROllBACK
LeetCode 20、有效的括号
Matlab matrix index problem
LeetCode 116. 填充每个节点的下一个右侧节点指针
bounding box iou
go array
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
go slice
Go zero framework database avoidance Guide
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
黑客的入侵方式你知道几种?
Leetcode 1346. Check whether integers and their multiples exist
[PTA] l2-011 play with binary tree
LeetCode 74、搜索二维矩阵