SQL Data Types
SQL Data Types
SQL (Structured Query Language) is used to manage and manipulate data in relational databases. Each column in a table has a specific data type that defines the type of data it can hold. These data types help ensure data integrity and optimize storage.
Here’s a breakdown of SQL data types, categorized with syntax and examples:
1. Numeric Data Types
Numeric data types store numerical values, including integers, decimals, and floating-point numbers.
Integer Types:
-
TINYINT: A very small integer.- Syntax:
TINYINT[(M)] [UNSIGNED] [ZEROFILL] -
Example:
CREATE TABLE example ( small_value TINYINT );
- Syntax:
-
SMALLINT,MEDIUMINT,INT(orINTEGER),BIGINT: Store increasingly larger integer values.-
Example:
CREATE TABLE example ( medium_value MEDIUMINT, large_value BIGINT );
-
Example:
Decimal Types:
-
DECIMAL(orNUMERIC): For exact fixed-point numbers.- Syntax:
DECIMAL[(precision, scale)] -
Example:
CREATE TABLE example ( price DECIMAL(10, 2) -- 10 digits, 2 after the decimal point );
- Syntax:
Floating-Point Types:
-
FLOAT,DOUBLE(orREAL): For approximate values.-
Example:
CREATE TABLE example ( weight FLOAT, height DOUBLE );
-
Example:
2. Character/String Data Types
These store alphanumeric data.
Fixed-Length:
-
CHAR: Stores fixed-length strings.- Syntax:
CHAR(M) -
Example:
CREATE TABLE example ( fixed_name CHAR(10) );
- Syntax:
Variable-Length:
-
VARCHAR: Stores variable-length strings.- Syntax:
VARCHAR(M) -
Example:
CREATE TABLE example ( dynamic_name VARCHAR(50) );
- Syntax:
Large Text:
-
TEXT,TINYTEXT,MEDIUMTEXT,LONGTEXT: Store large amounts of text.-
Example:
CREATE TABLE example ( description TEXT );
-
Example:
3. Date and Time Data Types
These types store dates, times, or combinations of both.
Types:
-
DATE: Stores a date (YYYY-MM-DD).-
Example:
CREATE TABLE example ( birth_date DATE );
-
Example:
-
TIME: Stores time (HH:MM:SS).-
Example:
CREATE TABLE example ( event_time TIME );
-
Example:
-
DATETIME,TIMESTAMP: Stores date and time.-
Example:
CREATE TABLE example ( order_datetime DATETIME, last_updated TIMESTAMP );
-
Example:
-
YEAR: Stores a year (YYYY).-
Example:
CREATE TABLE example ( year_of_release YEAR );
-
Example:
4. Boolean Data Type
-
BOOLEAN: RepresentsTRUEorFALSE(internally stored asTINYINT(1)).-
Example:
CREATE TABLE example ( is_active BOOLEAN );
-
Example:
5. Binary Data Types
Binary data types store binary strings.
Types:
-
BINARY,VARBINARY: Fixed and variable-length binary data.-
Example:
CREATE TABLE example ( binary_data BINARY(16), varbinary_data VARBINARY(255) );
-
Example:
-
BLOB,TINYBLOB,MEDIUMBLOB,LONGBLOB: Store binary large objects.-
Example:
CREATE TABLE example ( image_data BLOB );
-
Example:
6. Spatial Data Types
Used for geographical data.
Types:
-
GEOMETRY,POINT,LINESTRING,POLYGON: Store spatial data.-
Example:
CREATE TABLE example ( location POINT );
-
Example:
General Syntax Example
Here’s a table creation example using multiple data types:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2),
hire_date DATE,
is_manager BOOLEAN
);
In this example:
idis an integer with auto-increment.nameis a variable-length string.salaryhas a fixed decimal format.hire_datestores date information.is_manageris a boolean flag.
By understanding these data types and using them appropriately, you can design efficient, maintainable database schemas.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand SQL Tutorial visually:
What You'll Learn:
- 📌 Understanding SQL Data Types: Strings, Integers, and Floats Explained
- 📌 SQL Tutorial #23 - SQL Data Types | Data Types in MySQL