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.

  • ๐Ÿ”ข Numeric types: TINYINT through BIGINT cover whole numbers, while DECIMAL(precision, scale) keeps exact values that FLOAT and DOUBLE cannot.
  • ๐Ÿ”ค String types: STRING carries no declared limit, VARCHAR accepts 1 to 65535 characters, and CHAR is fixed at up to 255.
  • ๐Ÿ“… Date and time: DATE stores a plain YYYY-MM-DD value and TIMESTAMP adds optional nanosecond precision on top of it.
  • ๐Ÿงฉ Complex types: ARRAY, MAP, STRUCT and UNIONTYPE pack several related values into one column instead of many.
  • ๐Ÿ—๏ธ Create a database: CREATE DATABASE registers a namespace in the metastore, and SHOW DATABASES confirms that it exists.
  • ๐Ÿ—‘๏ธ Drop a database: DROP DATABASE removes the namespace, and CASCADE is required whenever tables still sit inside it.

Hive data types and how to create and drop databases in Hive

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.

Hive shell creating the guru99 database and listing databases with show

From the above screenshot, we are doing two things:

  1. Creating database “guru99” in Hive
  2. 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.

Hive shell dropping the guru99 database and confirming removal with show

In the above screenshot, we are doing two things:

  1. We are dropping database ‘guru99’ from Hive
  2. 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.

FAQs

No. DATABASE and SCHEMA are interchangeable keywords in HiveQL, so CREATE SCHEMA sales and CREATE DATABASE sales produce the same metastore object. The choice is purely a matter of house style.

Under the warehouse directory, normally /user/hive/warehouse/database_name.db on HDFS. A LOCATION clause on CREATE DATABASE overrides that path, and DESCRIBE DATABASE reports whichever location was actually used.

STRING is the usual choice because it carries no declared limit and needs no padding. VARCHAR helps when a length must be enforced, and CHAR suits short fixed-width codes such as country abbreviations.

DOUBLE is binary floating point, so repeated sums drift by fractions of a cent. DECIMAL stores exact values at a stated precision and scale, which keeps financial totals reproducible across runs.

A plain TIMESTAMP is time-zone free and is read back exactly as written. Hive 3.1 added TIMESTAMP WITH LOCAL TIME ZONE, which normalises the value to UTC on write and converts it on read.

Yes. A STRUCT may sit inside an ARRAY and a MAP value may itself be a STRUCT, so ARRAY<STRUCT<city:STRING>> is valid in Hive. Deep nesting complicates delimiters, so keep it shallow.

Yes. Data profiling tools sample cardinality, null rates and value ranges, then suggest the narrowest workable type and a file format. Treat the suggestion as a draft, because the model cannot see future workloads.

Copilot drafts CREATE TABLE and CREATE DATABASE statements from a short comment, and agentic assistants can convert a sample file into a schema. Always check the DECIMAL scale and the delimiter clauses before running the result.

Summarize this post with: