Skip to main content
MySQL Basics
CHAPTER 06 Beginner

INSERT, UPDATE, and DELETE Queries

Updated: May 16, 2026
6 min read

# CHAPTER 6

INSERT, UPDATE, and DELETE Queries

1. Introduction

You have successfully built an empty employees 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 INTO to add new single or multiple rows.
  • Use UPDATE to modify existing data.
  • Understand the critical importance of the WHERE clause.
  • Use DELETE to 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:

sql
12
INSERT INTO table_name (column1, column2, column3) 
VALUES (value1, value2, value3);

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).*

sql
12
INSERT INTO employees (emp_id, first_name, email, salary, hire_date, department) 
VALUES (1, 'John', 'john@company.com', 55000.00, '2024-01-15', 'Sales');

Inserting Multiple Rows at Once: You don't need to write the INSERT INTO command 50 times. You can chain values!

sql
12345
INSERT INTO employees (emp_id, first_name, email, salary, department) 
VALUES 
(2, 'Sarah', 'sarah@company.com', 85000.00, 'IT'),
(3, 'Mike', 'mike@company.com', 45000.00, 'HR'),
(4, 'Anna', 'anna@company.com', 120000.00, 'Exec');

*(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!

sql
123456789
-- Let's give Sarah (emp_id 2) a massive raise!
UPDATE employees 
SET salary = 95000.00 
WHERE emp_id = 2;

-- We can update multiple columns at once
UPDATE employees 
SET salary = 50000.00, department = 'Sales' 
WHERE emp_id = 3;

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!

sql
1234567
-- Safely delete Mike from the company
DELETE FROM employees 
WHERE emp_id = 3;

-- Delete all employees in the Sales department
DELETE FROM employees 
WHERE department = 'Sales';

6. Mini Project: Manage Employee Records

Let's do a quick simulation of an employee lifecycle.
sql
123456789101112
-- 1. Hire a new employee
INSERT INTO employees (emp_id, first_name, department) 
VALUES (5, 'David', 'IT');

-- 2. Oops, we forgot his salary. Update his record.
UPDATE employees 
SET salary = 70000.00 
WHERE emp_id = 5;

-- 3. David was fired on day one. Remove him.
DELETE FROM employees 
WHERE emp_id = 5;

7. The Dangers of DML Operations

Unlike a Word Document, there is no Ctrl + 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 because John is 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, phpMyAdmin and modern MySQL interfaces often have "Safe Updates" turned on by default. This feature literally blocks any UPDATE or DELETE command that does not include a WHERE clause targeting a Primary Key, saving you from catastrophic mistakes.

10. Exercises

  1. 1. Write the SQL command to update the department of all employees currently in 'Sales' to a new department named 'Marketing'.
  1. 2. What happens if you execute the command UPDATE employees SET salary = 100000;?

11. MCQ Quiz with Answers

Question 1

Which SQL clause is absolutely critical when executing an UPDATE or DELETE command to prevent modifying the entire table at once?

Question 2

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; and DROP TABLE tablename;.
  • Q: Describe a scenario where an application might need to execute a bulk INSERT statement containing multiple rows, rather than executing 100 separate INSERT statements.

13. FAQs

Q: Can I undo an UPDATE 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 mastering INSERT, 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: the SELECT statement.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·