3.3. Foreign Keys
Recall theweather
andcities
tables fromChapter 2. Consider the following problem: You want to make sure that no one can insert rows in theweather
table that do not have a matching entry in thecities
table. This is called maintaining thereferential integrity of your data. In simplistic database systems this would be implemented (if at all) by first looking at thecities
table to check if a matching record exists, and then inserting or rejecting the newweather
records. This approach has a number of problems and is very inconvenient, soPostgres Pro can do this for you.
The new declaration of the tables would look like this:
CREATE TABLE cities ( name varchar(80) primary key, location point);CREATE TABLE weather ( city varchar(80) references cities(name), temp_lo int, temp_hi int, prcp real, date date);
Now try inserting an invalid record:
INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
ERROR: insert or update on table "weather" violates foreign key constraint "weather_city_fkey"DETAIL: Key (city)=(Berkeley) is not present in table "cities".
The behavior of foreign keys can be finely tuned to your application. We will not go beyond this simple example in this tutorial, but just refer you toChapter 5 for more information. Making correct use of foreign keys will definitely improve the quality of your database applications, so you are strongly encouraged to learn about them.