SQL Data Types: A Comprehensive Guide

Data types define the type of data that can be stored in a column of a table. They are crucial for database design as they help ensure data integrity and optimize storage and performance. Different SQL databases support various data types, but most share common categories.

Categories of Data Types

image Explore and Read Our Blogs Written By Our Insutry Experts Learn From KSR Data Vizon

Numeric Data Types

Used for storing numeric values, including integers and floating-point numbers.

Data TypeDescriptionExample
INTA standard integer type.id INT
SMALLINTA smaller range integer type.age SMALLINT
BIGINTA larger range integer type.population BIGINT
FLOATA floating-point number.price FLOAT
DOUBLEA double-precision floating-point number.weight DOUBLE
DECIMAL(p,s)A fixed-point number with p total digits and s decimal places.salary DECIMAL(10,2)

Note: The above data types are consistent across MySQL, PostgreSQL, and MSSQL.

String Data Types

Used for storing text data, including characters and variable-length strings.

Data TypeDescriptionExample
CHAR(n)Fixed-length string, padded with spaces if necessary.code CHAR(5)
VARCHAR(n)Variable-length string, up to n characters.name VARCHAR(100)
TEXTVariable-length string with a maximum length of 65,535 bytes (in MySQL).description TEXT
NCHAR(n)Fixed-length Unicode string.unicode_char NCHAR(10)
NVARCHAR(n)Variable-length Unicode string.unicode_name NVARCHAR(100)
CLOBCharacter large object for storing large text data (in PostgreSQL and MSSQL).bio CLOB

Note: TEXT and CLOB types may differ in maximum size limits across databases.

Date and Time Data Types

Used for storing dates, times, and timestamps.

Data TypeDescriptionExample
DATEStores date values (year, month, day).birthdate DATE
TIMEStores time values (hour, minute, second).event_time TIME
DATETIMECombines date and time values.created_at DATETIME
TIMESTAMPStores timestamp (date and time), auto-updating for changes.last_updated TIMESTAMP
INTERVALRepresents a time span (in PostgreSQL).duration INTERVAL

Note: The TIMESTAMP type may auto-update on certain actions in some databases.

Boolean Data Type

Used for storing truth values.

Data TypeDescriptionExample
BOOLEANStores TRUE, FALSE, or NULL.is_active BOOLEAN

Note: Supported consistently across MySQL, PostgreSQL, and MSSQL.

Binary Data Types

Used for storing binary data, such as images and files.

Data TypeDescriptionExample
BINARY(n)Fixed-length binary data.image BINARY(100)
VARBINARY(n)Variable-length binary data.file VARBINARY(500)
BLOBBinary large object for storing large binary data (in MySQL).file BLOB

Note: The BLOB type is specific to MySQL, while PostgreSQL uses BYTEA.

Creating a Table with Various Data Types

Here’s an example of how to create a table using various data types in SQL:

CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    salary DECIMAL(10, 2),
    hire_date DATE,
    is_active BOOLEAN,
    profile_picture BLOB
);

Conclusion

Choosing the right data types in SQL is essential for database design, impacting both performance and data integrity. By understanding the available data types—numeric, string, date and time, boolean, and binary—database administrators can optimize their databases for efficient storage and retrieval.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *