DDL and DML Commands
You have now written every command this chapter teaches. They fall into two groups, and the paper asks you which group a command belongs to. The answer is always the same question: does it change the shape of the table, or what is written inside it?
1Two jobs, two groups
Think of the attendance register in your class. Someone ruled the columns, wrote the headings and decided how many there are. Every day after that, a teacher writes names and marks in those columns, and sometimes rubs one out.
Those are two different jobs on the same register. SQL splits its commands the same way.
| Group | Full name | The job | Commands |
|---|---|---|---|
| DDL | Data Definition Language | Rules the columns — creates and changes the structure itself | create, alter, drop, truncate |
| DML | Data Manipulation Language | Writes in the columns — puts rows in, changes them, takes them out | insert, update, delete |
2The DDL commands, with examples
A school is starting a sports club and needs somewhere to keep the members. Nothing exists yet, so the first commands are all DDL.
Now the table. create table names the columns and their data types, and that list is the structure.
0 rows affected is worth reading. The command did its job perfectly and touched no rows at all, because there are none. That is what a DDL command looks like: it works on the table, not on the data.
3The DML commands, with examples
The register is ruled. Now people join the club, and every command from here is DML.
Pema changes her game. update changes values in rows that are already there.
Anjali leaves the club. delete takes her whole row out.
Three commands, three different effects on the rows — and the table has the same three columns it was created with. Nothing DML does can change that.
4The pair that gets confused: alter and update
Both of these change the table in ordinary English, which is why the question is worth asking. alter adds a column, so every row gets a new box. update writes in a box that is already there.
Blue, so it is tempting to call this DML. The status line says 0 rows affected: MySQL changed the structure and gave the new column a default, and no row was edited. alter is DDL, always — even alter table … add primary key, which is the same command doing the same kind of job.5Emptying a table and removing it
delete with no where takes out every row. It is still DML, and the proof is that the table is still standing afterwards.
drop is the DDL twin of that. It removes the structure too, and the proof is the error you get when you ask for the table afterwards.
truncate table t; also empties a table and keeps it, but it is grouped with DDL because of how it does it — it drops the table and builds it again. Deleting Rows sets all three out in one table, which is the form the question usually takes.6Answering “differentiate between DDL and DML”
This is a two-mark question, and two marks means two differences, each with an example. One sentence each is enough.
Defines or changes the structure of the database and its tables. Commands: create, alter, drop, truncate. Example: alter table club add house varchar(10);
Changes the data stored inside the tables. Commands: insert, update, delete. Example: update club set game = "Chess" where name = "Pema";
select reads data and changes nothing, so many books give it a group of its own, DQL — Data Query Language. Others count it as DML. Both answers are accepted; say what select does and you have the mark.7Recap
DDL. They build and change the table itself.
DML. They put rows in, change them and take them out.
The mark of a DDL command: it worked on the structure, not on any row.
delete empties the table and leaves it. drop removes the table as well.
Which of these is a DDL command?
A command runs and MySQL replies “Query OK, 0 rows affected”. What does that tell you?
Riya must empty the members table but keep its structure for next year. Which group of commands does she need?