当前位置:网站首页>mysql如何删除数据表,被关联的数据表如何删除呢
mysql如何删除数据表,被关联的数据表如何删除呢
2022-07-17 16:20:00 【运维家】
删除数据表的时候,表的定义和表中所有的数据均会被删除。因此,在进行删除操作前,最好对表中的数据做一个备份,以免造成无法挽回的后果。
mysql删除数据表分为两种情况;
mysql删除没有关联的表;mysql删除被关联的表;
下面我们就分别来看一下这两种情况;
一、mysql删除没有被关联的表
语法:
drop table [if exists] 表1, 表2,...,表n;
可以同时删除多个表,只需要将删除的表名依次写到后面就行,相互之间用逗号隔开即可。如果删除的表不存在会报错。
if exists用于在删除前判断被删除的表是否存在,加上该参数后,再删除表的时候,如果表不存在,sql不会报错,可以顺利执行下去,但是会发去警告(warning)。
举个栗子:
删除一个名为test_user_3的数据表;
执行sql语句;
mysql> drop table if exists test_user_3;
Query OK, 0 rows affected (0.02 sec)
mysql>
然后我们再次查看的时候,就会发现,这个表已经不见了;
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test_dept |
| test_user |
| test_user_4 |
| test_user_5 |
| test_user_6 |
| test_user_7 |
| test_user_8 |
| test_user_9 |
| test_user_two |
+-------------------+
9 rows in set (0.00 sec)
mysql>
二、删除被其他表关联的主表
在两个数据表相互关联的情况下,如果直接删除主表,会显示失败,原因就是直接删除主表会破坏表的参照完整性。
如果的确有删除主表的需求,可以先删除与它关联的子表,然后再删除父表,不过这样的话就同时删除了两个表中的数据。
因为我们有时候是要保留子表,仅仅是想要删除主表的数据的,那么我们应该如何处理呢?首先我们取消外键约束条件,然后就可以删除主表了。
举个栗子:
创建一个临时数据表,名字为ceshi_1,让他当做这个关联关系中的父表,sql语句如下:
mysql> create table ceshi_1 (id INT primary key, name varchar(50), area varchar(50));
Query OK, 0 rows affected (0.04 sec)
mysql>
然后我们再创建一个临时数据表,名字为ceshi_2,让他当做这个关联关系中的子表,sql语句如下;
mysql> create table ceshi_2 (id int primary key, name varchar(50), deptid int, constraint yunweijia_ceshi foreign key(deptid) references ceshi_1(id));
Query OK, 0 rows affected (0.05 sec)
mysql>
查看下我们新建的临时表ceshi_2的外键约束,结果:
mysql> show create table ceshi_2\G;
*************************** 1. row ***************************
Table: ceshi_2
Create Table: CREATE TABLE `ceshi_2` (
`id` int NOT NULL,
`name` varchar(50) DEFAULT NULL,
`deptid` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `yunweijia_ceshi` (`deptid`),
CONSTRAINT `yunweijia_ceshi` FOREIGN KEY (`deptid`) REFERENCES `ceshi_1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
然后我们尝试删除这个父表ceshi_1;
mysql> drop table ceshi_1;
ERROR 3730 (HY000): Cannot drop table 'ceshi_1' referenced by a foreign key constraint 'yunweijia_ceshi' on table 'ceshi_2'.
mysql>
可以看到他报错了,报错说的是ceshi_1中有一个外键名为yunweijia_ceshi,在ceshi_2中,所以删除失败了。
那么我们接下来接触下关联关系;
mysql> alter table ceshi_2 drop foreign key yunweijia_ceshi;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
这个时候我们再次尝试删除下ceshi_1这个父表;
mysql> drop table ceshi_1;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| ceshi_2 |
| test_dept |
| test_user |
| test_user_4 |
| test_user_5 |
| test_user_6 |
| test_user_7 |
| test_user_8 |
| test_user_9 |
| test_user_two |
+-------------------+
10 rows in set (0.00 sec)
mysql>
可以看到我们顺利的删除了名为ceshi_1的父表,查看整个数据库中也没有了这个数据表。
至此,本文结束。
更多内容请转至VX公众号 “运维家” ,获取最新文章。
------ “运维家” ------
------ “运维家” ------
------ “运维家” ------
系统运维工程师面试,运维工程师优秀员工提名词,tr运维工程师,特来电运维工程师工作日常,IT运维工程师高级;
智能制造运维工程师培训课程,远程办公的运维工程师,迈瑞医疗运维工程师工资待遇,后台运维工程师是做什么的;
风力运维工程师怎样,浪潮云运维工程师,医疗设备运维工程师证书样本,运维工程师男朋友,运维工程师暴躁。
边栏推荐
- Machine learning Assignment 1
- Conversion between Swift binary data and hexadecimal string
- String correlation function (II)
- One click backup of files
- How to delay loading JS
- 第五天笔记
- [machine learning] evaluation index and code implementation of multi label classification
- 机器学习作业1
- 【C语言编程7】BTB模型
- Example of C language painting - progress bar
猜你喜欢

Genesis and bluerun ventures have in-depth exchanges

Focus on the new track of green development - release of MAPGIS intelligent environmental protection solution

How to apply applet container technology to develop hybrid app

Es install IK word breaker

Example of C language drawing - 20 examples of chromatic diagram

Mysql-1366 - Incorrect string value: ‘\xE5\xBC\xA0\xE4\xB8\x89‘ for column ‘userName‘ at row 1

机器学习作业1

核芯基站_启动CPA文件报错‘No gateways configured’

编辑技巧篇

MATLAB(4)函数及文件
随机推荐
电路故障的分析简略
HCIP(6)
STL string input / output overload 1
阿趣的思考
Editing skills
Solution: code error: error reported by error could not resolve
Conversion between Swift binary data and hexadecimal string
Leetcode 150. 逆波兰表达式求值
HCIP(8)
Research on downlink spectrum efficiency of 6G space earth integrated network high altitude platform base station
Leetcode 20. 有效的括号
第四天作業
WAV和PCM的关系和区别
ros(26):ros::Time::now(),ros::Duration,toSec(),toNSec(); Calculate program execution time
米哈游2023秋季招聘正式开始~提前批有机会免笔试!
Opencv tutorial 03: how to track an object in a video
解决:Function rgb is missing argument $green. 的问题
说说 Redis 缓存穿透场景与相应的解决方法
NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
HCIP(4)