DDL statements define, modify or remove database objects – the structures that hold data. They affect the metadata stored in the system catalogue and are usually auto‑committed (most DBMS cannot roll back DDL).
| Statement | Purpose (why we need it) |
|---|---|
CREATE | Establishes a new object (database, table, view, index). Enables the logical design derived from normalization. |
ALTER | Changes an existing definition – e.g. adding a column to accommodate a new business rule. |
DROP | Removes an object permanently – used when a table is obsolete or when redesigning a schema. |
TRUNCATE | Deletes all rows quickly while preserving the table definition; useful for test data refreshes. |
RENAME | Changes the identifier of an object – helpful during refactoring. |
CHECK (Age >= 0)).
-- 1. Create a database (optional in many DBMS)
CREATE DATABASE UniversityDB;
-- 2. Create a table with integrity constraints
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
DOB DATE,
Programme VARCHAR(50) NOT NULL,
Email VARCHAR(120) UNIQUE,
CONSTRAINT chk_DOB CHECK (DOB <= CURRENT_DATE)
);
-- 3. Add a new column (ALTER)
ALTER TABLE Student
ADD Phone VARCHAR(15);
-- 4. Rename the table (RENAME)
RENAME TABLE Student TO Learner;
-- 5. Remove a column (ALTER)
ALTER TABLE Learner
DROP COLUMN Phone;
-- 6. Delete the table (DROP)
DROP TABLE Learner;
-- 7. Remove all rows but keep the definition (TRUNCATE)
TRUNCATE TABLE Student;
DML statements operate on the actual data stored in the structures created by DDL. Every DML command is executed inside a transaction, allowing the DBMS to guarantee the ACID properties (Atomicity, Consistency, Isolation, Durability).
SELECT – retrieves data (queries).INSERT – adds new rows.UPDATE – modifies existing rows.DELETE – removes rows.MERGE – “up‑sert”: inserts rows that do not exist and updates those that do.Explicit transaction boundaries give the programmer control over when changes become permanent.
BEGIN TRANSACTION;
INSERT INTO Student (StudentID, Name, DOB, Programme, Email)
VALUES (101, 'Alice Brown', '2002-04-15', 'Computer Science', 'alice@example.com');
UPDATE Student
SET Programme = 'Software Engineering'
WHERE StudentID = 101;
-- If any statement fails, undo everything
IF @@ERROR <> 0
ROLLBACK TRANSACTION;
ELSE
COMMIT TRANSACTION;
Note:
MERGE)
-- INSERT a new student
INSERT INTO Student (StudentID, Name, DOB, Programme, Email)
VALUES (102, 'Bob Carter', '2001-09-30', 'Mathematics', 'bob@example.com');
-- SELECT students enrolled in Computer Science
SELECT Name, Programme
FROM Student
WHERE Programme = 'Computer Science';
-- UPDATE a student's programme
UPDATE Student
SET Programme = 'Data Science'
WHERE StudentID = 102;
-- DELETE a student record
DELETE FROM Student
WHERE StudentID = 102;
-- MERGE (up‑sert) – useful for synchronising with external data sources
MERGE INTO Student AS tgt
USING (SELECT 103 AS StudentID, 'Clara Lee' AS Name,
'2000-12-05' AS DOB, 'Physics' AS Programme,
'clara@example.com' AS Email) AS src
ON tgt.StudentID = src.StudentID
WHEN MATCHED THEN
UPDATE SET Programme = src.Programme, Email = src.Email
WHEN NOT MATCHED THEN
INSERT (StudentID, Name, DOB, Programme, Email)
VALUES (src.StudentID, src.Name, src.DOB, src.Programme, src.Email);
Every DML command has a direct counterpart in relational algebra, which helps students understand the logical operations behind SQL.
SELECT Name, Programme FROM Student WHERE Programme='Computer Science'
↔ πName,Programme(σProgramme='Computer Science'(Student))
COMMIT makes them durable, ROLLBACK restores the previous state.| Aspect | DDL | DML |
|---|---|---|
| Primary purpose | Define, modify or remove the database schema. | Insert, retrieve, modify or delete the actual data. |
| Typical commands | CREATE, ALTER, DROP, TRUNCATE, RENAME. | SELECT, INSERT, UPDATE, DELETE, MERGE. |
| Effect on data | Indirect – changes the structures that hold data. | Direct – adds, changes, or removes rows. |
| Transaction control | Auto‑committed; most DBMS cannot roll back DDL. | Fully transactional – can be COMMIT‑ed or ROLLBACK‑ed. |
| DBMS processing | Updates system catalogue; may lock schema objects. | Parsed → Optimised → Executed → Logged → Integrity‑checked. |
ROLLBACK.TRUNCATE vs. DELETE in terms of speed, logging and recoverability. Discuss which is more appropriate for a production environment.BEGIN, COMMIT, ROLLBACK) ensures reliable data handling and supports the evaluation of reliability and security (AO3).Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.