INSERT, UPDATE and DELETE safely
With the reviews table created, it is time to fill it and learn to modify it. The three data manipulation statements are INSERT (add rows), UPDATE (change existing rows) and DELETE (remove rows). Unlike SELECT, these statements really change the data, and a slip can be very costly: an UPDATE without WHERE modifies all the rows in the table, and a DELETE without WHERE removes them all. This lesson teaches the syntax and, above all, the habits to avoid mistakes.
INSERT
INSERT INTO table (columns) VALUES (values) adds a row. Always write the column list: that way the code does not break if someone adds a column to the table, and it is clear which value goes where. The columns you omit get their DEFAULT value, or NULL if they have none; identity columns fill themselves. You can insert several rows in one statement, which is much faster than many separate statements. The RETURNING clause (PostgreSQL and SQLite 3.35 onward) returns the generated values, for example the new review_id.
-- Six reviews that we will use in the following weeks
INSERT INTO reviews (product_id, customer_id, rating, comment) VALUES
(1, 1, 5, 'Fast and light'),
(1, 4, 4, 'Good, but the fan is loud'),
(2, 3, 4, 'Works fine'),
(5, 4, 5, NULL),
(3, 1, 3, 'Keys are noisy'),
(7, 3, 2, 'Broke after a month');
-- A temporary extra row; RETURNING shows the id that the database generated
INSERT INTO reviews (product_id, customer_id, rating)
VALUES (9, 2, 1)
RETURNING review_id, created_at;
SELECT * FROM reviews ORDER BY review_id;UPDATE and DELETE: the safe protocol
UPDATE table SET column = value WHERE condition changes only the rows meeting the condition; DELETE FROM table WHERE condition removes only those rows. To avoid mistakes, always follow this protocol. One: first write a SELECT with the same WHERE and check that it returns exactly the rows you want to touch. Two: open a transaction with BEGIN, which lets you undo. Three: run the UPDATE or DELETE and look at how many rows it says it affected (UPDATE 2, DELETE 1). Four: if the number is the expected one, confirm with COMMIT; if not, ROLLBACK undoes everything as if nothing had happened.
-- 1) Look first
SELECT review_id, product_id, rating FROM reviews WHERE rating = 1;
-- 2) Open a transaction, 3) change, 4) decide
BEGIN;
UPDATE reviews SET rating = 2, comment = 'Edited by support' WHERE rating = 1;
-- psql answers: UPDATE 1
SELECT review_id, rating, comment FROM reviews WHERE review_id = 7;
COMMIT; -- or ROLLBACK; if something looks wrong
-- Remove the temporary row (same protocol, a precise WHERE on the primary key)
DELETE FROM reviews WHERE review_id = 7;Notice that the WHERE uses the primary key when possible: that way you are sure to touch a single row. An UPDATE can modify several columns separated by commas and can use expressions, such as SET price = price * 1.10. If you want to practice without risk on the shop's data, work on a copy: CREATE TABLE products_copy AS SELECT * FROM products creates a new table with the current content, and you can wreck it freely.
CREATE TABLE products_copy AS SELECT * FROM products;
-- 10 % price rise for stationery only
UPDATE products_copy
SET price = ROUND(price * 1.10, 2)
WHERE category = 'Stationery';
-- Upsert: insert a new product, or update it if the id already exists (PostgreSQL / SQLite)
INSERT INTO products_copy (product_id, name, category, price, stock)
VALUES (11, 'USB Cable', 'Accessories', 7.50, 100)
ON CONFLICT (product_id) DO UPDATE SET stock = EXCLUDED.stock;
DELETE FROM products_copy WHERE stock = 0;
SELECT * FROM products_copy ORDER BY product_id;✎ マイノート
このブラウザにのみ保存されます。ログインするとアカウントに保存できます。0/5000 文字