DDL (CREATE, ALTER, DROP) in DBMS Theory - Time & Space Complexity
When working with database structure commands like CREATE, ALTER, and DROP, it's important to understand how the time to complete these commands changes as the database grows.
We want to know how the execution time changes when the database has more tables or larger table definitions.
Analyze the time complexity of the following DDL commands.
-- Create a new table
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(50)
);
-- Alter the table to add a new column
ALTER TABLE Employees ADD Salary DECIMAL(10,2);
-- Drop the table
DROP TABLE Employees;
These commands create a table, modify its structure, and then remove it from the database.
Look for operations that repeat or scale with input size.
- Primary operation: Scanning metadata and updating system catalogs for the table.
- How many times: Usually once per command, but may involve checking existing table definitions or dependencies.
Execution time depends mostly on the size of the table definition and related metadata, not on the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| Small table (few columns) | Few operations, quick execution |
| Medium table (dozens of columns) | More operations, slightly longer |
| Large table (hundreds of columns) | More metadata to process, longer time |
Pattern observation: Time grows roughly with the size of the table structure, not the data inside.
Time Complexity: O(m)
This means the time to execute DDL commands grows linearly with the size of the table's structure (number of columns or constraints).
[X] Wrong: "DDL commands take longer as the number of rows in the table grows."
[OK] Correct: DDL commands mainly change the table structure, not the data, so the number of rows usually does not affect execution time.
Understanding how DDL commands scale helps you explain database management tasks clearly and shows you grasp how databases handle structure changes efficiently.
"What if we altered a table with many foreign key constraints? How would the time complexity change?"
Practice
CREATE statement do in a database?Solution
Step 1: Understand the purpose of CREATE
The CREATE statement is used to make new database objects, such as tables, to store data.Step 2: Compare with other statements
DROP deletes tables, ALTER changes structure, but CREATE makes new tables.Final Answer:
It makes a new table to store data. -> Option AQuick Check:
CREATE = Make new table [OK]
- Confusing CREATE with DROP which deletes tables
- Thinking CREATE changes existing data
- Assuming CREATE backs up data
age of type INT to an existing table students?Solution
Step 1: Identify correct ALTER syntax
The correct syntax to add a column is: ALTER TABLE table_name ADD COLUMN column_name datatype;Step 2: Check each option
ALTER TABLE students ADD COLUMN age INT; matches the correct syntax. Options B and C misuse CREATE and DROP. ALTER students ADD age INT; misses TABLE keyword.Final Answer:
ALTER TABLE students ADD COLUMN age INT; -> Option BQuick Check:
ALTER TABLE + ADD COLUMN = ALTER TABLE students ADD COLUMN age INT; [OK]
- Omitting TABLE keyword after ALTER
- Using CREATE or DROP instead of ALTER
- Forgetting the COLUMN keyword (optional but clearer)
CREATE TABLE employees (id INT, name VARCHAR(50));ALTER TABLE employees ADD COLUMN salary INT;What will be the structure of the
employees table after these commands?Solution
Step 1: Analyze CREATE TABLE command
The first command creates a table named employees with columns id and name.Step 2: Analyze ALTER TABLE command
The second command adds a new column salary to the employees table.Final Answer:
Table with columns: id, name, salary -> Option AQuick Check:
CREATE + ALTER ADD COLUMN = id, name, salary [OK]
- Ignoring the ALTER command effect
- Assuming DROP was used to remove columns
- Thinking ALTER replaces columns instead of adding
ALTER TABLE employees DROP salary;Solution
Step 1: Understand DROP syntax in ALTER TABLE
To remove a column, syntax requires: ALTER TABLE table_name DROP COLUMN column_name;Step 2: Check the given statement
The statement misses the COLUMN keyword after DROP, causing syntax error.Final Answer:
Missing COLUMN keyword after DROP -> Option CQuick Check:
ALTER TABLE DROP COLUMN required [OK]
- Omitting COLUMN keyword after DROP
- Confusing DROP TABLE with DROP COLUMN
- Assuming DROP alone removes columns
orders and all its data from the database. Which SQL command should you use?Solution
Step 1: Understand the purpose of DROP TABLE
DROP TABLE deletes the entire table and its data permanently from the database.Step 2: Evaluate the options
DROP TABLE orders; uses correct syntax. ALTER TABLE orders DROP; and D misuse ALTER. CREATE TABLE orders; creates a table instead of deleting.Final Answer:
DROP TABLE orders; -> Option DQuick Check:
DROP TABLE = delete table permanently [OK]
- Using ALTER instead of DROP to delete tables
- Confusing CREATE with DROP
- Omitting TABLE keyword in DROP statement
Ready to prove it?
Pass the test and this pattern moves unknown
