Skip to Content
Course content

Data types in SQL define the kind of data that can be stored in a column of a table. They ensure consistency, accuracy, and efficient storage of data within a database.

2.2.1 What Are Data Types?

A data type specifies the type of value a column can store, such as numbers, text, dates, or binary data. When creating tables, defining appropriate data types is crucial to ensure proper handling of data.

2.2.2 Importance of Data Types in SQL

  1. Data Validation: Ensures that only valid data is entered into the database.
    • Example: A column defined as INTEGER won't accept text data like "hello."
  2. Memory Optimization: Helps allocate storage efficiently based on the type and size of data.
  3. Query Performance: Improperly chosen data types can slow down queries or increase storage requirements.
  4. Data Integrity: Ensures consistent and meaningful data storage.

2.2.3 Common SQL Data Types

SQL data types are broadly categorized into numeric, string, date/time, and other types. Let's look at them in detail:

1. Numeric Data Types

Used to store numbers, including integers and decimals.

Data Type Description Example
INT Stores whole numbers. 123, -456
BIGINT Stores larger whole numbers. 9876543210
DECIMAL(p,s) Stores precise fixed-point numbers. DECIMAL(10,2) -> 12345.67
FLOAT Stores approximate floating-point numbers. 123.45
SMALLINT Stores smaller whole numbers. -32,768 to 32,767

Example Usage:

CREATE TABLE products (
    product_id INT,
    price DECIMAL(10, 2)
);
2. String (Character) Data Types

Used to store text or string data.

Data Type Description Example
CHAR(n) Fixed-length string of n characters. CHAR(5) -> Hello
VARCHAR(n) Variable-length string of up to n characters. VARCHAR(20) -> SQL Tutorial
TEXT Large text data (size varies by database system). Article Content

Example Usage:

CREATE TABLE employees (
    emp_id INT,
    name VARCHAR(50),
    bio TEXT
);
3. Date and Time Data Types

Used to store dates, times, or both.

Data Type Description Example
DATE Stores date in YYYY-MM-DD format. 2024-12-14
TIME Stores time in HH:MM:SS format. 13:45:30
DATETIME Stores both date and time. 2024-12-14 13:45:30
TIMESTAMP Stores date and time with timezone. 2024-12-14 13:45:30+00:00

Example Usage:

CREATE TABLE orders (
    order_id INT,
    order_date DATE,
    delivery_time TIME
);
4. Binary Data Types

Used to store binary data such as images, files, or multimedia.

Data Type Description Example
BLOB Binary Large Object. Image or audio file
VARBINARY(n) Variable-length binary data. Binary strings

Example Usage:

CREATE TABLE documents (
    doc_id INT,
    content BLOB
);
5. Other Data Types

Used for specific use cases or database-dependent features.

Data Type Description Example
BOOLEAN Stores TRUE or FALSE values. TRUE or FALSE
JSON Stores JSON-formatted data. {"key": "value"}
UUID Stores universally unique identifiers. 550e8400-e29b-41d4

2.2.4 Choosing the Right Data Type

Selecting the right data type is critical for database performance and storage optimization. Here are some guidelines:

  1. Understand Data Requirements:
    • Use INT for numbers without decimals.
    • Use DECIMAL for precise monetary values.
  2. Avoid Over-Sizing:
    • Use VARCHAR(50) instead of VARCHAR(255) if the text length is known to be shorter.
  3. Consider Future Growth:
    • Use BIGINT for primary keys in large datasets.
  4. Use Specialized Types When Needed:
    • Use JSON for storing structured data like API responses.

2.2.5 Example Table with Various Data Types

CREATE TABLE users (
    user_id INT PRIMARY KEY,
    username VARCHAR(50),
    password CHAR(60),
    email VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN
);


Conclusion

Data types in SQL are crucial for ensuring the proper storage, retrieval, and manipulation of data in a database. By understanding and selecting the appropriate data type for each column, you can optimize database performance, reduce storage costs, and maintain data integrity.

Commenting is not enabled on this course.