INSERT, UPDATE, and DELETE Queries
# CHAPTER 6
INSERT, UPDATE, and DELETE Queries
1. Introduction
You have successfully built an emptyemployees table. But an empty table is useless. A database's primary job is to manage the flow of living data. We call this Data Manipulation Language (DML). In the software industry, this is often referred to as "CUD" (Create, Update, Delete). In this chapter, we will write the essential SQL commands that power every single registration form, profile editor, and deletion button on the internet.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use
INSERT INTOto add new single or multiple rows.
-
Use
UPDATEto modify existing data.
-
Understand the critical importance of the
WHEREclause.
-
Use
DELETEto safely erase rows.
- Avoid the catastrophic "Global Update/Delete" mistake.
3. Inserting Data (INSERT INTO)
When a user fills out a "Sign Up" form on a website, the backend server converts their inputs into an INSERT INTO statement and sends it to MySQL.
Syntax:
Let's insert our first employee into the table we built in Chapter 5.
*(Note: Strings and Dates must be wrapped in single quotes ''. Numbers do not).*
Inserting Multiple Rows at Once:
You don't need to write the INSERT INTO command 50 times. You can chain values!
*(Notice we didn't provide a hire_date for these three? If a column allows it, MySQL will simply insert a blank NULL value in that spot).*
4. Updating Data (UPDATE)
Employees get raises. People change their email addresses. We use the UPDATE command to modify existing rows.
The Golden Rule: You almost *always* must use a WHERE clause to target a specific row. If you forget the WHERE clause, MySQL will update EVERY SINGLE ROW in the entire table!
5. Deleting Data (DELETE)
When an employee quits, we must remove their record from the active database.
The Golden Rule applies here too: If you forget the WHERE clause, you will wipe out the entire table in a millisecond!
6. Mini Project: Manage Employee Records
Let's do a quick simulation of an employee lifecycle.7. The Dangers of DML Operations
Unlike a Word Document, there is noCtrl + Z (Undo) button in standard SQL execution.
If you type:
DELETE FROM users; (and accidentally hit Enter before typing the WHERE id = 5 part)
MySQL will instantly execute the command and delete all 10 million users from your database. The company is destroyed.
8. Common Mistakes
-
String Quote Errors: Typing
VALUES (1, John, 55000)will fail becauseJohnis text and must be wrapped in quotes:VALUES (1, 'John', 55000).
-
Date Format: MySQL strictly requires dates to be inserted as
'YYYY-MM-DD'. If you insert'12-25-2024'or'12/25/24', the insertion will fail.
9. Best Practices
-
Safe Updates: In production environments,
phpMyAdminand modern MySQL interfaces often have "Safe Updates" turned on by default. This feature literally blocks anyUPDATEorDELETEcommand that does not include aWHEREclause targeting a Primary Key, saving you from catastrophic mistakes.
10. Exercises
-
1.
Write the SQL command to update the
departmentof all employees currently in 'Sales' to a new department named 'Marketing'.
-
2.
What happens if you execute the command
UPDATE employees SET salary = 100000;?
11. MCQ Quiz with Answers
Which SQL clause is absolutely critical when executing an UPDATE or DELETE command to prevent modifying the entire table at once?
When writing an INSERT INTO statement, how must text (VARCHAR) and date (DATE) values be formatted in the VALUES section?
12. Interview Questions
-
Q: Explain the difference between
DELETE FROM tablename;andDROP TABLE tablename;.
-
Q: Describe a scenario where an application might need to execute a bulk
INSERTstatement containing multiple rows, rather than executing 100 separateINSERTstatements.
13. FAQs
Q: Can I undo anUPDATE command if I messed up?
A: By default, no. However, Enterprise databases use "Transactions" (which we cover in Chapter 20). If you wrap your commands in a Transaction, you can type ROLLBACK; to undo the mistakes!
14. Summary
You are now capable of altering the reality of a database. By masteringINSERT, UPDATE, and DELETE, you can populate empty tables with millions of rows, modify their attributes, and prune outdated records. Just remember: with great power comes the massive responsibility of the WHERE clause.
15. Next Chapter Recommendation
Our database is now full of employee data. How do we get it back out? How do we find all employees making over $80,000? In Chapter 7: SELECT Queries and Filtering Data, we will learn the most commonly used command in the entire SQL language: theSELECT statement.