Implementation:Heibaiying BigData Notes Hive Create Database
| Knowledge Sources | |
|---|---|
| Domains | Data_Warehouse, Big_Data |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Concrete tool for creating, selecting, listing, and dropping Hive databases provided by Apache Hive.
Description
Hive database DDL statements allow users to create logical namespaces that organize tables, views, and other schema objects. Each database maps to a directory on HDFS under the Hive warehouse path. The CREATE DATABASE statement provisions the namespace and its HDFS directory, USE switches the active database context, SHOW DATABASES lists all available databases, and DROP DATABASE removes a database and optionally all its contents.
These operations are executed as HiveQL statements through the Hive CLI, Beeline client, or any JDBC/ODBC-connected tool. They are metadata-only operations (except DROP CASCADE, which also removes HDFS data for managed tables).
Usage
Use these statements to set up and manage the top-level organizational structure of a Hive data warehouse. Typical scenarios include initializing project databases during environment setup, switching contexts in multi-database scripts, and cleaning up deprecated databases.
Code Reference
Source Location
- Repository: BigData-Notes
- File:
notes/Hive常用DDL操作.md(Lines 1-453)
Signature
-- Create a database
CREATE DATABASE [IF NOT EXISTS] db_name
[COMMENT 'description']
[LOCATION 'hdfs_path']
[WITH DBPROPERTIES (key='value', ...)];
-- Switch to a database
USE db_name;
-- Drop a database
DROP DATABASE [IF EXISTS] db_name [RESTRICT | CASCADE];
-- List databases
SHOW DATABASES [LIKE 'pattern'];
Import
N/A (HiveQL statements executed via Hive CLI or Beeline)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| db_name | STRING | Yes | Name of the database to create, use, or drop |
| COMMENT | STRING | No | Human-readable description stored in the metastore |
| LOCATION | STRING (HDFS path) | No | Custom HDFS directory path for the database; defaults to /user/hive/warehouse/db_name.db
|
| DBPROPERTIES | MAP<STRING, STRING> | No | Key-value metadata properties associated with the database |
| CASCADE | Keyword | No | When dropping, removes all tables and data within the database first |
| LIKE pattern | STRING | No | Glob pattern to filter database names when listing (e.g., 'sales_*')
|
Outputs
| Name | Type | Description |
|---|---|---|
| Database directory | HDFS directory | Created at the specified or default HDFS location upon CREATE DATABASE |
| Metastore entry | Metadata | Database registration in the Hive metastore including name, comment, location, and properties |
| Database list | Result set | Output of SHOW DATABASES listing matching database names |
Usage Examples
-- Create a database with a comment and custom location
CREATE DATABASE IF NOT EXISTS sales_dw
COMMENT 'Sales data warehouse'
LOCATION '/data/warehouse/sales_dw'
WITH DBPROPERTIES ('owner'='data_team', 'env'='production');
-- List all databases matching a pattern
SHOW DATABASES LIKE 'sales_*';
-- Switch to the new database
USE sales_dw;
-- Verify the current database context by showing tables
SHOW TABLES;
-- Drop the database and all its contents
DROP DATABASE IF EXISTS sales_dw CASCADE;