当前位置:网站首页>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运维工程师高级;
智能制造运维工程师培训课程,远程办公的运维工程师,迈瑞医疗运维工程师工资待遇,后台运维工程师是做什么的;
风力运维工程师怎样,浪潮云运维工程师,医疗设备运维工程师证书样本,运维工程师男朋友,运维工程师暴躁。
边栏推荐
- The adaptation of go language under windows10:vscode
- OpenCV基于DLCO描述子匹配
- [machine learning] evaluation index and code implementation of multi label classification
- NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
- QT implementation traverses folders
- Travail du quatrième jour
- 一个技巧;教你轻松下载抖音直播视频,抖音直播视频下载新方案!
- 微机原理与技术接口 实验五 基本IO操作温度控制实验
- 3.Golang字符串string类型
- 文件一键备份
猜你喜欢

Mysql学习笔记-分页-表的创建-数据类型

新一代云数据库的引领者---AWS

字符串相关函数(二)

C language drawing example - flower pattern

Experiment the next day

Talk about the redis cache penetration scenario and the corresponding solutions

es安装ik分词器

HCIP(6)

Enabling cities to "plan, build, operate, manage and serve" -- MAPGIS CIM platform explores "cim+" multi scenario applications

OpenCV基于DLCO描述子匹配
随机推荐
Talk about the redis cache penetration scenario and the corresponding solutions
2022-07-07:Spire.Office 7.7.2 for net 闪亮登场
Chinese garbled code problem in reading binary files
Es install IK word breaker
【C# wpf】个人网盘练习项目总结
Genesis与BlueRun Ventures展开深度交流
01 knapsack interview questions series (I)
RMAN abnormal machine recovery error rman-06026 rman-06023
Hcip second day notes
Overview of the application of air, space and sea Association
C语言绘画示例-进度条
核芯基站_启动CPA文件报错‘No gateways configured’
Deep learning parameter initialization (II) Kaiming initialization with code
String correlation function (II)
Leetcode 239. Sliding window maximum
2022年低压电工考试题及在线模拟考试
Experiment the next day
Nintendo patent shows that the follow-up products of fitness ring accessories may be under development
C#从入门到精通之第一篇: C#概述与入门
WAV和PCM的关系和区别