Constraints, ALTER TABLE and week 6 review
A well-designed database does not trust applications to always send correct data: it defends itself. Constraints are rules that the database enforces on every INSERT and UPDATE. If some data violates them, the operation is rejected with an error. Putting the rules in the database, and not only in the program that uses it, is the most reliable: no matter which application, script or person writes the data, the rules always apply.
- NOT NULL: the column cannot be left empty.
- UNIQUE: repeated values are not allowed (several NULLs are, in PostgreSQL).
- PRIMARY KEY: UNIQUE plus NOT NULL; it identifies the row.
- CHECK (condition): the value must meet a condition, for example rating between 1 and 5.
- FOREIGN KEY ... REFERENCES: the value must exist in another table.
- DEFAULT value: default value (not a strict constraint, but it completes the design).
ALTER TABLE: changing an existing table
Tables are rarely born perfect. ALTER TABLE lets you add or drop columns, rename them and add or drop constraints without losing the data. When adding a constraint, PostgreSQL first checks that the current data meets it; if any row violates it, the statement fails and that data must be fixed first. Adding a NOT NULL column to a table with rows requires giving a DEFAULT, because the existing rows need some value.
-- Complete the reviews table that we created in lesson 1
ALTER TABLE reviews
ADD CONSTRAINT reviews_product_fk FOREIGN KEY (product_id) REFERENCES products (product_id),
ADD CONSTRAINT reviews_customer_fk FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
ADD CONSTRAINT reviews_rating_ck CHECK (rating BETWEEN 1 AND 5),
ADD CONSTRAINT reviews_one_per_customer UNIQUE (product_id, customer_id);
-- Other common changes
ALTER TABLE reviews ADD COLUMN verified BOOLEAN NOT NULL DEFAULT false;
-- ALTER TABLE reviews RENAME COLUMN comment TO review_text; (we keep the original name)
ALTER TABLE reviews DROP COLUMN verified;Naming constraints (CONSTRAINT name) is a good practice: error messages show it and it is easy to drop them later with ALTER TABLE ... DROP CONSTRAINT name. The UNIQUE (product_id, customer_id) constraint is composite: it does not prevent a customer from reviewing several products, only from reviewing the same one twice. Now try to violate the rules and read the errors; learning to interpret them is part of the craft.
-- Each of these must FAIL (try them one by one):
INSERT INTO reviews (product_id, customer_id, rating) VALUES (2, 5, 9);
-- ERROR: new row for relation "reviews" violates check constraint "reviews_rating_ck"
INSERT INTO reviews (product_id, customer_id, rating) VALUES (99, 5, 4);
-- ERROR: ... violates foreign key constraint "reviews_product_fk"
INSERT INTO reviews (product_id, customer_id, rating) VALUES (1, 1, 4);
-- ERROR: duplicate key value violates unique constraint "reviews_one_per_customer"What happens on delete: ON DELETE
By default, you cannot delete a row that another table references. You can change this behavior when declaring the foreign key: ON DELETE CASCADE also deletes the dependent rows (if you delete an order, its lines are deleted), ON DELETE SET NULL puts NULL in the reference and ON DELETE RESTRICT (or NO ACTION) prevents it, which is the default behavior. CASCADE is convenient but dangerous: a single DELETE can drag many rows along, so use it only when the dependent data makes no sense without the main one. For customers and orders, which are accounting history, the prudent choice is not to cascade.
检验所学
第 1 / 6Which group of statements changes the database structure?
✎ 我的笔记
仅保存在此浏览器中。登录后可保存到账号。0/5000 字符