Column data types (INT, VARCHAR, DATE, DECIMAL) in SQL - Time & Space Complexity
When working with column data types like INT, VARCHAR, DATE, and DECIMAL, it's helpful to understand how the choice affects the time it takes to process data.
We want to know how the time to handle data changes as the amount of data grows.
Analyze the time complexity of inserting rows with different column data types.
INSERT INTO sales_data (id, product_name, sale_date, price) VALUES
(1, 'Widget', '2024-01-01', 19.99),
(2, 'Gadget', '2024-01-02', 29.99),
(3, 'Thingamajig', '2024-01-03', 9.99);
This code inserts multiple rows with columns of types INT, VARCHAR, DATE, and DECIMAL.
Look at what repeats as data grows.
- Primary operation: Inserting each row into the table.
- How many times: Once per row inserted.
As you add more rows, the database processes each row one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 insert operations |
| 100 | 100 insert operations |
| 1000 | 1000 insert operations |
Pattern observation: The time grows directly with the number of rows added.
Time Complexity: O(n)
This means the time to insert data grows in a straight line as you add more rows.
[X] Wrong: "Using different data types like VARCHAR or DECIMAL makes the insert time grow faster than INT."
[OK] Correct: While data types affect storage size, the main time cost comes from how many rows you insert, not the type itself.
Understanding how data types relate to time complexity helps you explain database performance clearly and confidently.
"What if we added an index on the product_name column? How would the time complexity of inserts change?"
Practice
Solution
Step 1: Understand the data type purpose
VARCHAR is used to store text or strings, like names.Step 2: Match data type to data
A person's full name is text, so VARCHAR fits best.Final Answer:
VARCHAR -> Option DQuick Check:
Text data = VARCHAR [OK]
- Choosing INT for text
- Using DATE for names
- Using DECIMAL for text
Solution
Step 1: Identify the data type for decimal numbers
DECIMAL is used for numbers with fixed decimal places, like prices.Step 2: Check syntax correctness
DECIMAL(10,2) means total 10 digits, 2 after decimal point, which is correct.Final Answer:
price DECIMAL(10,2) -> Option BQuick Check:
Decimal numbers = DECIMAL(precision, scale) [OK]
- Using VARCHAR for numbers
- INT with decimals
- DATE for numeric values
CREATE TABLE Orders (OrderID INT, OrderDate DATE, CustomerName VARCHAR(50), TotalAmount DECIMAL(8,2));What will be the result of this insert?
INSERT INTO Orders VALUES (101, '2024-05-10', 'Alice', 123.456);Solution
Step 1: Understand DECIMAL(8,2) behavior
DECIMAL(8,2) stores numbers with 2 decimal places, rounding if needed.Step 2: Analyze the inserted value 123.456
Since only 2 decimals allowed, 123.456 rounds to 123.46.Final Answer:
The TotalAmount will store 123.46, rounding to 2 decimals -> Option AQuick Check:
DECIMAL rounds to defined decimals [OK]
- Expecting exact 3 decimals stored
- Thinking insert fails
- Assuming truncation without rounding
CREATE TABLE Employees (ID INT, JoinDate VARCHAR(10));Why might storing dates as VARCHAR cause problems?
Solution
Step 1: Understand VARCHAR vs DATE for dates
VARCHAR stores text without checking if it is a valid date.Step 2: Identify problems with VARCHAR for dates
Data can be inconsistent or invalid, causing errors in date operations.Final Answer:
Because VARCHAR does not enforce date format, leading to inconsistent data -> Option AQuick Check:
Use DATE type to enforce valid dates [OK]
- Thinking INT stores dates
- Believing VARCHAR can't store text
- Assuming DATE is slower
- Product ID as a whole number
- Product name up to 100 characters
- Release date
- Price with up to 7 digits total and 2 decimals
Which is the best SQL column definition for the Price column?
Solution
Step 1: Understand the requirements for Price
Price needs to store numbers with 2 decimal places and up to 7 digits total.Step 2: Match data type to requirements
DECIMAL(7,2) fits perfectly: 7 digits total, 2 after decimal point.Step 3: Eliminate wrong options
INT stores whole numbers only, VARCHAR stores text, DATE is for dates.Final Answer:
Price DECIMAL(7,2) -> Option CQuick Check:
Use DECIMAL for precise decimal numbers [OK]
- Using INT for prices with decimals
- Using VARCHAR for numbers
- Confusing DATE with price
Ready to prove it?
Pass the test and this pattern moves unknown
