Skip to content

Latest commit

 

History

History
119 lines (84 loc) · 2.16 KB

sql-statement-drop-table.md

File metadata and controls

119 lines (84 loc) · 2.16 KB
title summary aliases
DROP TABLE
TiDB 数据库中 DROP TABLE 的使用概况。
/docs-cn/dev/sql-statements/sql-statement-drop-table/
/docs-cn/dev/reference/sql/statements/drop-table/

DROP TABLE

DROP TABLE 语句用于从当前所选的数据库中删除表。如果表不存在则会报错,除非使用 IF EXISTS 修饰符。

语法图

DropTableStmt ::=
    'DROP' OptTemporary TableOrTables IfExists TableNameList RestrictOrCascadeOpt

OptTemporary ::=
    ( 'TEMPORARY' | ('GLOBAL' 'TEMPORARY') )?

TableOrTables ::=
    'TABLE'
|   'TABLES'

TableNameList ::=
    TableName ( ',' TableName )*

删除临时表

删除普通表和临时表的用法如下:

  • DROP TEMPORARY TABLE 只能删除本地临时表
  • DROP GLOBAL TEMPORARY TABLE 只能删除全局临时表
  • DROP TABLE 可以删除普通表或临时表

示例

{{< copyable "sql" >}}

CREATE TABLE t1 (a INT);
Query OK, 0 rows affected (0.11 sec)

{{< copyable "sql" >}}

DROP TABLE t1;
Query OK, 0 rows affected (0.22 sec)

{{< copyable "sql" >}}

DROP TABLE table_not_exists;
ERROR 1051 (42S02): Unknown table 'test.table_not_exists'

{{< copyable "sql" >}}

DROP TABLE IF EXISTS table_not_exists;
Query OK, 0 rows affected, 1 warning (0.01 sec)
SHOW WARNINGS;
+-------+------+---------------------------------------+
| Level | Code | Message                               |
+-------+------+---------------------------------------+
| Note  | 1051 | Unknown table 'test.table_not_exists' |
+-------+------+---------------------------------------+
1 row in set (0.01 sec)

{{< copyable "sql" >}}

CREATE VIEW v1 AS SELECT 1;
Query OK, 0 rows affected (0.10 sec)

{{< copyable "sql" >}}

DROP TABLE v1;
Query OK, 0 rows affected (0.23 sec)

MySQL 兼容性

  • 目前 RESTRICTCASCADE 仅在语法上支持。

另请参阅