CREATE TABLE, ALTER TABLE, DROP TABLE,
CREATE INDEX, DROP INDEX.
DDL statements define the structure of the database – the tables, columns, data types and constraints. Once the schema is in place, DML statements (INSERT, UPDATE, DELETE, SELECT) manipulate the actual data.
| Aspect | DDL (Data Definition Language) | DML (Data Manipulation Language) |
|---|---|---|
| Purpose | Define, modify or remove database objects (tables, indexes, constraints). | Insert, update, delete or retrieve rows of data. |
| Typical Commands | CREATE, ALTER, DROP, RENAME (limited to sub‑set for this course). | INSERT, UPDATE, DELETE, SELECT. |
| When Executed | Rarely – usually during design or major schema changes. | Often – during normal program operation. |
| Effect on Data | May delete data (e.g., DROP TABLE) or lock the schema for a short time. | Never changes the schema, only the stored rows. |
Defines a new table, its columns, data types and any column‑level constraints.
CREATE TABLE table_name (
column1 datatype [constraint],
column2 datatype [constraint],
…
[table_constraint]
);
| Datatype | Typical Use |
|---|---|
INT | Whole numbers (e.g., IDs, counts) |
DECIMAL(p,s) | Fixed‑point numbers; p total digits, s after the decimal point |
VARCHAR(n) | Variable‑length character strings up to n characters |
CHAR(n) | Fixed‑length strings (always n characters) |
DATE | Calendar dates (YYYY‑MM‑DD) |
TIME | Times of day (HH:MM:SS) |
BOOLEAN | Logical values TRUE / FALSE |
PRIMARY KEY – unique, not null.NOT NULL – column must contain a value.UNIQUE – all values must be different.DEFAULT value – value used when none is supplied.CHECK (condition) – e.g., CHECK (Credits BETWEEN 1 AND 6).PRIMARY KEY (col1, col2) – composite key.FOREIGN KEY (col) REFERENCES OtherTable(PK).Modifies an existing table – add, drop or change columns and constraints.
-- Add a new column
ALTER TABLE tablename ADD columnname datatype [constraint];
-- Drop an existing column
ALTER TABLE tablename DROP COLUMN columnname;
-- Add a table‑level constraint (e.g., foreign key)
ALTER TABLE tablename ADD CONSTRAINT fkname
FOREIGN KEY (col) REFERENCES OtherTable(PK);
Deletes a table and all its data permanently.
DROP TABLE table_name;
Creates a secondary structure to speed up searches on one or more columns.
CREATE INDEX indexname ON tablename(column1 [, column2 …]);
Removes an index that is no longer needed.
DROP INDEX index_name;
Student tableCREATE TABLE Student (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
DOB DATE,
EnrolDate DATE DEFAULT CURRENT_DATE,
Email VARCHAR(50) UNIQUE
);
StudentID is the primary key – automatically NOT NULL and unique.Email has a UNIQUE constraint to prevent duplicates.EnrolDate gets the current date if no value is supplied.Course tableCREATE TABLE Course (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100) NOT NULL,
Credits INT DEFAULT 3 CHECK (Credits BETWEEN 1 AND 6)
);
Enrollment (many‑to‑many)CREATE TABLE Enrollment (
StudentID INT,
CourseID INT,
EnrolDate DATE DEFAULT CURRENT_DATE,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
CourseALTER TABLE Course ADD RoomNumber VARCHAR(10);
CREATE INDEX idxcoursename ON Course(CourseName);
DROP INDEX idxcoursename;
DROP TABLE TempResults;
DATE (or DATETIME if time is needed). Storing dates as VARCHAR prevents date arithmetic.CREATE TABLE: The parser will raise an error – double‑check spelling.DROP TABLE … CASCADE only when you are sure you want to lose dependent data.SupplierColumns: SupplierID (INT, PK), Name (VARCHAR 50, NOT NULL), Phone (VARCHAR 15, may be NULL), Country (VARCHAR 30, default ‘UK’).
Supplier with a Website column (VARCHAR 100) that must be unique.idxsuppliercountry on the Country column.TmpStock is no longer needed – write the statement to remove it.-- Exercise 1
CREATE TABLE Supplier (
SupplierID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Phone VARCHAR(15),
Country VARCHAR(30) DEFAULT 'UK'
);
-- Exercise 2
ALTER TABLE Supplier ADD Website VARCHAR(100) UNIQUE;
-- Exercise 3
CREATE INDEX idxsuppliercountry ON Supplier(Country);
-- Exercise 4
DROP TABLE TmpStock;
| Command | Typical Use | Key Syntax Elements |
|---|---|---|
CREATE TABLE | Define a new table. | CREATE TABLE name (col datatype [constraint], …, [table_constraint]); |
ALTER TABLE … ADD | Add a column or constraint. | ALTER TABLE name ADD column datatype [constraint]; |
ALTER TABLE … DROP COLUMN | Remove an existing column. | ALTER TABLE name DROP COLUMN column; |
DROP TABLE | Delete a table and its data. | DROP TABLE name; |
CREATE INDEX | Speed up searches on one or more columns. | CREATE INDEX idx_name ON table(col1 [, col2 …]); |
DROP INDEX | Remove an index. | DROP INDEX idx_name; |
CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX and DROP INDEX enables you to model real‑world entities accurately.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.