当前位置:网站首页>mysql常用命令
mysql常用命令
2022-07-31 05:15:00 【为今天而努力】
修改表名称
mysql> ALTER TABLE test_foreing_key RENAME test_foreign_key;
修改字段排列位置
ALTER TABLE tableName【表名】 MODIFY column1【字段名 1】 varchar(80)【数据类型】 FIRST|AFTER bookName【字段名2】;
- FIRST; 将【字段名1】修改为表的第一个字段
AFTER bookName【字段名2】; 将【字段名1】更新到【字段名2】的后面
ALTER TABLE book MODIFY owner varchar(80) AFTER bookName;
删除表中的外键约束
ALTER TABLE 【表名】DOPR FOREIGN KEY 【外键约束】
// 准备测试表
mysql> CREATE TABLE test1 (id INT(11) PRIMARY KEY, name VARCHAR(10));
mysql> CREATE TABLE test_foreing_key(
-> id INT(11) PRIMARY KEY,
-> foreKeyTestId INT(11),
-> CONSTRAINT fk_test_id FOREIGN KEY (foreKeyTestId) REFERENCES test1(id));REFERENCES test1(id));
// 删除外键约束
mysql> ALTER TABLE test_foreign_key DROP FOREIGN KEY fk_test_id;
删除没有约束表
删除表时如果没有表不存在,MySQL 会报错误信息 “ERROR 1051(42S02):Unknown table”, 参数“IF EXISTS ”
用于再删出前判断删除的表是否存在,加上参数后,在删除表时,如果不存在时可以顺利执行,不过会报警告【warning】
DROP TABLE 【IF EXISTS】table1, table2, table3, ..., tableN;
删除被其他表关联的主表
数据表之间存在外键关联的情况下,如果直接删除父表,会失败,原因是直接删除父表破坏了表的参照完整性。
如果必须要删除,可以先删除与它关联的子表,再删除父表,只是这样同时删除了两个表中的数据。但有的情况下
可能要保留子表,这时要单独删除父表,只需要将关联的表的外键约束条件取消,然后就可以删除父表。
// 查看所有表
mysql> SHOW TABLES;
+------------------+
| Tables_in_reader |
+------------------+
| test1 |
| test2 |
| test_foreign_key |
+------------------+
// 直接删除父表提示错误
mysql> DROP TABLE test1;
ERROR 3730 (HY000): Cannot drop table 'test1' referenced by a foreign key constraint 'fk_test_id' on table 'test_foreing_key'.
// 删除外键约束
mysql> ALTER TABLE test_foreign_key DROP FOREIGN KEY fk_test_id;
// 继续删除
mysql> DROP TABLE test1;
Query OK, 0 rows affected (0.16 sec)
// 查看所有表
mysql> SHOW TABLES;
+------------------+
| Tables_in_reader |
+------------------+
| test2 |
| test_foreign_key |
+------------------+
边栏推荐
- 【JVM加载】---类加载机制
- sql 外键约束【表关系绑定】
- 局部变量成员变量、引用类型、this,static(第五天)
- 自定dialog 布局没有居中解决方案
- 为什么bash中的read要配合while才能读取/dev/stdin的内容
- Why is the redis single-threaded also so fast?
- mysql启动报错The server quit without updating PID file几种解决办法
- 【swagger关闭】生产环境关闭swagger方法
- Volatility取证工具使用日记
- Yuan prospect and four track of the universe
猜你喜欢
mysql password modification method in Linux (pro-test available)
Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘
Getting to know regular expressions
阿里云中mysql数据库被攻击了,最终数据找回来了
Linux modify MySQL database password
为什么redis是单线程还那么快?
Gradle sync failed: Uninitialized object exists on backward branch 142
Yuan prospect and four track of the universe
著名网站msdn.itellyou.cn原理分析
The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
随机推荐
碎片化NFT(Fractional NFT)
[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version
2021 Mianjing - Embrace Change
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
C language tutorial (1) - preparation
Volatility取证工具使用日记
永恒之蓝漏洞复现
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
【云原生】SQL(及存储过程)跑得太慢怎么办?
C语言 | 获取字符串里逗号间隔的内容
2021年京东数据分析工程师秋招笔试编程题
SSH自动重连脚本
Common JVM interview questions and answers
[swagger close] The production environment closes the swagger method
【uiautomation】微信好友列表获取(存储到txt中)
初识正则表达式
How MySQL - depots table?A look at will understand
MySQL压缩包方式安装,傻瓜式教学
数据库 | SQL增删改查基础语法
对于输出点是时间戳的渗透测试方法(以Oracle数据库为例)