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.

2. Categories of SQL 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

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)

6. VARCHAR Data Type

The VARCHAR data type stores variable-length character strings.

Name VARCHAR(50)

7. DATE Data Type

The DATE data type stores date values in YYYY-MM-DD format.

DOB DATE

8. DATETIME Data Type

The DATETIME data type stores both date and time.

CreatedAt DATETIME

9. DECIMAL Data Type

The DECIMAL data type stores exact numeric values with precision.

Salary DECIMAL(10,2)

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

  1. What are SQL data types?
  2. Differentiate CHAR and VARCHAR.
  3. What is the use of DECIMAL data type?
  4. Explain DATE and DATETIME.
  5. What is BIT data type?

Practice Task

Create a table student using: ✔ CHAR ✔ VARCHAR ✔ DATE ✔ BOOLEAN ✔ DECIMAL