- SQL Basics
-
Overview
- Introduction to Databases and SQL
- Creating and Managing Tables in SQL
- Data Types and Constraints
- SELECT Queries and Filtering Data
- GROUP BY, HAVING, and ORDER BY
- SQL Joins (INNER, LEFT, RIGHT, FULL)
- Subqueries and Nested Queries
- UNION, INTERSECT, and EXCEPT
- Common Table Expressions (CTE)
- SQL Views and Stored Procedures
Data Types and Constraints
Add to BookmarkIntroduction
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 Data Types
SQL supports different data types categorized into numeric, string, date/time, and other specialized types.
1. Numeric Data 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 values
2. String (Character) Data Types
Used for storing textual data.
CHAR(n)
– Fixed-length string ofn
charactersVARCHAR(n)
– Variable-length string up ton
charactersTEXT
– Large text storage (specific to some database systems)
3. Date and Time Data Types
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 time
4. Other Data Types
Some SQL databases support specialized data types:
BOOLEAN
– Stores TRUE or FALSE valuesBLOB
– Stores binary large objects (e.g., images, files)JSON
– Stores JSON formatted data
SQL Constraints
Constraints are rules applied to table columns to enforce data integrity and accuracy.
1. NOT NULL Constraint
Ensures that a column cannot store NULL values.
CREATE TABLE Students (
ID INT NOT NULL,
Name VARCHAR(100) NOT NULL
);
2. UNIQUE Constraint
Ensures that all values in a column are unique.
CREATE TABLE Employees (
EmpID INT UNIQUE,
Email VARCHAR(255) UNIQUE
);
3. PRIMARY KEY Constraint
A combination of NOT NULL
and UNIQUE
, ensuring each row has a unique identifier.
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100)
);
4. FOREIGN KEY Constraint
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)
);
5. CHECK Constraint
Limits the values that can be stored in a column.
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Price DECIMAL(10,2) CHECK (Price > 0)
);
6. DEFAULT Constraint
Sets a default value for a column when no value is provided.
CREATE TABLE Users (
UserID INT PRIMARY KEY,
Status VARCHAR(20) DEFAULT 'Active'
);
7. AUTO_INCREMENT
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
);
8. INDEX
Speeds up data retrieval from a database table.
CREATE INDEX idx_customer_name ON customers (name);
9. CASCADE (ON DELETE & ON UPDATE)
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
);
List of Commonly used SQL data Types
1. Numeric Data Types
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 point
2. String (Character) Data Types
CHAR(n)
– Fixed-length stringVARCHAR(n)
– Variable-length stringTEXT
– Large text dataTINYTEXT
– Very small textMEDIUMTEXT
– Medium-sized textLONGTEXT
– Large text
3. Date and Time Data Types
DATE
– 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)
4. Boolean Data Type
BOOLEAN
/ BOOL
– Stores TRUE (1) or FALSE (0)
5. Binary Data Types
BLOB
– Binary Large ObjectTINYBLOB
– Very small binary objectMEDIUMBLOB
– Medium-sized binary objectLONGBLOB
– Large binary object
6. JSON and Special Data Types
JSON
– Stores JSON data (supported in MySQL & PostgreSQL)ENUM
– Stores predefined set of valuesSET
– Stores multiple predefined values
Conclusion
Understanding 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.
Prepare for Interview
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
Random Blogs
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Understanding SQL vs MySQL vs PostgreSQL vs MS SQL vs Oracle and Other Popular Databases
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- Transforming Logistics: The Power of AI in Supply Chain Management
- How AI is Making Humans Weaker – The Hidden Impact of Artificial Intelligence
- Role of Digital Marketing Services to Uplift Online business of Company and Beat Its Competitors
- Exploratory Data Analysis On Iris Dataset
- The Ultimate Guide to Data Science: Everything You Need to Know
- Big Data: The Future of Data-Driven Decision Making
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- 15 Amazing Keyword Research Tools You Should Explore
- Top 10 Knowledge for Machine Learning & Data Science Students
- Best Platform to Learn Digital Marketing in Free
- Python Challenging Programming Exercises Part 2
- String Operations in Python
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset