Hive Data Types: How to Create and Drop Databases in Hive
โก Smart Summary
Hive data types define what each table column can hold, and the CREATE DATABASE and DROP DATABASE commands set up or remove the namespace those tables live in inside the Hive metastore.

Data types are very important elements in Hive query language and data modeling. For defining the table column types, we must have to know about the data types and their usage.
The following gives a brief overview of some data types present in Hive:
- Numeric Types
- String Types
- Date/Time Types
- Complex Types
Data types in Hive
Every Hive column is declared with one of the types below. The primitive families come first, followed by the complex types that hold more than one value in a single column.
Hive Numeric Data Types
Numeric columns range from a single byte to eight bytes, so picking the smallest type that fits the values keeps both storage and scan cost down.
| Type | Memory allocation |
|---|---|
| TINYINT | 1-byte signed integer (-128 to 127) |
| SMALLINT | 2-byte signed integer (-32,768 to 32,767) |
| INT | 4-byte signed integer (-2,147,483,648 to 2,147,483,647) |
| BIGINT | 8-byte signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) |
| FLOAT | 4-byte single precision floating point number |
| DOUBLE | 8-byte double precision floating point number |
| DECIMAL | We can define precision and scale in this type, as DECIMAL(precision, scale). When they are omitted Hive uses DECIMAL(10,0) |
Hive String Data Types
Character columns come in three shapes, and the difference is whether Hive enforces a declared length.
| Type | Length |
|---|---|
| CHAR | Fixed length, up to 255 characters. Shorter values are padded with spaces |
| VARCHAR | 1 to 65,535 characters. A longer value is silently truncated |
| STRING | We can define length here (no limit) |
Hive Date/Time Data Types
Temporal columns are stored without any time-zone offset, which is worth remembering before comparing values written by different clusters.
| Type | Usage |
|---|---|
| Timestamp | Supports traditional Unix timestamp with optional nanosecond precision |
| Date | It is in YYYY-MM-DD format. The range of values supported for the Date type is 0000-01-01 to 9999-12-31, dependent on support by the primitive Java Date type |
Hive Miscellaneous Data Types
Two further primitives sit outside the numeric, string and date families but appear regularly in real schemas.
| Type | Usage |
|---|---|
| BOOLEAN | Stores true or false |
| BINARY | Stores an array of bytes, which keeps raw content out of a STRING column |
Hive Complex Data Types
Complex types nest values inside one column, which removes the extra join that a relational design would need.
| Type | Usage |
|---|---|
| Arrays | ARRAY<data_type> Negative values and non-constant expressions not allowed |
| Maps | MAP<primitive_type, data_type> Negative values and non-constant expressions not allowed |
| Structs | STRUCT<col_name : data_type [COMMENT col_comment], …> |
| Union | UNIONTYPE<data_type, data_type, …> |
Hive Complex Data Types Example
The table above gives the declaration form. The statement below puts three of the complex types into one table so the delimiter clauses and the access syntax can be seen together.
CREATE TABLE employees ( name STRING, skills ARRAY<STRING>, deductions MAP<STRING, FLOAT>, address STRUCT<street:STRING, city:STRING, pin:INT> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY ':' LINES TERMINATED BY '\n' STORED AS TEXTFILE;
Three separate delimiters are needed here: one between columns, a second between the items of an ARRAY or STRUCT, and a third between a MAP key and its value. Once the table exists, each complex column is read with its own accessor.
SELECT name, skills[0], deductions['tax'], address.city FROM employees;
The table below summarises which accessor belongs to which type.
| Type | How a value is read |
|---|---|
| ARRAY | Zero-based index, such as skills[0] |
| MAP | Key lookup in square brackets, such as deductions[‘tax’] |
| STRUCT | Dot notation on the field name, such as address.city |
| UNIONTYPE | Rarely used, because query support for it has stayed incomplete |
Complex types may be nested, so ARRAY<STRUCT<street:STRING, city:STRING>> is a valid column declaration. Once the column layout is settled, the tables themselves are built with the statements covered in Hive create, alter and drop table.
How to Create and Drop Databases in Hive
A database in Hive is simply a namespace that groups tables, so it is the first object to create before any table work begins. Following are the steps on how to create and drop databases in Hive.
Step 1) Create Database in Hive
For creating a database in Hive shell, we have to use the command as shown in the syntax below:
Syntax:
Create database <DatabaseName>
Example: Create database “guru99”
The screenshot below shows the create statement followed by a show command listing every database on the cluster.
From the above screenshot, we are doing two things:
- Creating database “guru99” in Hive
- Displaying existing databases using the “show” command
In the same screen, database “guru99” is displayed at the end when we execute the show command, which means database “guru99” is successfully created.
Step 2) Drop Database in Hive
For dropping a database in Hive shell, we have to use the “drop” command as shown in the syntax below:
Syntax:
Drop database <DatabaseName>
Example: Drop database guru99
In the next screenshot the drop statement runs first, and the show command that follows no longer lists the database.
In the above screenshot, we are doing two things:
- We are dropping database ‘guru99’ from Hive
- Cross checking the same with the “show” command
In the same screen, after checking databases with the show command, database “guru99” does not appear inside Hive. So we can confirm now that database “guru99” is dropped.
Hive Database Commands: USE, DESCRIBE, SHOW and ALTER DATABASE
The two statements above use their shortest form. The documented syntax carries optional clauses that decide where the files land and what happens when the name is already taken.
CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_name=property_value, ...)];
DROP DATABASE [IF EXISTS] database_name [RESTRICT|CASCADE];
The keywords DATABASE and SCHEMA are interchangeable throughout, so CREATE SCHEMA and CREATE DATABASE do exactly the same thing. The remaining commands round out day-to-day work with a namespace.
| Command | What it does |
|---|---|
| SHOW DATABASES; | Lists every database registered in the metastore |
| USE database_name; | Sets the current database so table names need no prefix |
| DESCRIBE DATABASE database_name; | Reports the comment, the HDFS location and the owner |
| DESCRIBE DATABASE EXTENDED database_name; | Adds the DBPROPERTIES key-value pairs to that output |
| ALTER DATABASE database_name SET DBPROPERTIES (…); | Adds or replaces metadata properties |
| ALTER DATABASE database_name SET OWNER USER user_name; | Transfers ownership to another user or role |
| ALTER DATABASE database_name SET LOCATION hdfs_path; | Changes the path used by tables created from then on |
Two defaults are worth memorising. Without a LOCATION clause the files sit under the warehouse directory, normally /user/hive/warehouse/database_name.db, and without IF EXISTS a drop against a missing name raises an error instead of passing quietly. Both settings are held in the Hive metastore.
Hive Data Type Conversion and Common Errors
Types are not always used exactly as declared. Hive widens a value automatically when no information can be lost, and leaves everything else to an explicit conversion.
- Implicit widening follows the chain TINYINT to SMALLINT to INT to BIGINT to FLOAT to DOUBLE, and a STRING that holds digits is promoted to DOUBLE.
- Narrowing never happens on its own, so a DOUBLE assigned to an INT column needs a written conversion.
- CAST performs that written conversion, and it returns NULL rather than an error when the value cannot be converted.
SELECT CAST(salary AS DECIMAL(10,2)) FROM employees;
The errors below account for most of the surprises beginners meet on their first schema.
| Symptom | Cause and fix |
|---|---|
| Drop fails while the database still holds tables | RESTRICT is the default. Append CASCADE to drop the tables with it |
| A long string arrives shortened | VARCHAR truncates silently past its declared length. Use STRING when no limit is wanted |
| A column reads as NULL after a conversion | CAST could not parse the value. Check the source data before widening the type |
| Currency values lose their paise or cents | DECIMAL without arguments means DECIMAL(10,0). State the scale explicitly |
| Two identical-looking dates never match | A STRING date and a DATE column are different types. Cast one side before comparing |
Once the types behave, the next step is loading and querying the data itself, which is covered in Hive queries with Order By, Group By and Cluster By.


