Data types and constraints are essential in SQL as they define the kind of data that can be stored in tables and ensure data integrity. Understanding these concepts is crucial for designing efficient and reliable databases.
SQL supports different data types categorized into numeric, string, date/time, and other specialized types.
Used for storing numbers, including integers and floating-point values.
INT – Stores whole numbers (e.g., 1, 2, 1000)BIGINT – Stores large integers (useful for big datasets)DECIMAL(p, s) / NUMERIC(p, s) – Stores exact numeric values with precision (p) and scale (s)FLOAT / REAL – Stores approximate floating-point valuesUsed for storing textual data.
CHAR(n) – Fixed-length string of n charactersVARCHAR(n) – Variable-length string up to n charactersTEXT – Large text storage (specific to some database systems)Used for handling date and time values.
DATE – Stores only date (YYYY-MM-DD)TIME – Stores only time (HH:MM:SS)DATETIME / TIMESTAMP – Stores both date and timeSome SQL databases support specialized data types:
BOOLEAN – Stores TRUE or FALSE valuesBLOB – Stores binary large objects (e.g., images, files)JSON – Stores JSON formatted dataConstraints are rules applied to table columns to enforce data integrity and accuracy.
Ensures that a column cannot store NULL values.
CREATE TABLE Students (
ID INT NOT NULL,
Name VARCHAR(100) NOT NULL
);Ensures that all values in a column are unique.
CREATE TABLE Employees (
EmpID INT UNIQUE,
Email VARCHAR(255) UNIQUE
);A combination of NOT NULL and UNIQUE, ensuring each row has a unique identifier.
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100)
);Establishes a relationship between two tables by linking a column to another table’s primary key.
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);Limits the values that can be stored in a column.
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Price DECIMAL(10,2) CHECK (Price > 0)
);Sets a default value for a column when no value is provided.
CREATE TABLE Users (
UserID INT PRIMARY KEY,
Status VARCHAR(20) DEFAULT 'Active'
);Automatically generates a unique number for each row in a column.
CREATE TABLE employees (
emp_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL
);Speeds up data retrieval from a database table.
CREATE INDEX idx_customer_name ON customers (name);Maintains referential integrity when a referenced record is deleted or updated.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE
);INT – Integer valueTINYINT – Very small integerSMALLINT – Small integerMEDIUMINT – Medium-sized integerBIGINT – Large integerDECIMAL(p, s) / NUMERIC(p, s) – Fixed-point precisionFLOAT – Floating point numberDOUBLE – Double precision floating pointCHAR(n) – Fixed-length stringVARCHAR(n) – Variable-length stringTEXT – Large text dataTINYTEXT – Very small textMEDIUMTEXT – Medium-sized textLONGTEXT – Large textDATE – Stores date (YYYY-MM-DD)DATETIME – Stores date and time (YYYY-MM-DD HH:MM:SS)TIMESTAMP – Stores timestamp valueTIME – Stores time (HH:MM:SS)YEAR – Stores a year (YYYY)BOOLEAN / BOOL – Stores TRUE (1) or FALSE (0)
BLOB – Binary Large ObjectTINYBLOB – Very small binary objectMEDIUMBLOB – Medium-sized binary objectLONGBLOB – Large binary objectJSON – Stores JSON data (supported in MySQL & PostgreSQL)ENUM – Stores predefined set of valuesSET – Stores multiple predefined valuesUnderstanding SQL data types and constraints is essential for designing structured and error-free databases. By selecting appropriate data types and applying constraints effectively, you can ensure data accuracy, consistency, and integrity in your database applications.
Sign in to join the discussion and post comments.
Sign in