MySql 删除数据表

定义:

删除数据表就是删除数据库中已经存在的表。请注意,当表被删除时,表的定义和表中的所有数据都将被删除。因此,在执行删除操作之前,最好对表中的数据进行备份,以避免无法挽回的后果。本节详细说明如何删除数据库表。

[En]

To delete a data table is to delete a table that already exists in the database. Note that when the table is deleted, the definition of the table and all data in the table are deleted. Therefore, before the delete operation, it is best to make a backup of the data in the table to avoid irreparable consequences. This section explains in detail how to delete database tables.

1 删除一个或多个没有被其他表关联的数据表

如果一个数据表没有和其它表存在关联关系,即删除它自己对其它表没有影响的话,可以借助 DROP TABLE 。语法格式:

DROP TABLE [IF EXSITS] 表1 ,表2,...,表n ;

解释:表n”指要删除的表的名称,后面可以同时删除多个表,只需将要删除的表名依次写在后面,相互之间用逗号隔开即可。如果要删除的数据表不存在,则MySQL会提示一条错误信息,”ERROR 1051 (42S02): Unknown table ‘表名'”。参数”IF EXISTS”用于在删除前判断删除的表是否存在,加上该参数后,再删除表的时候,如果表不存在,SQL语句可以顺利执行,但是会发出警告(warning)

案例1:

1.1 新建一张表

CREATE TABLE tb_school (

id INT(10) PRIMARY KEY,
name VARCHAR(11)
)

1.2 执行删除命令

DROP TABLE IF EXISTS tb_school;

1.3 结果检查

删除前:

删除后:

从执行结果可以看到,数据表列表中已经不存在名称为tb_school的表,删除操作成功。

2 删除被其他表关联的主表

在数据表之间存在外键关联的情况下,如果直接删除父表,结果将失败,因为直接删除会破坏表的引用完整性。如果必须删除,可以先删除与其关联的子表,然后再删除父表,但同时删除两个表中的数据。在某些情况下,您可能希望保留子表,而要单独删除父表,只需取消关联表的外键约束,然后即可删除父表。

[En]

In the case where there is a foreign key association between the data tables, if you delete the parent table directly, the result will fail because the direct deletion will destroy the referential integrity of the table. If you have to delete, you can first delete the child table associated with it, and then delete the parent table, but you delete the data in both tables at the same time. In some cases, you may want to keep the child table, and to delete the parent table separately, simply cancel the foreign key constraint of the associated table, and then you can delete the parent table.

案例2

2.1 创建两张具有关联关系的表

CREATE TABLE tb_school ( id INT ( 11 ) PRIMARY KEY, name VARCHAR ( 22 ) );

CREATE TABLE tb_classroom (
id  INT(11) PRIMARY KEY,
schoolId INT(11),

CONSTRAINT fk_emp_school FOREIGN KEY(schoolId) REFERENCES tb_school(id)
)

MySql 删除数据表

可以看到,以上执行结果创建了两个关联表tb_school和表tb_classroom。其中,tb_classroom表为子表,具有名称为fk_emp_school的外键约束;tb_school为父表,其主键id被子表tb_classroom所关联。

2.2 执行删除DROP TABLE命令

执行结果显示,存在外键关联,不能删除。在这种情况下,有两种处理方法:

[En]

The execution results show that it cannot be deleted because there is a foreign key association. In this case, there are two ways to handle it:

一: 先删除子表 tb_classroom ,再删除 父表tb_school,可参考 1 方法,这里不再赘述

第二步:取消表前的外键关系,然后进行删除。

[En]

Second: cancel the foreign key relationship before the table, and then perform the deletion.

2.3 取消外键关系,再删除。

ALTER TABLE tb_classroom DROP FOREIGN KEY fk_emp_school

MySql 删除数据表

因此,在执行成功后,执行删除命令以显示删除成功。

[En]

As a result, after the execution is successful, the delete command is executed to show that the deletion is successful.

MySql 删除数据表

文章连接:https://www.cnblogs.com/xiong97/p/16564428.html

Original: https://www.cnblogs.com/xiong97/p/16564428.html
Author: 静言善思
Title: MySql 删除数据表

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505075/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球