1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>3.3. Foreign Keys</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="tutorial-views.html" title="3.2. Views" /><link rel="next" href="tutorial-transactions.html" title="3.4. Transactions" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">3.3. Foreign Keys</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-views.html" title="3.2. Views">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-advanced.html" title="Chapter 3. Advanced Features">Up</a></td><th width="60%" align="center">Chapter 3. Advanced Features</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="tutorial-transactions.html" title="3.4. Transactions">Next</a></td></tr></table><hr /></div><div class="sect1" id="TUTORIAL-FK"><div class="titlepage"><div><div><h2 class="title" style="clear: both">3.3. Foreign Keys <a href="#TUTORIAL-FK" class="id_link">#</a></h2></div></div></div><a id="id-1.4.5.4.2" class="indexterm"></a><a id="id-1.4.5.4.3" class="indexterm"></a><p>
3 Recall the <code class="classname">weather</code> and
4 <code class="classname">cities</code> tables from <a class="xref" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Chapter 2</a>. Consider the following problem: You
5 want to make sure that no one can insert rows in the
6 <code class="classname">weather</code> table that do not have a matching
7 entry in the <code class="classname">cities</code> table. This is called
8 maintaining the <em class="firstterm">referential integrity</em> of
9 your data. In simplistic database systems this would be
10 implemented (if at all) by first looking at the
11 <code class="classname">cities</code> table to check if a matching record
12 exists, and then inserting or rejecting the new
13 <code class="classname">weather</code> records. This approach has a
14 number of problems and is very inconvenient, so
15 <span class="productname">PostgreSQL</span> can do this for you.
17 The new declaration of the tables would look like this:
19 </p><pre class="programlisting">
21 name varchar(80) primary key,
25 CREATE TABLE weather (
26 city varchar(80) references cities(name),
34 Now try inserting an invalid record:
36 </p><pre class="programlisting">
37 INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
40 </p><pre class="screen">
41 ERROR: insert or update on table "weather" violates foreign key constraint "weather_city_fkey"
42 DETAIL: Key (city)=(Berkeley) is not present in table "cities".
45 The behavior of foreign keys can be finely tuned to your
46 application. We will not go beyond this simple example in this
47 tutorial, but just refer you to <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a>
48 for more information. Making correct use of
49 foreign keys will definitely improve the quality of your database
50 applications, so you are strongly encouraged to learn about them.
51 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-views.html" title="3.2. Views">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-advanced.html" title="Chapter 3. Advanced Features">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-transactions.html" title="3.4. Transactions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.2. Views </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 3.4. Transactions</td></tr></table></div></body></html>