删除表

描述

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;