Data Definition Language (DDL) and Data Manipulation Language (DML)

Computer Science AS · Databases. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science AS topics

The idea

SQL is the industry standard language for relational databases. It has two parts. The Data Definition Language (DDL) creates and changes the structure: databases, tables, fields and keys. The Data Manipulation Language (DML) works with the data inside the tables: queries, and adding, changing and deleting records.

A query is built in a fixed order: SELECT the fields, FROM the table, WHERE a condition is true, GROUP BY a field, ORDER BY a field. Text and dates go in quotes; numbers do not.

The functions SUM, COUNT and AVG give one value for a set of records. With GROUP BY they give one value for each group.

In simpler words

SQL is the language for databases. It has two parts. DDL builds the structure, like making tables and keys. DML works with the data: finding, adding, changing and deleting records. A search is written in a set order: SELECT, FROM, WHERE, GROUP BY, ORDER BY. Text goes in quotes, numbers do not. SUM, COUNT and AVG give one answer for a group.

An everyday comparison

DDL is like building a set of shelves with labelled sections; DML is like putting items on them, finding them, moving them and throwing them away. A WHERE clause is like saying 'only the red boxes'. The comparison stops working because forgetting WHERE in an UPDATE or DELETE changes every record at once, something no careless shelf tidy would ever do so completely or so quickly.

Rules to remember

Common mistake

Leaving out WHERE in UPDATE or DELETE changes or removes every record in the table. Also, SELECT is DML, not DDL, although it does not change any data.

In the exam

Write SQL in the fixed clause order: SELECT fields, FROM table, WHERE condition, GROUP BY, ORDER BY. Use the exact field and table names given. Put text and dates in quotes, numbers without. For a join, write INNER JOIN ... ON A.Key = B.Key. For CREATE TABLE, give each field a data type and define PRIMARY KEY and FOREIGN KEY ... REFERENCES. Always include WHERE in UPDATE and DELETE. Classify statements correctly: SELECT is DML.

Worked example

STOCK has five records with (Category, Qty): (Pen, 12), (Paper, 40), (Pen, 8), (Ink, 5), (Paper, 10). What does this query return?
SELECT Category, SUM(Qty) FROM STOCK GROUP BY Category;

  1. GROUP BY Category puts the records into three groups: Pen, Paper and Ink.
  2. SUM(Qty) is worked out for each group. Pen: 12 + 8 = 20. Paper: 40 + 10 = 50. Ink: 5.
  3. The query returns one row for each group, not one row for each record.

Answer: Three rows: Pen 20, Paper 50, Ink 5.

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← Database Management Systems (DBMS)Computational thinking skills →Take a Computer Science AS test