DDL Commands:
What is DDL (Data Definition Language)?
DDL is used to define and manage the structure of database objects like tables, views,
schemas, constraints, etc.
These commands affect the schema, not the actual data.
Command Purpose / Action Syntax Example
CREATE TABLE Student ( RollNo INT
CREATE Create a new table or object PRIMARY KEY, Name VARCHAR(50) );
ALTER – ALTER TABLE Student ADD Email
Add a new column to existing table VARCHAR(100);
ADD
ALTER – Change datatype or size of existing ALTER TABLE Student MODIFY Name
MODIFY column VARCHAR(100);
ALTER – ALTER TABLE Student DROP COLUMN
DROP Remove a column Email;
COLUMN
ALTER –
Rename a column name (MySQL ALTER TABLE Student RENAME COLUMN
RENAME Name TO FullName;
8+)
COLUMN
ALTER – ALTER TABLE Student RENAME TO
Rename entire table
RENAME Learner;
(PostgreSQL/Oracle style)
TABLE
ALTER –
ADD ALTER TABLE Student ADD PRIMARY
Add a primary key KEY (RollNo);
PRIMARY
KEY
ALTER –
DROP ALTER TABLE Student DROP PRIMARY
Remove the primary key KEY;
PRIMARY
KEY
ALTER – ALTER TABLE Student ADD
ADD Add foreign key referencing CONSTRAINT fk_dept FOREIGN KEY
FOREIGN another table (DeptID) REFERENCES
KEY Department(DeptID);
ALTER – Drop foreign key constraint ALTER TABLE Student DROP FOREIGN
DROP KEY fk_dept;
Command Purpose / Action Syntax Example
FOREIGN
KEY
ALTER – ALTER TABLE Student ADD
ADD Add a check constraint CONSTRAINT chk_age CHECK (Age >=
CHECK 18);
ALTER – ALTER TABLE Student DROP CHECK
Remove a check constraint (SQL
DROP chk_age;
Server)
CHECK
Delete entire table including DROP TABLE Student;
DROP
structure and data
Delete all rows from a table TRUNCATE TABLE Student;
TRUNCATE
(structure remains)
RENAME RENAME TABLE Student TO Learner;
Rename a table (MySQL style)
TABLE
📘 DDL Commands –
🔹 What is DDL (Data Definition Language)?
DDL is used to define and manage the structure of database objects like tables, views,
schemas, constraints, etc.
These commands affect the schema, not the actual data.
PART A: DDL COMMANDS – THEORY + SYNTAX + EXAMPLES
🔹 1. CREATE – To create new tables or objects
Syntax:
CREATE TABLE table_name (
column1 datatype [CONSTRAINTS],
column2 datatype [CONSTRAINTS],
...
);
Example:
CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
DeptID INT
);
🔹 2. ALTER – To modify existing table structure
a) Add a column
ALTER TABLE Student ADD Email VARCHAR(100);
b) Modify a column datatype
ALTER TABLE Student MODIFY Age VARCHAR(10);
c) Drop a column
ALTER TABLE Student DROP COLUMN Email;
d) Rename a column (MySQL 8+)
ALTER TABLE Student RENAME COLUMN Name TO FullName;
e) Rename table name
ALTER TABLE Student RENAME TO Learner;(PostgreSQL/Oracle)
f) Add a primary key
ALTER TABLE Student ADD PRIMARY KEY (RollNo);
g) Drop primary key
ALTER TABLE Student DROP PRIMARY KEY;
h) Add a foreign key
ALTER TABLE Student
ADD CONSTRAINT fk_dept
FOREIGN KEY (DeptID) REFERENCES Department(DeptID);
i) Drop foreign key
ALTER TABLE Student DROP FOREIGN KEY fk_dept;
j) Add a CHECK constraint
ALTER TABLE Student ADD CONSTRAINT chk_age CHECK (Age >= 18);
k) Drop CHECK constraint
ALTER TABLE Student DROP CHECK chk_age; -- SQL Server
🔹 3. DROP – To delete entire table (structure + data)
DROP TABLE Student;
● Cannot be recovered once dropped.
🔹 4. TRUNCATE – To delete all rows but keep the table
TRUNCATE TABLE Student;
● ⚠️Faster than DELETE. Cannot use WHERE.
🔹 5. RENAME – To rename a table (alternative to ALTER)
● RENAME TABLE Student TO Learner; -- MySQL
PART B: PRACTICE QUESTIONS FOR STUDENTS
Q1. Create a table called Teacher with:
● TID (int, primary key)
● TName (varchar 50)
● Subject (varchar 30)
Q2. Add a new column Phone (varchar 15) to Teacher.
Q3. Change Subject column datatype to VARCHAR(50).
Q4. Drop Phone column from Teacher table.
Q5. Add a CHECK constraint to ensure Subject is not null.
Q6. Rename TName to FullName.
Q7. Create a table Library with:
● BookID (int, primary key)
● Title (varchar 100)
● Author (varchar 50)
● Price (decimal)
Q8. Add a foreign key to Library: AuthorID referencing Author table.
Q9. Delete all records from Library table (structure must remain).
Q10. Delete the Library table completely.
PART C: QUICK SUMMARY TABLE
Comman
Use
d
CREATE Create new table or object
Modify table structure (columns, constraints,
ALTER
etc.)
DROP Delete the table completely
TRUNCAT
E Delete all rows, keep structure
RENAME Change table name