当前位置:网站首页>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
边栏推荐
- An error occurs when the addressable assets system project is packaged. Runtimedata is null
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
- JSX syntax rules
- go map
- Come in and teach you how to solve the problem of port occupation
- [stack and queue topics] - sliding window
- ABAQUS script email auto notification
- Leetcode 232, queue with stack
- 【PTA】L1-002 打印沙漏
- Modeling based on catiav6
猜你喜欢
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
JS arrow function user and processing method of converting arrow function into ordinary function
Modeling based on catiav6
[latex] 5 how to quickly write out the latex formula corresponding to the formula
Some basic configurations in interlij idea
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
GO語言開發天天生鮮項目第三天 案例-新聞發布系統二
Rt-1052 learning notes - GPIO architecture analysis
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
Unity solves Z-fighting
随机推荐
Latex formula
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
ABAQUS script email auto notification
【PTA】整除光棍
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
Leetcode 1346. Check whether integers and their multiples exist
[PTA] l1-002 printing hourglass
Leetcode 1337. Row K with the weakest combat effectiveness in the matrix
RT-1052学习笔记 - GPIO架构分析
Thirty What are VM and VC?
Async function ------ ES6
Experience of mathematical modeling in 18 year research competition
BMP JPEG picture to vector image contourtrace
Implementation of mypromise
LeetCode 116. Populate the next right node pointer for each node
LeetCode 1337、矩阵中战斗力最弱的 K 行
DOS command of Intranet penetration
Commit and rollback in DCL of 16 MySQL
LeetCode 74、搜索二维矩阵
Historical track data reading of Holux m1200-e Bluetooth GPS track recorder