SQL Data Types ποΈΒΆ
Prerequisites: Introduction to SQL
Mentor's Note: Choosing a data type is like picking a container. You don't put soup in a sieve, and you don't put a 10-digit phone number in a 2-digit integer. Choosing the right "box" saves space and prevents errors! π‘
π The Scenario: The Storage Unit π¦ΒΆ
Imagine you are packing for a move.
- Small Box: For your keys (Short String/ID).
- Large Box: For your books (Long Text).
- Heavy Duty Box: For your weights (Big Numbers).
- Specialized Case: For your watch (Date & Time).
In SQL, every column needs a specific "box type" to hold its data.
π¨ Visual Logic: The Type CategoriesΒΆ
mindmap
root((SQL Data Types))
Numeric
INT/NUMBER
DECIMAL/FLOAT
String
CHAR (Fixed)
VARCHAR (Variable)
CLOB (Large Text)
Date-Time
DATE
TIMESTAMP
Other
BOOLEAN
BLOB (Images)
π» Common Data Types (Multi-Dialect)ΒΆ
1. String Types (The Text Boxes)ΒΆ
| Type | Use Case | Scenario |
|---|---|---|
| CHAR(n) | Fixed length | "IND", "USA" (Country Codes) π³οΈ |
| VARCHAR2(n) | Variable length | Name, Address (Saves space!) π |
| CLOB | Very long text | Product Reviews, Biographies π |
2. Numeric Types (The Calculators)ΒΆ
| Type | Use Case | Scenario |
|---|---|---|
| INT / NUMBER | Whole numbers | Student Roll No, Quantity π’ |
| DECIMAL(p,s) | Precise decimals | Salary (βΉ55000.50), Product Price π° |
| FLOAT | Scientific decimals | Weight, Distance π |
3. Date & Time Types (The Calendars)ΒΆ
| Type | Use Case | Scenario |
|---|---|---|
| DATE | Date only | Birthday, Joined Date π |
| TIMESTAMP | Date + Time | Exact Login Time β±οΈ |
π Comparison: CHAR vs VARCHAR2ΒΆ
Scenario: Storing the word "CAT" (3 letters) in a column of size 10.
| Feature | CHAR(10) | VARCHAR2(10) |
|---|---|---|
| Storage | Uses 10 bytes (Adds 7 spaces) | Uses 3 bytes (Saves 7 bytes!) |
| Performance | Faster for fixed data | Slightly slower, but efficient |
| Best For | Status Codes (Y/N) | Names, Descriptions |
π‘ Pro TipΒΆ
"Always use
VARCHAR2for names and addresses. UsingCHARfor a Name field will waste gigabytes of storage over time because of trailing spaces!"