gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
4.3KB

  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 V1.79.1" /><link rel="prev" href="tutorial-views.html" title="3.2. Views" /><link rel="next" href="tutorial-transactions.html" title="3.4. Transactions" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 12.4 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></hr></div><div class="sect1" id="TUTORIAL-FK"><div class="titlepage"><div><div><h2 class="title" style="clear: both">3.3. Foreign Keys</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.
  16. </p><p>
  17. The new declaration of the tables would look like this:
  18. </p><pre class="programlisting">
  19. CREATE TABLE cities (
  20. city varchar(80) primary key,
  21. location point
  22. );
  23. CREATE TABLE weather (
  24. city varchar(80) references cities(city),
  25. temp_lo int,
  26. temp_hi int,
  27. prcp real,
  28. date date
  29. );
  30. </pre><p>
  31. Now try inserting an invalid record:
  32. </p><pre class="programlisting">
  33. INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
  34. </pre><p>
  35. </p><pre class="screen">
  36. ERROR: insert or update on table "weather" violates foreign key constraint "weather_city_fkey"
  37. DETAIL: Key (city)=(Berkeley) is not present in table "cities".
  38. </pre><p>
  39. </p><p>
  40. The behavior of foreign keys can be finely tuned to your
  41. application. We will not go beyond this simple example in this
  42. tutorial, but just refer you to <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a>
  43. for more information. Making correct use of
  44. foreign keys will definitely improve the quality of your database
  45. applications, so you are strongly encouraged to learn about them.
  46. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-advanced.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-transactions.html">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">Home</a></td><td width="40%" align="right" valign="top"> 3.4. Transactions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1