June 2010
Intermediate to advanced
328 pages
7h 51m
English
There’s a better solution to restrict values in a column: create a lookup table with one row for each value you allow in the Bugs.status column. Then declare a foreign key constraint on Bugs.status referencing the new table.
| 31-Flavors/soln/create-lookup-table.sql | |
| | CREATE TABLE BugStatus ( |
| | status VARCHAR(20) PRIMARY KEY |
| | ); |
| | |
| | INSERT INTO BugStatus (status) VALUES ('NEW'), ('IN PROGRESS'), ('FIXED'); |
| | |
| | CREATE TABLE Bugs ( |
| | -- other columns |
| | status VARCHAR(20), |
| | FOREIGN KEY (status) REFERENCES BugStatus(status) |
| | ON UPDATE CASCADE |
| | ); |
When you insert or update a row in the Bugs table, you must use a status value that exists in the BugStatus table. This enforces the status ...
Read now
Unlock full access