DROP DATABASE Statement

Removes a database from the system. The physical operations involve removing the metadata for the database from the metastore, and deleting the corresponding *.db directory from HDFS.

Syntax:

  1. DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT | CASCADE];

Statement type: DDL

Usage notes:

By default, the database must be empty before it can be dropped, to avoid losing any data.

In Impala 2.3 and higher, you can include the CASCADE clause to make Impala drop all tables and other objects in the database before dropping the database itself. The RESTRICT clause enforces the original requirement that the database be empty before being dropped. Because the RESTRICT behavior is still the default, this clause is optional.

The automatic dropping resulting from the CASCADE clause follows the same rules as the corresponding DROP TABLE, DROP VIEW, and DROP FUNCTION statements. In particular, the HDFS directories and data files for any external tables are left behind when the tables are removed.

When you do not use the CASCADE clause, drop or move all the objects inside the database manually before dropping the database itself:

  • Use the SHOW TABLES statement to locate all tables and views in the database, and issue DROP TABLE and DROP VIEW statements to remove them all.

  • Use the SHOW FUNCTIONS and SHOW AGGREGATE FUNCTIONS statements to locate all user-defined functions in the database, and issue DROP FUNCTION and DROP AGGREGATE FUNCTION statements to remove them all.

  • To keep tables or views contained by a database while removing the database itself, use ALTER TABLE and ALTER VIEW to move the relevant objects to a different database before dropping the original database.

You cannot drop the current database, that is, the database your session connected to either through the USE statement or the -d option of impala-shell. Issue a USE statement to switch to a different database first. Because the default database is always available, issuing USE default is a convenient way to leave the current database before dropping it.

Hive considerations:

When you drop a database in Impala, the database can no longer be used by Hive.

Examples:

See CREATE DATABASE Statement for examples covering CREATE DATABASE, USE, and DROP DATABASE.

Amazon S3 considerations:

In Impala 2.6 and higher, Impala DDL statements such as CREATE DATABASE, CREATE TABLE, DROP DATABASE CASCADE, DROP TABLE, and ALTER TABLE [ADD|DROP] PARTITION can create or remove folders as needed in the Amazon S3 system. Prior to Impala 2.6, you had to create folders yourself and point Impala database, tables, or partitions at them, and manually remove folders when no longer needed. See Using Impala with Amazon S3 Object Store for details about reading and writing S3 data with Impala.

Cancellation: Cannot be cancelled.

HDFS permissions:

The user ID that the impalad daemon runs under, typically the impala user, must have write permission for the directory associated with the database.

Examples:

  1. create database first_db;
  2. use first_db;
  3. create table t1 (x int);
  4. create database second_db;
  5. use second_db;
  6. -- Each database has its own namespace for tables.
  7. -- You can reuse the same table names in each database.
  8. create table t1 (s string);
  9. create database temp;
  10. -- You can either USE a database after creating it,
  11. -- or qualify all references to the table name with the name of the database.
  12. -- Here, tables T2 and T3 are both created in the TEMP database.
  13. create table temp.t2 (x int, y int);
  14. use database temp;
  15. create table t3 (s string);
  16. -- You cannot drop a database while it is selected by the USE statement.
  17. drop database temp;
  18. ERROR: AnalysisException: Cannot drop current default database: temp
  19. -- The always-available database 'default' is a convenient one to USE
  20. -- before dropping a database you created.
  21. use default;
  22. -- Before dropping a database, first drop all the tables inside it,
  23. -- or in Impala 2.3 and higher use the CASCADE clause.
  24. drop database temp;
  25. ERROR: ImpalaRuntimeException: Error making 'dropDatabase' RPC to Hive Metastore:
  26. CAUSED BY: InvalidOperationException: Database temp is not empty
  27. show tables in temp;
  28. +------+
  29. | name |
  30. +------+
  31. | t3 |
  32. +------+
  33. -- Impala 2.3 and higher:
  34. drop database temp cascade;
  35. -- Earlier releases:
  36. drop table temp.t3;
  37. drop database temp;

Related information:

Overview of Impala Databases, CREATE DATABASE Statement, USE Statement, SHOW DATABASES, DROP TABLE Statement

Parent topic: Impala SQL Statements