SQL Data Types


When designing a database, one of the most important decisions you’ll make is choosing the correct SQL data types for your columns. SQL data types determine the kind of data that can be stored in a column and help ensure the integrity and performance of your database.


1. What Are SQL Data Types?

SQL data types define the kind of data a column can hold in a database table. Choosing the right data type is essential for optimal database performance, storage efficiency, and data integrity. Each SQL database (MySQL, PostgreSQL, SQL Server, etc.) may have slight variations in their supported data types, but the fundamental categories remain consistent.

The most common SQL data types include:

  • Numeric Data Types
  • String Data Types
  • Date and Time Data Types
  • Other Data Types

2. Numeric Data Types

Numeric data types are used to store numbers, whether they’re whole numbers (integers) or numbers with decimals (floating point numbers). Here are the main numeric types:

2.1 INT (Integer)

  • Description: Stores whole numbers.
  • Storage Size: Typically 4 bytes.
  • Range: Varies depending on the SQL database, but generally from -2,147,483,648 to 2,147,483,647 (for signed integers).
Example:
CREATE TABLE employees (
    employee_id INT
);

2.2 DECIMAL (or NUMERIC)

  • Description: Stores numbers with a fixed number of decimal places. Ideal for financial calculations.
  • Storage Size: Varies depending on precision and scale.
  • Range: Defines the total number of digits and how many digits are after the decimal point (e.g., DECIMAL(10, 2)).
Example:
CREATE TABLE products (
    price DECIMAL(10, 2)  -- 10 digits in total, 2 digits after the decimal point
);

2.3 FLOAT and DOUBLE

  • Description: These types store floating-point numbers (numbers with decimals) and are used for approximate calculations. DOUBLE provides more precision than FLOAT.
  • Storage Size: FLOAT uses 4 bytes; DOUBLE uses 8 bytes.
Example:
CREATE TABLE products (
    weight FLOAT
);

3. String Data Types

String data types are used to store text or alphanumeric values. Here’s a breakdown of the common string types:

3.1 VARCHAR (Variable Character)

  • Description: Stores variable-length strings. The length is specified when defining the column (e.g., VARCHAR(50)).
  • Storage Size: The actual storage depends on the length of the string.
  • Usage: Best for columns where the length of the data can vary.
Example:
CREATE TABLE employees (
    name VARCHAR(100)  -- Up to 100 characters
);

3.2 CHAR (Fixed-length Character)

  • Description: Stores fixed-length strings. If the string is shorter than the defined length, it will be padded with spaces.
  • Storage Size: The defined length (e.g., CHAR(10) always takes 10 bytes of storage, even if the string is shorter).
Example:
CREATE TABLE departments (
    department_code CHAR(5)  -- Always 5 characters, padded if needed
);

3.3 TEXT

  • Description: Stores large amounts of text. The size is usually much larger than VARCHAR or CHAR, and it can store up to several gigabytes of text data.
  • Storage Size: Varies, typically much larger than VARCHAR.
Example:
CREATE TABLE blog_posts (
    content TEXT  -- Store large articles or blog post content
);

4. Date and Time Data Types

SQL supports several types for storing date and time information, allowing you to store everything from just dates to full timestamps.

4.1 DATE

  • Description: Stores dates in the format YYYY-MM-DD.
  • Storage Size: Typically 3 bytes.
Example:
CREATE TABLE events (
    event_date DATE  -- Stores just the date (no time)
);

4.2 TIME

  • Description: Stores time values in the format HH:MI:SS.
  • Storage Size: Typically 3 bytes.
Example:
CREATE TABLE work_schedule (
    start_time TIME  -- Stores the start time of an event
);

4.3 DATETIME (or TIMESTAMP)

  • Description: Stores both date and time in the format YYYY-MM-DD HH:MI:SS. It’s often used for timestamps when you need both components.
  • Storage Size: Typically 8 bytes.
Example:
CREATE TABLE orders (
    order_time DATETIME  -- Stores both date and time when the order was placed
);

4.4 YEAR

  • Description: Stores a year in the format YYYY. Typically used for year-only data.
  • Storage Size: Typically 1 byte.
Example:
CREATE TABLE employees (
    hire_year YEAR  -- Store the year the employee was hired
);

5. Other SQL Data Types

SQL also includes other data types for special purposes. Here are a few examples:

5.1 BOOLEAN

  • Description: Stores boolean values (true or false).
  • Storage Size: Typically 1 byte.
Example:
CREATE TABLE users (
    is_active BOOLEAN  -- Stores whether the user is active (TRUE/FALSE)
);

5.2 BLOB (Binary Large Object)

  • Description: Used to store large binary data, such as images or files.
  • Storage Size: Varies depending on the size of the data.
Example:
CREATE TABLE files (
    file_data BLOB  -- Store files like images, videos, etc.
);

5.3 ENUM

  • Description: Stores a predefined set of values, which can be a string or number.
  • Storage Size: Varies depending on the number of possible values.
Example:
CREATE TABLE orders (
    order_status ENUM('Pending', 'Shipped', 'Delivered')  -- Limited choices for the order status
);

5.4 JSON

  • Description: Stores JSON (JavaScript Object Notation) data for flexible data storage.
  • Storage Size: Varies depending on the size and structure of the JSON data.
Example:
CREATE TABLE configurations (
    settings JSON  -- Store configuration settings in JSON format
);