DROP TABLE

描述

DROP TABLE 会删除表,并且如果该表不是 EXTERNAL 表,还会从文件系统中移除与该表关联的目录。如果表不存在,则会抛出异常。

如果是外部表,则只从元数据存储数据库中移除相关的元数据信息。

如果表已缓存,此命令会取消缓存该表及其所有依赖项。

语法

DROP TABLE [ IF EXISTS ] table_identifier [ PURGE ]

参数

示例

-- Assumes a table named `employeetable` exists.
DROP TABLE employeetable;

-- Assumes a table named `employeetable` exists in the `userdb` database
DROP TABLE userdb.employeetable;

-- Assumes a table named `employeetable` does not exist.
-- Throws exception
DROP TABLE employeetable;
Error: org.apache.spark.sql.AnalysisException: Table or view not found: employeetable;
(state=,code=0)

-- Assumes a table named `employeetable` does not exist,Try with IF EXISTS
-- this time it will not throw exception
DROP TABLE IF EXISTS employeetable;

-- Completely purge the table skipping trash.
DROP TABLE employeetable PURGE;