Every column in an SQL table must have a specific data type, which defines the kind of values that can be stored in it. Choosing the right data type is essential for performance, storage efficiency, and data integrity.
SQL data types are mainly divided into the following categories:
Used to store numbers (integers, decimals, and floating-point values).
| Data Type | Description | Example |
|---|---|---|
INT |
Integer (whole number) | 25 |
SMALLINT |
Smaller integer (uses less storage) | 1000 |
BIGINT |
Large integer values | 9876543210 |
DECIMAL(p, s) |
Fixed-point decimal | DECIMAL(10,2) → 99999999.99 |
NUMERIC(p, s) |
Same as DECIMAL, ensures precision |
NUMERIC(8,3) → 12345.678 |
FLOAT(n) |
Floating-point number, less precision than DECIMAL | 3.14159 |
REAL |
Less precise floating point | 0.123456 |
Example: Creating a Table with Numeric Data Types
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Salary DECIMAL(10,2),
Age SMALLINT
);
Used to store text and alphanumeric characters.
| Data Type | Description | Example |
|---|---|---|
CHAR(n) |
Fixed-length string (up to 255 characters) | 'ABC' |
VARCHAR(n) |
Variable-length string (up to 65,535 characters) | 'Hello World' |
TEXT |
Large text data, unlimited size (depends on DBMS) | 'This is a long text...' |
Example: Creating a Table with String Data Types
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(50),
Address TEXT
);
Used to store dates, times, and timestamps.
| Data Type | Description | Example |
|---|---|---|
DATE |
Stores only date (YYYY-MM-DD) | '2025-02-03' |
TIME |
Stores only time (HH:MI:SS) | '14:30:00' |
DATETIME |
Stores date and time (YYYY-MM-DD HH:MI:SS) | '2025-02-03 14:30:00' |
TIMESTAMP |
Stores date and time with automatic updates | '2025-02-03 14:30:00' |
Example: Creating a Table with Date and Time Data Types
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDate DATE,
DeliveryTime TIME,
CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
BOOLEAN type (TRUE or FALSE).BOOLEAN is stored as TINYINT(1), where 0 = FALSE and 1 = TRUE.Example: Creating a Table with Boolean Data Type
CREATE TABLE Users (
UserID INT PRIMARY KEY,
IsActive BOOLEAN DEFAULT TRUE
);
Used for storing large files such as images, videos, and documents.
| Data Type | Description | Example |
|---|---|---|
BLOB |
Binary Large Object (images, videos) | - |
CLOB |
Character Large Object (large text data) | - |
Constraints are rules applied to table columns to ensure data accuracy and integrity.
A Primary Key is a unique identifier for a row. It must be unique and NOT NULL.
Example: Creating a Table with a Primary Key
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);
A Foreign Key links two tables, ensuring referential integrity.
Example: Foreign Key in SQL
CREATE TABLE Departments (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50)
);
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
DeptID INT,
FOREIGN KEY (DeptID) REFERENCES Departments(DeptID)
);
Prevents a column from having NULL values.
Example: NOT NULL Constraint
CREATE TABLE Users (
UserID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Email VARCHAR(100) NOT NULL
);
Ensures all values in a column are distinct.
Example: UNIQUE Constraint
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Email VARCHAR(100) UNIQUE
);
Ensures values meet specific conditions.
Example: CHECK Constraint
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Age INT CHECK (Age >= 18)
);
Sets a default value for a column if no value is provided.
Example: DEFAULT Constraint
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
Status VARCHAR(20) DEFAULT 'Pending'
);