CHAPTER 12
Beginner
SQL Data Types | Designing Tables and Storage Optimization
Updated: May 16, 2026
20 min read
# CHAPTER 12
Designing Tables and Choosing Data Types
1. Introduction
In the Logical Design phase, we established that a User has anage, a name, and an accountbalance. But when we move to Physical Design—actually writing the SQL CREATE TABLE commands—the database engine demands mathematical precision. You cannot just tell SQL "store a name." You must define exactly how many bytes of hard drive space that name is allowed to consume. Choosing the correct Data Types is critical for enforcing Data Integrity and preventing your server's hard drive from filling up with wasted space.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the architectural importance of Data Types.
-
Choose optimal Integer types (
TINYINT,INT,BIGINT).
-
Differentiate between String types (
CHAR,VARCHAR,TEXT).
- Select precise Decimal types for financial data.
-
Utilize Temporal types (
DATE,DATETIME,TIMESTAMP).
3. The Role of Data Types
A Data Type serves two massive architectural purposes:-
1.
Validation: If you define
ageas anINT(Integer), and a hacker tries to insert the word "Twenty", the database engine throws a fatal error. It physically blocks corrupted data.
- 2. Storage Optimization: If you define a boolean column (True/False) as a 255-character text string, you are wasting 254 bytes of space per row. Across 1 billion rows, you have wasted 254 Gigabytes of expensive SSD storage!
4. Numeric Data Types
Numbers are categorized by how large they can get.-
TINYINT: Uses 1 byte. Stores numbers from 0 to 255. (Perfect forageorstatuscodes).
-
INT: Uses 4 bytes. Stores up to 2.1 Billion. (The absolute industry standard foridPrimary Keys).
-
BIGINT: Uses 8 bytes. Stores massive numbers. (Used for Primary Keys at Facebook/Google scale, or for tracking global website views).
5. String (Text) Data Types
Text storage requires precision to prevent wasted space.-
CHAR(X): Fixed length. If you defineCHAR(10)and insert "Hi", the database secretly adds 8 blank spaces to fill the void. (Use ONLY for data that is *always* the exact same length, likestate_code'NY' or a 32-character MD5 Hash).
-
VARCHAR(X): Variable length.VARCHAR(255)means it can hold *up to* 255 characters, but if you insert "Hi", it only consumes 2 bytes! (The absolute industry standard foremail,names,titles).
-
TEXT: Used for massive blocks of text, like blog post contents or JSON payloads.
6. Financial Data Types (Crucial!)
WARNING: Never, ever useFLOAT or DOUBLE to store money. Floating-point numbers are mathematical approximations and will cause rounding errors (e.g., $10.00 might save as $9.99999).
-
DECIMAL(M, D): Mathematically exact.DECIMAL(10, 2)means the number can be up to 10 digits total, with exactly 2 digits after the decimal point (e.g.,99999999.99). This is mandatory for accounting systems.
7. Temporal Data Types (Time)
-
DATE: Stores Year, Month, Day (2024-10-31).
-
DATETIME: Stores Date and Time (2024-10-31 14:30:00).
-
TIMESTAMP: Similar to DATETIME, but automatically converts to UTC timezone for storage. (Highly recommended for logging when users click buttons globally).
8. Mini Project: Designing an Optimized User Table
Let's translate a conceptual requirement into highly optimized Physical SQL.
sql
9. Common Mistakes
-
The "VARCHAR(255) for Everything" Fallacy: Junior developers often default every single string column to
VARCHAR(255). While this doesn't waste hard drive space, it drastically reduces RAM efficiency when the database engine tries to sort or index the data. Restrict your strings to logical limits (e.g.,VARCHAR(50)for a name).
10. Best Practices
-
Use ENUM for Statuses: If an
order_statuscan only ever be "Pending", "Shipped", or "Delivered", use theENUM('Pending', 'Shipped', 'Delivered')data type. The database will physically block any other text from being inserted, protecting your application logic.
11. Exercises
- 1. What data type is the mandatory industry standard for storing monetary values like Prices or Bank Balances?
-
2.
What is the functional difference between
CHAR(50)andVARCHAR(50)?
12. Database Design Challenges
You are creating a database for a global airline. You need a column to store the 3-letter IATA Airport Code (e.g., "JFK", "LAX", "LHR"). What exact SQL Data Type is the most highly optimized choice for this specific column, and why? *(Answer:CHAR(3). Because the data is universally, exactly 3 characters long, fixed-length CHAR performs faster than variable-length VARCHAR and wastes zero space).*
13. MCQ Quiz with Answers
Question 1
Why is it considered a catastrophic architectural mistake to store financial data (like a bank account balance) using the FLOAT data type?
Question 2
When designing a table schema, a developer assigns an id column intended to handle a maximum of 50 distinct Departments. Which integer data type is the most highly optimized choice to minimize RAM and storage consumption?
14. Interview Questions
-
Q: Compare and contrast
DATETIMEandTIMESTAMP. In a globally distributed SaaS application where users exist across 10 different time zones, which data type should you use to record a "Purchase Event" and why?
-
Q: Explain the RAM and storage performance implications of using
CHAR(255)versusVARCHAR(255)when storing user First Names.
15. FAQs
Q: Can I change a data type after the table has 1 million rows? A: Yes, using theALTER TABLE command. However, be extremely careful. Changing a VARCHAR(255) down to a VARCHAR(10) will physically permanently delete (truncate) any existing data that is longer than 10 characters!