SQL and MySQL course Β· Module 5: Modifying data (DML)
The scribe's safe habits
In this lesson4
You now hold all three great commands. Before you move on, let us cement the discipline that ensures you never destroy the Library through carelessness.
The golden rule: WHERE first
I will repeat it one last time, for it is the most important sentence of this location: UPDATE and DELETE without WHERE act on the whole shelf. Build the habit of writing the WHERE condition before you write the rest of the command.
Check before you run
An experienced scribe never shoots in the dark. First they check which rows will be affected, by turning the UPDATE/DELETE into a SELECT with the same WHERE:
SELECT * FROM ksiazki WHERE dostepna = 0;If this SELECT returns exactly the scrolls you want to remove - only then do you run:
DELETE FROM ksiazki WHERE dostepna = 0;Three steps of safe editing
- Write the
WHEREcondition - define exactly which rows you want to change. - Check it with a
SELECT- see how many and which rows match. - Run the
UPDATEorDELETEwith the sameWHERE.
Small steps, great care
Modify data in small portions. It is better to run several precise commands than one big one that covers too much. And if an operation is truly serious - a good scribe first makes a copy of the important scrolls. Remember: in the Library there is no trash bin and no undo.
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which habit lets you safely check which rows will be changed BEFORE you run an UPDATE or DELETE?
Hands-on tasks in the game
- Horizontal ordering
Arrange on one line the preview of rows before deletion (left to right):
- Click in order
Click the steps of safe data editing in the correct order: