当前位置:网站首页>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 |
+------------------+
边栏推荐
- 在kali上搭建vulhub漏洞靶场
- 碎片化NFT(Fractional NFT)
- flutter arr 依赖
- 场效应管 | N-mos内部结构详解
- [Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
- Linux中mysql密码修改方法(亲测可用)
- npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
- Access数据库的查询
- NFTs: The Heart of Digital Ownership
- 什么是 GameFi?
猜你喜欢

MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
![[Elastic-Job source code analysis] - job listener](/img/99/5e047b1aa83aad7d7f17b4eec606e6.png)
[Elastic-Job source code analysis] - job listener
初识正则表达式

Android软件安全与逆向分析阅读笔记

GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?

阿里一面,说说你知道消息中间件的应用场景有哪些?

js中的对象与函数的理解

Artifact SSMwar exploded Error deploying artifact.See server log for details

工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息

MySql创建数据表
随机推荐
通信原理——纠错编码 | 汉明码(海明码)手算详解
什么是EVM兼容链?
Digital twins will be an important way to enter the "metaverse"
"limit" query in Oracle database
Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘
The latest MySql installation teaching, very detailed
cocos2d-x-3.2 Physics
Redis:安装使用
NFT与数字藏品到底有何区别?
mysql启动报错The server quit without updating PID file几种解决办法
MySQL高级学习笔记
win11中利用IIS10搭建asp网站
Linux修改MySQL数据库密码
C language tutorial (3) - if and loop
SQL注入中数据库的判断
powershell统计文件夹大小
数据库 | SQL查询进阶语法
cocoscreator3.5.2打包微信小游戏发布到QQ小游戏修改
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
Why is the redis single-threaded also so fast?