What Is SQL DML?
INSERT, UPDATE, and DELETE change the data itself. The safest habit is to SELECT first.
Everything so far has been read-only: a
SELECT
looks at data but never changes it. This chapter introduces
DML (Data Manipulation Language): the statements that
do change it:
INSERT,
UPDATE, and
DELETE.
The workshop rule is measure twice, cut once. Before you
change rows, run a
SELECT
with the same filter so you can see exactly which rows you're about to
affect. Everything you change here happens in a private, in-browser copy, so
experiment freely.
-- always preview before you change
select * from orders where status = 'pending';
-- ...then the UPDATE targets exactly those rows
update orders set status = 'shipped' where status = 'pending'; DML, DDL, and DQL
SQL statements are often sorted into three groups, by what they change:
- DML changes the rows in a table:
INSERT,UPDATE, andDELETE. The table itself stays the same shape. - DDL (Data Definition Language) changes the tables
themselves:
CREATE TABLE,ALTER TABLE, andDROP TABLE. Lessons 8.8 to 8.10 cover them, starting with Lesson 8.8 · Creating a table. - DQL (Data Query Language) is the name some courses give
SELECT, which reads and changes nothing. Others count it as DML.
The pair that trips people up is
DELETE
and
DROP TABLE.
delete from orders
is DML: it removes every row and leaves an empty
orders
table behind.
drop table orders
is DDL: the table is gone, and every query that reads it now fails.
Schema · Garden Shop Table · orders 6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
Preview the rows a future change would touch: find every
order whose status is 'pending'. Return
order_id, customer_id, and status,
sorted by order_id.
Run your query to inspect the result.
Expected answer
- Columns: order_id, customer_id, status.
- Rows: 3 pending orders.
- This preview is the set of rows a later UPDATE would touch.
Related
A checklist for changing the right rows.
Add rows and confirm they landed.
Change existing rows safely.
Preview rows before deleting them.
Wrap risky changes so you can undo them.