Relational database concepts in MySQL - Time & Space Complexity
When working with relational databases, it is important to understand how the time to run queries grows as the data grows.
We want to know how the number of operations changes when the database tables get bigger.
Analyze the time complexity of the following SQL query.
SELECT *
FROM employees
WHERE department_id = 5;
This query selects all employees who belong to department number 5 from the employees table.
Look at what repeats when the query runs.
- Primary operation: Checking each row in the employees table to see if the department_id matches 5.
- How many times: Once for every row in the employees table.
As the number of employees grows, the query has to check more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line with the number of rows in the table.
[X] Wrong: "The query time stays the same no matter how many rows there are."
[OK] Correct: The database must check each row to find matches, so more rows mean more work.
Understanding how query time grows helps you write better database queries and explain your thinking clearly in interviews.
"What if the employees table had an index on department_id? How would the time complexity change?"
Practice
primary key in a relational database table?Solution
Step 1: Understand the role of primary keys
A primary key is a column or set of columns that uniquely identifies each row in a table.Step 2: Compare options with the definition
Only To uniquely identify each record in the table describes this unique identification role correctly.Final Answer:
To uniquely identify each record in the table -> Option DQuick Check:
Primary key = unique record ID [OK]
- Confusing primary key with foreign key
- Thinking primary key stores large data
- Assuming primary key speeds backups
id in a MySQL table?Solution
Step 1: Recall MySQL primary key syntax
In MySQL, the primary key is declared usingPRIMARY KEY (column_name).Step 2: Match syntax with options
PRIMARY KEY (id); matches the correct syntax with parentheses around the column name.Final Answer:
PRIMARY KEY (id); -> Option AQuick Check:
Primary key syntax uses parentheses [OK]
- Omitting parentheses around column name
- Swapping order of keywords
- Using incorrect keywords like KEY PRIMARY
Customers(id, name) and Orders(order_id, customer_id), which SQL query correctly retrieves all orders with the customer names?Solution
Step 1: Identify the correct join condition
The foreign keycustomer_idinOrderslinks toidinCustomers.Step 2: Check each option's join condition
SELECT Orders.order_id, Customers.name FROM Orders JOIN Customers ON Orders.customer_id = Customers.id; correctly joins onOrders.customer_id = Customers.id. Others use wrong columns or join types.Final Answer:
SELECT Orders.order_id, Customers.name FROM Orders JOIN Customers ON Orders.customer_id = Customers.id; -> Option BQuick Check:
Join on foreign key = primary key [OK]
- Swapping join columns incorrectly
- Using WHERE instead of JOIN condition properly
- Mixing up LEFT JOIN with INNER JOIN
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id). But it gives an error. What is the likely cause?Solution
Step 1: Understand foreign key requirements
A foreign key must reference a column that exists and is a primary or unique key in the referenced table.Step 2: Analyze the error cause
IfCustomershas nocustomer_idcolumn or it is not a key, the foreign key declaration fails.Final Answer:
The referenced column customer_id does not exist or is not a primary key in Customers -> Option CQuick Check:
Foreign key must reference existing primary key [OK]
- Referencing non-existent columns
- Assuming foreign keys must reference 'id' column
- Incorrect order of key declarations
Employees(emp_id, name) and Departments(dept_id, dept_name, manager_id). The manager_id in Departments references emp_id in Employees. Which of these statements is TRUE about this relationship?Solution
Step 1: Understand foreign key relationship
Themanager_idin Departments points toemp_idin Employees, linking each department to one employee as manager.Step 2: Analyze relationship cardinality
One department has one manager, but an employee can manage many departments (one-to-many).Step 3: Evaluate options
An employee can manage multiple departments, but a department can have only one manager correctly states this one-to-many relationship. Each department must have exactly one manager who is an employee is wrong because departments can have one manager, but not necessarily exactly one if NULL allowed. A department can have multiple managers if multiple employees share the same emp_id is wrong because emp_id is unique. The manager_id column in Departments can be NULL only if emp_id is NULL in Employees is incorrect because NULL in manager_id is independent of emp_id NULL.Final Answer:
An employee can manage multiple departments, but a department can have only one manager -> Option AQuick Check:
One-to-many foreign key = An employee can manage multiple departments, but a department can have only one manager [OK]
- Assuming multiple managers per department
- Confusing NULL constraints
- Thinking emp_id can be duplicated
Ready to prove it?
Pass the test and this pattern moves unknown
