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.
-
Utilize the PostgreSQL-exclusive
RETURNINGclause.
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 PostgreSQL.
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 and Booleans do not).*
*(Notice we completely ignored the emp_id column! Because we defined it as SERIAL, PostgreSQL will automatically generate ID #1 for John).*
Inserting Multiple Rows at Once:
You don't need to write the INSERT INTO command 50 times. You can chain values!
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, PostgreSQL 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. The PostgreSQL Superpower: RETURNING
In standard SQL, if you INSERT a row, the database just replies "Success." But what if your PHP backend immediately needs to know what auto-incremented ID was assigned to that new row?
PostgreSQL features the incredible RETURNING clause. It allows an INSERT, UPDATE, or DELETE statement to instantly hand data back to the application!
7. Mini Project: Manage Employee Records
Let's do a quick simulation of an employee lifecycle usingRETURNING.
8. Common Mistakes
-
String Quote Errors: Typing
VALUES (John, 55000)will fail. PostgreSQL will thinkJohnis the name of a column! Text must be wrapped in single quotes:VALUES ('John', 55000).
-
Double Quotes: In PostgreSQL, single quotes
' 'are for Strings/Text. Double quotes" "are strictly for forcing case-sensitivity on Table/Column names. Never use double quotes for string values!
9. Best Practices
-
Never rely on Table Order: When writing
INSERT INTO employees VALUES ('John', 55000);(without specifying the columns in parentheses), PostgreSQL assumes you are inserting data in the exact order the table was created. If someone adds a column to the table later, your query will crash. Always explicitly declare the columns:INSERT INTO employees (name, salary) VALUES...
10. Exercises
-
1.
Write the SQL command to update the
ismanagerstatus of all employees who make exactly $120,000 toTRUE.
-
2.
What does the
RETURNING *clause do when appended to aDELETEstatement?
11. SQL Challenges
Write a single query that deletes all employees who do not have an email address (where the email is NULL), and returns thefirstname of the people who were deleted.
12. 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?
What is the primary benefit of appending RETURNING id to an INSERT statement in PostgreSQL?
13. Interview Questions
-
Q: Explain the difference between single quotes (
') and double quotes (") in PostgreSQL syntax.
-
Q: Describe a backend web development scenario where the PostgreSQL
RETURNINGclause drastically improves application efficiency.
14. FAQs
Q: Can I undo anUPDATE command if I messed up and forgot the WHERE clause?
A: By default, no. The data is destroyed. However, if you wrap your commands in a "Transaction" (which we cover in Chapter 20), you can type ROLLBACK; to time-travel and undo the mistakes!
15. 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. By utilizing RETURNING, you bridge the gap between database mutations and backend application logic. Just remember: with great power comes the massive responsibility of the WHERE clause.