SQL FOUNDATIONS:Table Management (CREATE, ALTER, DROP)
Understand DDL with CREATE, ALTER, and DROP using schema previews.
SQL Table Management (CREATE, ALTER, DROP) Explained with Examples + Practice Questions
When to Use Table Management
- Use CREATE to create new tables
- Use ALTER to modify existing tables
- Use DROP to delete tables
These commands are part of DDL (Data Definition Language) and are used in:
- Database design
- Backend development
- Schema changes
Example: Table Structure
Table: employees
CREATE TABLE
Used to create a new table.
sqlCREATE TABLE employees (id INT PRIMARY KEY,name VARCHAR(50),department VARCHAR(50),salary INT);
With Constraints
sqlCREATE TABLE employees (id INT PRIMARY KEY,name VARCHAR(50) NOT NULL,department VARCHAR(50),salary INT DEFAULT 0);
Key Points:
- Define column names and data types
- Use constraints like PRIMARY KEY, NOT NULL, DEFAULT
- Plan schema carefully
ALTER TABLE
Used to modify an existing table.
Add Column
sqlALTER TABLE employeesADD email VARCHAR(100);
Modify Column
sqlALTER TABLE employeesMODIFY salary BIGINT;
Note: syntax may vary by database.
Rename Column
sqlALTER TABLE employeesRENAME COLUMN name TO full_name;
Drop Column
sqlALTER TABLE employeesDROP COLUMN email;
Key Points:
- Used for schema updates
- Syntax varies slightly across databases
- Always test before applying changes
DROP TABLE
Used to delete a table completely.
sqlDROP TABLE employees;
Important Warning:
- Deletes table structure and data permanently
- Cannot be undone
CREATE vs ALTER vs DROP
Key Concepts
- These are DDL commands
- Affect structure, not just data
- Often restricted in production environments
- Critical for database design
Practice Note
For safety, these exercises use schema inspection queries. When DDL is enabled later, you can swap to real CREATE/ALTER/DROP statements.
Table Management (CREATE, ALTER, DROP) Missions
Solve exercises in sequence to unlock the next mission.
Courses table schema
Create a table courses with id, course_name, duration.
Students columns
Add a column email to students table.
Column types
Modify column age to BIGINT.
Rename column
Rename column name to full_name.
Drop email
Drop column email.
Default value age
Create table with default value for age = 18.
Drop courses table
Drop the courses table.
NOT NULL name
Add NOT NULL constraint to name column.
Employees2 schema
Create a table with id primary key, name not null, salary default 0.