当前位置:网站首页>mysql common commands
mysql common commands
2022-07-31 05:58:00 【Hard for today】
修改表名称
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;
Drop a table without constraints
When dropping a table if no table does not exist,MySQL 会报错误信息 “ERROR 1051(42S02):Unknown table”, 参数“IF EXISTS ”
It is used to determine whether the deleted table exists before deleting it again,加上参数后,在删除表时,If it does not exist, it can be executed smoothly,But there will be a warning【warning】
DROP TABLE 【IF EXISTS】table1, table2, table3, ..., tableN;
删除被其他表关联的主表
数据表之间存在外键关联的情况下,如果直接删除父表,会失败,The reason is that dropping the parent table directly breaks the referential integrity of the table.
如果必须要删除,可以先删除与它关联的子表,再删除父表,只是这样同时删除了两个表中的数据.但有的情况下
Subtables may be preserved,In this case, delete the parent table separately,It is only necessary to cancel the foreign key constraint of the associated table,然后就可以删除父表.
// 查看所有表
mysql> SHOW TABLES;
+------------------+
| Tables_in_reader |
+------------------+
| test1 |
| test2 |
| test_foreign_key |
+------------------+
// Directly delete the parent table prompts an error
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 |
+------------------+
边栏推荐
- kotlin 插件更新到1.3.21
- File operations in C language (1)
- win11中利用IIS10搭建asp网站
- 2021年京东数据分析工程师秋招笔试编程题
- 带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
- [Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]
- Judgment of database in SQL injection
- [Cloud native] Open source data analysis SPL easily copes with T+0
- js中流程控制语句
- 利用phpstudy搭建DVWA
猜你喜欢
mac10.14中安装mysqldb
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
什么是EVM兼容链?
Using IIS10 to build an asp website in win11
What is GameFi?
【JVM加载】---类加载机制
腾讯云GPU桌面服务器驱动安装
Linux修改MySQL数据库密码
[Cloud native] Simple introduction and use of microservice Nacos
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
随机推荐
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
Redis:安装使用
NFT与数字藏品到底有何区别?
通信原理——纠错编码 | 汉明码(海明码)手算详解
2021年京东数据分析工程师秋招笔试编程题
Regular Expression Basics
了解SSRF,这一篇就足够了
How to distinguish big and small endian in C language
MySQL compressed package installation, fool teaching
[Elastic-Job source code analysis] - job listener
cocos2d-x 实现跨平台的目录遍历
Hyper-V新建虚拟机注意事项
MySQL分页查询的5种方法
Yuan prospect and four track of the universe
【云原生】SQL(及存储过程)跑得太慢怎么办?
Build DVWA with phpstudy
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
5 methods of MySQL paging query
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解