SQL Data Types
SQL Data Types define the type of data that can be stored in a table column.
Choosing the correct data type ensures data accuracy, efficiency,
and better database performance.
1. What are SQL Data Types?
SQL data types specify the kind of values that can be stored in a column, such as numbers, characters, or dates.
- Control data storage
- Ensure valid data entry
- Improve database performance
2. Categories of SQL Data Types
- Numeric Data Types
- Character/String Data Types
- Date & Time Data Types
- Boolean Data Types
3. BIT Data Type
The BIT data type stores binary values (0 or 1). It is mainly used to represent flags.
IsActive BIT
- Stores 0 or 1
- Uses very little storage
4. BOOLEAN Data Type
The BOOLEAN data type stores TRUE or FALSE values. (Some DBMS internally store it as 0 and 1.)
IsVerified BOOLEAN
5. CHAR Data Type
The CHAR data type stores fixed-length character strings.
Gender CHAR(1)
- Fixed length
- Faster access
- Wastes space if data is shorter
6. VARCHAR Data Type
The VARCHAR data type stores variable-length character strings.
Name VARCHAR(50)
- Saves storage space
- Most commonly used string type
7. DATE Data Type
The DATE data type stores date values in YYYY-MM-DD format.
DOB DATE
- Stores only date
- No time component
8. DATETIME Data Type
The DATETIME data type stores both date and time.
CreatedAt DATETIME
- Stores date and time
- Used for logs and transactions
9. DECIMAL Data Type
The DECIMAL data type stores exact numeric values with precision.
Salary DECIMAL(10,2)
- Accurate for financial data
- Avoids rounding errors
10. Example Table Using Data Types
CREATE TABLE employee (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Gender CHAR(1),
Salary DECIMAL(10,2),
IsActive BOOLEAN,
JoinDate DATE,
CreatedAt DATETIME
);
11. CHAR vs VARCHAR
CHAR VARCHAR --------------------------- --------------------------- Fixed length Variable length Faster access Saves storage May waste space No space wastage
Practice Questions
- What are SQL data types?
- Differentiate CHAR and VARCHAR.
- What is the use of DECIMAL data type?
- Explain DATE and DATETIME.
- What is BIT data type?
Practice Task
Create a table student using:
✔ CHAR
✔ VARCHAR
✔ DATE
✔ BOOLEAN
✔ DECIMAL