当前位置:网站首页>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
- bounding box iou
- [graph theory brush question-4] force deduction 778 Swimming in a rising pool
- Elastic box model
- Leetcode 542, 01 matrix
- LeetCode 709、转换成小写字母
- Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
- Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
- Solve the Chinese garbled code of URL in JS - decoding
- Unity solves Z-fighting
猜你喜欢
Automatically fill in body temperature and win10 task plan
Commit and ROLLBACK in DCL of 16mysql
2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real
The ODB model calculates the data and outputs it to excel
Customize timeline component styles
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行
Modeling based on catiav6
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
Summary and effect analysis of methods for calculating binocular parallax
随机推荐
Recommend an open source free drawing software draw IO exportable vector graph
LeetCode 74、搜索二维矩阵
JSX syntax rules
LeetCode-279-完全平方数
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
【PTA】整除光棍
Historical track data reading of Holux m1200-e Bluetooth GPS track recorder
CONDA environment management command
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
PostgreSQL basic functions
Matlab matrix index problem
Latest investigation and progress of building intelligence based on sati
SQL: query duplicate data and delete duplicate data
On IRP from the perspective of source code
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
"Meta function" of tidb 6.0: what is placement rules in SQL?
Scrapy教程 - (2)寫一個簡單爬蟲
Common form verification
LeetCode 1351、统计有序矩阵中的负数