File: tutorial-fk.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (67 lines) | stat: -rw-r--r-- 3,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>3.3.Foreign Keys</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="tutorial-advanced.html" title="Chapter3.Advanced Features">
<link rel="prev" href="tutorial-views.html" title="3.2.Views">
<link rel="next" href="tutorial-transactions.html" title="3.4.Transactions">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="tutorial-fk"></a>3.3.Foreign Keys</h2></div></div></div>
<a name="id569748"></a><a name="id569759"></a><p>    Recall the <code class="classname">weather</code> and
    <code class="classname">cities</code> tables from <a href="tutorial-sql.html" title="Chapter2.The SQL Language">Chapter2, <i>The <acronym class="acronym">SQL</acronym> Language</i></a>.  Consider the following problem:  You
    want to make sure that no one can insert rows in the
    <code class="classname">weather</code> table that do not have a matching
    entry in the <code class="classname">cities</code> table.  This is called
    maintaining the <em class="firstterm">referential integrity</em> of
    your data.  In simplistic database systems this would be
    implemented (if at all) by first looking at the
    <code class="classname">cities</code> table to check if a matching record
    exists, and then inserting or rejecting the new
    <code class="classname">weather</code> records.  This approach has a
    number of problems and is very inconvenient, so
    <span class="productname">PostgreSQL</span> can do this for you.
   </p>
<p>    The new declaration of the tables would look like this:

</p>
<pre class="programlisting">CREATE TABLE cities (
        city     varchar(80) primary key,
        location point
);

CREATE TABLE weather (
        city      varchar(80) references cities(city),
        temp_lo   int,
        temp_hi   int,
        prcp      real,
        date      date
);</pre>
<p>

    Now try inserting an invalid record:

</p>
<pre class="programlisting">INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');</pre>
<p>

</p>
<pre class="screen">ERROR:  insert or update on table "weather" violates foreign key constraint "weather_city_fkey"
DETAIL:  Key (city)=(Berkeley) is not present in table "cities".</pre>
<p>
   </p>
<p>    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 to <a href="ddl.html" title="Chapter5.Data Definition">Chapter5, <i>Data Definition</i></a>
    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.
   </p>
</div></body>
</html>