当前位置:网站首页>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
边栏推荐
- Summary and effect analysis of methods for calculating binocular parallax
- Monte Carlo py solves the area problem! (save pupils Series)
- Historical track data reading of Holux m1200-e Bluetooth GPS track recorder
- Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
- go-zero框架数据库方面避坑指南
- Commit and rollback in DCL of 16 MySQL
- Leetcode 994, rotten orange
- Development of Matlab GUI bridge auxiliary Designer (functional introduction)
- 【PTA】L2-011 玩转二叉树
- UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
猜你喜欢

Elastic box model

go-zero框架数据库方面避坑指南
![[latex] 5 how to quickly write out the latex formula corresponding to the formula](/img/1f/3c5a332ce1d6852dde38040faea5bf.png)
[latex] 5 how to quickly write out the latex formula corresponding to the formula

Cmake project under vs2019: calculating binocular parallax using elas method

Tensorflow 2 basic operation dictionary
![[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph](/img/fb/9822cccde4ca39d8066024c09a7349.png)
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph

Commit and rollback in DCL of 16 MySQL

GO語言開發天天生鮮項目第三天 案例-新聞發布系統二

高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?

Scrapy教程 - (2)寫一個簡單爬蟲
随机推荐
LeetCode 994、腐烂的橘子
go map
ABAQUS script email auto notification
Common form verification
Analysis of the relationship between generalized Bim and CAD under the current background
Resolve the eslint warning -- ignore the warning that there is no space between the method name and ()
內網滲透之DOS命令
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
Leetcode 20. Valid parentheses
Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
Solve the Chinese garbled code of URL in JS - decoding
Solution to PowerDesigner's failure to connect to MySQL in x64 system
Go language development Daily Fresh Project Day 3 Case - Press Release System II
Bash script learning -- for loop traversal
[latex] 5 how to quickly write out the latex formula corresponding to the formula
黑客的入侵方式你知道几种?
Experience of mathematical modeling in 18 year research competition
LeetCode 20、有效的括号