SQL Data Types
๐ Welcome to SQL Data Types!โ
Hey there! If you're new to SQL or just getting started, this module is perfect for you. Data types are like labels that tell your database what kind of information (like numbers, text, or dates) can go into each column of a table. Think of them as rules to keep your data neat and safe! Letโs explore them step by step with simple examples.
๐ What Are Data Types?โ
Data types are super important because they:
- Decide what kind of data you can store (e.g., numbers or words).
- Help save space in your database.
- Prevent mistakes, like putting letters in a number column.
Imagine youโre making a table for a school called students with columns like id, name, and age. Data types ensure id is a number and name is text. Letโs see how!
Hereโs a handy table to get you started (check the image below for more details):

Pro Tip: Always pick the right data type to avoid confusion. For example, donโt use a text type for numbers!
๐ Numeric Data Types (Numbers Made Simple)โ
Numeric types are for numbersโwhole numbers or decimals. Letโs break them down:
- TINYINT: Small numbers, from -128 to 127 (or 0 to 255 if unsigned).
- INT: Bigger numbers, from -2,147,483,648 to 2,147,483,647.
- FLOAT: Decimals with up to 23 digits (e.g., 3.14).
- DOUBLE: Decimals with up to 53 digits (for super precise numbers).
- BIGINT: Huge numbers, from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Example:
- SQL Code
- Output
CREATE TABLE students (
id TINYINT,
age INT,
gpa FLOAT
);
INSERT INTO students (id, age, gpa) VALUES (1, 20, 3.75);
| id | age | gpa |
|---|---|---|
| 1 | 20 | 3.75 |
What NOT to Do: Donโt use FLOAT for money (e.g., $5.99) because it can have tiny rounding errors. Use DECIMAL instead for exact values like
DECIMAL(5,2)(5 digits total, 2 after the decimal).
๐ String Data Types (Text and More)โ
String types are for text or characters. Hereโs what you need:
- CHAR(length): Fixed length (e.g., CHAR(50) always uses 50 spaces, even if text is shorter).
- VARCHAR(length): Variable length up to a limit (e.g., VARCHAR(50) uses only whatโs needed, up to 50 characters).
- BLOB: For binary data like images or files (up to 65,535 bytes).
Example:
- SQL Code
- Output
CREATE TABLE students (
id TINYINT,
name VARCHAR(50)
);
INSERT INTO students (id, name) VALUES (2, 'Alice');
| id | name |
|---|---|
| 2 | Alice |
What NOT to Do: Avoid using CHAR for names if lengths vary a lotโit wastes space. Use VARCHAR instead!
๐ Date and Time Data Types (Tracking Time)โ
These types help with dates and times:
- DATE: Stores dates like '2025-08-14' (from 1000-01-01 to 9999-12-31).
- YEAR: Just the year, from 1901 to 2155 (e.g., 2025).
Example:
- SQL Code
- Output
CREATE TABLE students (
id TINYINT,
enrollment_date DATE
);
INSERT INTO students (id, enrollment_date) VALUES (3, '2025-08-14');
| id | enrollment_date |
|---|---|
| 3 | 2025-08-14 |
What NOT to Do: Donโt store dates as VARCHAR (e.g., '08-14-2025')โuse DATE to enable date functions like comparisons!
๐ Other Data Types (Special Cases)โ
- BOOLEAN: True (1) or False (0) values.
- BIT(x): Stores x bits (e.g., BIT(2) for 0 to 3).
Example:
- SQL Code
- Output
CREATE TABLE users (
id TINYINT,
is_active BOOLEAN
);
INSERT INTO users (id, is_active) VALUES (1, TRUE);
| id | is_active |
|---|---|
| 1 | 1 |
What NOT to Do: Donโt use INT for true/falseโBOOLEAN is clearer and safer.
๐งน Signed vs. Unsigned (Positive or Negative?)โ
For numeric types like TINYINT:
- Signed: Allows negative numbers (e.g., -128 to 127).
- Unsigned: Only positive numbers (e.g., 0 to 255).
Example:
- SQL Code
- Output
CREATE TABLE products (
stock TINYINT UNSIGNED
);
INSERT INTO products (stock) VALUES (200);
| stock |
|---|
| 200 |
What NOT to Do: Donโt use signed TINYINT for quantities (e.g., stock) that canโt be negativeโuse UNSIGNED to maximize the range.
โ What Youโve Learnedโ
Great job! Youโve explored:
- Numeric Types: TINYINT, INT, FLOAT, DOUBLE, BIGINT.
- String Types: CHAR, VARCHAR, BLOB.
- Date/Time Types: DATE, YEAR.
- Other Types: BOOLEAN, BIT.
- Signed vs. Unsigned: Choosing based on needs.
Try creating a table with these types and inserting data to practice. Avoid the "What NOT to Do" mistakes to keep your database happy!