A3.3.1 SQL's sub-languages: DDL, DML and more
SL + HLCommand term: Outline1. Watch a database change
Press Next to run SQL statements one at a time. Two boxes show the database: its structure (what DDL changes) and its data (what DML changes). The box that changes lights up.
2. SQL sandbox: run the statements yourself
Click a statement to run it on the Student database. Try them in the wrong order to see the error messages.
3. Flip-card deck
Click to flip. Say the answer before you flip.
4. Pick the statement
Table: Student(StudentID, Name, Grade). Choose the statement that does the job. Streaks of 3+ score double.
5. 45-second speed round: DDL or DML?
A command or a job appears. Which type of SQL is it?
Learning objectives
By the end of this page you should be able to:
- Outline the purpose of data definition language (DDL).
- Outline the purpose of data manipulation language (DML).
- Identify whether a given SQL statement is DDL or DML.
- Outline the statements CREATE, ALTER and DROP, and SELECT, INSERT, UPDATE and DELETE.
- Distinguish between commands that are often confused, such as DROP and DELETE, or ALTER and UPDATE.
SQL: one language, different jobs
SQL (Structured Query Language) is the standard language for working with relational databases. Its statements fall into groups by purpose. At this level you must know two.
| DDL: data definition language | DML: data manipulation language | |
|---|---|---|
| Purpose | Define and change the structure (schema) of the database | Add, change, remove and retrieve the data stored in the tables |
| What it works on | Tables, columns, data types, keys, constraints | Records (rows) and their values |
| Main statements | CREATE TABLE, ALTER TABLE, DROP TABLE | INSERT INTO, UPDATE ... SET, DELETE FROM, SELECT |
| Example | ALTER TABLE Student ADD Email VARCHAR(50); | UPDATE Student SET Grade = 7 WHERE StudentID = 2; |
| Effect | Changes what the database is: the table layout. Often takes effect at once. | Changes what the database holds: the records. Table layout stays the same. |
| Used by | Database designers and administrators | Programmers, applications and users |
Memory aid: Data Definition = Design the tables. Data Manipulation = Move the data around.
The statements
- CREATE TABLE builds a new table: its columns, data types and keys.
- ALTER TABLE changes an existing table, e.g.
ADDorDROP COLUMN. - DROP TABLE deletes the whole table: structure and data.
- INSERT INTO ... VALUES adds a new record.
- UPDATE ... SET ... WHERE changes values in existing records. Without
WHEREit changes every row. - DELETE FROM ... WHERE removes records but keeps the table. Without
WHEREit removes all rows. - SELECT retrieves data without changing it. In this course it is treated as DML (some textbooks call queries DQL).
Common confusions
- DELETE vs DROP:
DELETE FROM Student;empties the table (DML).DROP TABLE Student;removes the table itself (DDL). - ALTER vs UPDATE: ALTER changes the columns (DDL). UPDATE changes values in rows (DML).
- Order matters: CREATE the table before you INSERT into it, and ALTER in a column before you store values in it.
Other types exist, for example control of permissions (DCL) and transactions (TCL, covered at HL in A3.3.6). The syllabus focus here is DDL and DML. Queries across tables with joins come in A3.3.2.
Use the 中文 button at the top to show or hide the Chinese terms.
| Term | 中文 | Meaning |
|---|
Choose an answer. You get instant feedback.
Score: 0 /
Write your answer first, then reveal the markscheme. These are exam-style practice questions written for this site.
1. Outline the purpose of data definition language (DDL). [2]
2. Outline the purpose of data manipulation language (DML). [2]
3. State whether each SQL statement is DDL or DML: (a) CREATE TABLE, (b) UPDATE, (c) DROP TABLE, (d) INSERT INTO. [4]
4. Outline two differences between DDL and DML. [4]
5. A school uses a table Student(StudentID, Name, Grade). Write SQL statements to (a) add a new column Email of type VARCHAR(50) to the table [2], (b) change the grade of the student named Ben to 7. [3]
6. A teacher runs DELETE FROM Student; and a colleague runs DROP TABLE Student;. Explain the difference in effect. [3]