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.

523 lines
28KB

  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>5.4. Constraints</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="ddl-generated-columns.html" title="5.3. Generated Columns" /><link rel="next" href="ddl-system-columns.html" title="5.5. System Columns" /></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">5.4. Constraints</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ddl-generated-columns.html" title="5.3. Generated Columns">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><th width="60%" align="center">Chapter 5. Data Definition</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="ddl-system-columns.html" title="5.5. System Columns">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="DDL-CONSTRAINTS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5.4. Constraints</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS">5.4.1. Check Constraints</a></span></dt><dt><span class="sect2"><a href="ddl-constraints.html#id-1.5.4.6.6">5.4.2. Not-Null Constraints</a></span></dt><dt><span class="sect2"><a href="ddl-constraints.html#DDL-CONSTRAINTS-UNIQUE-CONSTRAINTS">5.4.3. Unique Constraints</a></span></dt><dt><span class="sect2"><a href="ddl-constraints.html#DDL-CONSTRAINTS-PRIMARY-KEYS">5.4.4. Primary Keys</a></span></dt><dt><span class="sect2"><a href="ddl-constraints.html#DDL-CONSTRAINTS-FK">5.4.5. Foreign Keys</a></span></dt><dt><span class="sect2"><a href="ddl-constraints.html#DDL-CONSTRAINTS-EXCLUSION">5.4.6. Exclusion Constraints</a></span></dt></dl></div><a id="id-1.5.4.6.2" class="indexterm"></a><p>
  3. Data types are a way to limit the kind of data that can be stored
  4. in a table. For many applications, however, the constraint they
  5. provide is too coarse. For example, a column containing a product
  6. price should probably only accept positive values. But there is no
  7. standard data type that accepts only positive numbers. Another issue is
  8. that you might want to constrain column data with respect to other
  9. columns or rows. For example, in a table containing product
  10. information, there should be only one row for each product number.
  11. </p><p>
  12. To that end, SQL allows you to define constraints on columns and
  13. tables. Constraints give you as much control over the data in your
  14. tables as you wish. If a user attempts to store data in a column
  15. that would violate a constraint, an error is raised. This applies
  16. even if the value came from the default value definition.
  17. </p><div class="sect2" id="DDL-CONSTRAINTS-CHECK-CONSTRAINTS"><div class="titlepage"><div><div><h3 class="title">5.4.1. Check Constraints</h3></div></div></div><a id="id-1.5.4.6.5.2" class="indexterm"></a><a id="id-1.5.4.6.5.3" class="indexterm"></a><p>
  18. A check constraint is the most generic constraint type. It allows
  19. you to specify that the value in a certain column must satisfy a
  20. Boolean (truth-value) expression. For instance, to require positive
  21. product prices, you could use:
  22. </p><pre class="programlisting">
  23. CREATE TABLE products (
  24. product_no integer,
  25. name text,
  26. price numeric <span class="emphasis"><strong>CHECK (price &gt; 0)</strong></span>
  27. );
  28. </pre><p>
  29. </p><p>
  30. As you see, the constraint definition comes after the data type,
  31. just like default value definitions. Default values and
  32. constraints can be listed in any order. A check constraint
  33. consists of the key word <code class="literal">CHECK</code> followed by an
  34. expression in parentheses. The check constraint expression should
  35. involve the column thus constrained, otherwise the constraint
  36. would not make too much sense.
  37. </p><a id="id-1.5.4.6.5.6" class="indexterm"></a><p>
  38. You can also give the constraint a separate name. This clarifies
  39. error messages and allows you to refer to the constraint when you
  40. need to change it. The syntax is:
  41. </p><pre class="programlisting">
  42. CREATE TABLE products (
  43. product_no integer,
  44. name text,
  45. price numeric <span class="emphasis"><strong>CONSTRAINT positive_price</strong></span> CHECK (price &gt; 0)
  46. );
  47. </pre><p>
  48. So, to specify a named constraint, use the key word
  49. <code class="literal">CONSTRAINT</code> followed by an identifier followed
  50. by the constraint definition. (If you don't specify a constraint
  51. name in this way, the system chooses a name for you.)
  52. </p><p>
  53. A check constraint can also refer to several columns. Say you
  54. store a regular price and a discounted price, and you want to
  55. ensure that the discounted price is lower than the regular price:
  56. </p><pre class="programlisting">
  57. CREATE TABLE products (
  58. product_no integer,
  59. name text,
  60. price numeric CHECK (price &gt; 0),
  61. discounted_price numeric CHECK (discounted_price &gt; 0),
  62. <span class="emphasis"><strong>CHECK (price &gt; discounted_price)</strong></span>
  63. );
  64. </pre><p>
  65. </p><p>
  66. The first two constraints should look familiar. The third one
  67. uses a new syntax. It is not attached to a particular column,
  68. instead it appears as a separate item in the comma-separated
  69. column list. Column definitions and these constraint
  70. definitions can be listed in mixed order.
  71. </p><p>
  72. We say that the first two constraints are column constraints, whereas the
  73. third one is a table constraint because it is written separately
  74. from any one column definition. Column constraints can also be
  75. written as table constraints, while the reverse is not necessarily
  76. possible, since a column constraint is supposed to refer to only the
  77. column it is attached to. (<span class="productname">PostgreSQL</span> doesn't
  78. enforce that rule, but you should follow it if you want your table
  79. definitions to work with other database systems.) The above example could
  80. also be written as:
  81. </p><pre class="programlisting">
  82. CREATE TABLE products (
  83. product_no integer,
  84. name text,
  85. price numeric,
  86. CHECK (price &gt; 0),
  87. discounted_price numeric,
  88. CHECK (discounted_price &gt; 0),
  89. CHECK (price &gt; discounted_price)
  90. );
  91. </pre><p>
  92. or even:
  93. </p><pre class="programlisting">
  94. CREATE TABLE products (
  95. product_no integer,
  96. name text,
  97. price numeric CHECK (price &gt; 0),
  98. discounted_price numeric,
  99. CHECK (discounted_price &gt; 0 AND price &gt; discounted_price)
  100. );
  101. </pre><p>
  102. It's a matter of taste.
  103. </p><p>
  104. Names can be assigned to table constraints in the same way as
  105. column constraints:
  106. </p><pre class="programlisting">
  107. CREATE TABLE products (
  108. product_no integer,
  109. name text,
  110. price numeric,
  111. CHECK (price &gt; 0),
  112. discounted_price numeric,
  113. CHECK (discounted_price &gt; 0),
  114. <span class="emphasis"><strong>CONSTRAINT valid_discount</strong></span> CHECK (price &gt; discounted_price)
  115. );
  116. </pre><p>
  117. </p><a id="id-1.5.4.6.5.12" class="indexterm"></a><p>
  118. It should be noted that a check constraint is satisfied if the
  119. check expression evaluates to true or the null value. Since most
  120. expressions will evaluate to the null value if any operand is null,
  121. they will not prevent null values in the constrained columns. To
  122. ensure that a column does not contain null values, the not-null
  123. constraint described in the next section can be used.
  124. </p><div class="note"><h3 class="title">Note</h3><p>
  125. <span class="productname">PostgreSQL</span> does not support
  126. <code class="literal">CHECK</code> constraints that reference table data other than
  127. the new or updated row being checked. While a <code class="literal">CHECK</code>
  128. constraint that violates this rule may appear to work in simple
  129. tests, it cannot guarantee that the database will not reach a state
  130. in which the constraint condition is false (due to subsequent changes
  131. of the other row(s) involved). This would cause a database dump and
  132. reload to fail. The reload could fail even when the complete
  133. database state is consistent with the constraint, due to rows not
  134. being loaded in an order that will satisfy the constraint. If
  135. possible, use <code class="literal">UNIQUE</code>, <code class="literal">EXCLUDE</code>,
  136. or <code class="literal">FOREIGN KEY</code> constraints to express
  137. cross-row and cross-table restrictions.
  138. </p><p>
  139. If what you desire is a one-time check against other rows at row
  140. insertion, rather than a continuously-maintained consistency
  141. guarantee, a custom <a class="link" href="triggers.html" title="Chapter 38. Triggers">trigger</a> can be used
  142. to implement that. (This approach avoids the dump/reload problem because
  143. <span class="application">pg_dump</span> does not reinstall triggers until after
  144. reloading data, so that the check will not be enforced during a
  145. dump/reload.)
  146. </p></div><div class="note"><h3 class="title">Note</h3><p>
  147. <span class="productname">PostgreSQL</span> assumes that
  148. <code class="literal">CHECK</code> constraints' conditions are immutable, that
  149. is, they will always give the same result for the same input row.
  150. This assumption is what justifies examining <code class="literal">CHECK</code>
  151. constraints only when rows are inserted or updated, and not at other
  152. times. (The warning above about not referencing other table data is
  153. really a special case of this restriction.)
  154. </p><p>
  155. An example of a common way to break this assumption is to reference a
  156. user-defined function in a <code class="literal">CHECK</code> expression, and
  157. then change the behavior of that
  158. function. <span class="productname">PostgreSQL</span> does not disallow
  159. that, but it will not notice if there are rows in the table that now
  160. violate the <code class="literal">CHECK</code> constraint. That would cause a
  161. subsequent database dump and reload to fail.
  162. The recommended way to handle such a change is to drop the constraint
  163. (using <code class="command">ALTER TABLE</code>), adjust the function definition,
  164. and re-add the constraint, thereby rechecking it against all table rows.
  165. </p></div></div><div class="sect2" id="id-1.5.4.6.6"><div class="titlepage"><div><div><h3 class="title">5.4.2. Not-Null Constraints</h3></div></div></div><a id="id-1.5.4.6.6.2" class="indexterm"></a><a id="id-1.5.4.6.6.3" class="indexterm"></a><p>
  166. A not-null constraint simply specifies that a column must not
  167. assume the null value. A syntax example:
  168. </p><pre class="programlisting">
  169. CREATE TABLE products (
  170. product_no integer <span class="emphasis"><strong>NOT NULL</strong></span>,
  171. name text <span class="emphasis"><strong>NOT NULL</strong></span>,
  172. price numeric
  173. );
  174. </pre><p>
  175. </p><p>
  176. A not-null constraint is always written as a column constraint. A
  177. not-null constraint is functionally equivalent to creating a check
  178. constraint <code class="literal">CHECK (<em class="replaceable"><code>column_name</code></em>
  179. IS NOT NULL)</code>, but in
  180. <span class="productname">PostgreSQL</span> creating an explicit
  181. not-null constraint is more efficient. The drawback is that you
  182. cannot give explicit names to not-null constraints created this
  183. way.
  184. </p><p>
  185. Of course, a column can have more than one constraint. Just write
  186. the constraints one after another:
  187. </p><pre class="programlisting">
  188. CREATE TABLE products (
  189. product_no integer NOT NULL,
  190. name text NOT NULL,
  191. price numeric NOT NULL CHECK (price &gt; 0)
  192. );
  193. </pre><p>
  194. The order doesn't matter. It does not necessarily determine in which
  195. order the constraints are checked.
  196. </p><p>
  197. The <code class="literal">NOT NULL</code> constraint has an inverse: the
  198. <code class="literal">NULL</code> constraint. This does not mean that the
  199. column must be null, which would surely be useless. Instead, this
  200. simply selects the default behavior that the column might be null.
  201. The <code class="literal">NULL</code> constraint is not present in the SQL
  202. standard and should not be used in portable applications. (It was
  203. only added to <span class="productname">PostgreSQL</span> to be
  204. compatible with some other database systems.) Some users, however,
  205. like it because it makes it easy to toggle the constraint in a
  206. script file. For example, you could start with:
  207. </p><pre class="programlisting">
  208. CREATE TABLE products (
  209. product_no integer NULL,
  210. name text NULL,
  211. price numeric NULL
  212. );
  213. </pre><p>
  214. and then insert the <code class="literal">NOT</code> key word where desired.
  215. </p><div class="tip"><h3 class="title">Tip</h3><p>
  216. In most database designs the majority of columns should be marked
  217. not null.
  218. </p></div></div><div class="sect2" id="DDL-CONSTRAINTS-UNIQUE-CONSTRAINTS"><div class="titlepage"><div><div><h3 class="title">5.4.3. Unique Constraints</h3></div></div></div><a id="id-1.5.4.6.7.2" class="indexterm"></a><a id="id-1.5.4.6.7.3" class="indexterm"></a><p>
  219. Unique constraints ensure that the data contained in a column, or a
  220. group of columns, is unique among all the rows in the
  221. table. The syntax is:
  222. </p><pre class="programlisting">
  223. CREATE TABLE products (
  224. product_no integer <span class="emphasis"><strong>UNIQUE</strong></span>,
  225. name text,
  226. price numeric
  227. );
  228. </pre><p>
  229. when written as a column constraint, and:
  230. </p><pre class="programlisting">
  231. CREATE TABLE products (
  232. product_no integer,
  233. name text,
  234. price numeric,
  235. <span class="emphasis"><strong>UNIQUE (product_no)</strong></span>
  236. );
  237. </pre><p>
  238. when written as a table constraint.
  239. </p><p>
  240. To define a unique constraint for a group of columns, write it as a
  241. table constraint with the column names separated by commas:
  242. </p><pre class="programlisting">
  243. CREATE TABLE example (
  244. a integer,
  245. b integer,
  246. c integer,
  247. <span class="emphasis"><strong>UNIQUE (a, c)</strong></span>
  248. );
  249. </pre><p>
  250. This specifies that the combination of values in the indicated columns
  251. is unique across the whole table, though any one of the columns
  252. need not be (and ordinarily isn't) unique.
  253. </p><p>
  254. You can assign your own name for a unique constraint, in the usual way:
  255. </p><pre class="programlisting">
  256. CREATE TABLE products (
  257. product_no integer <span class="emphasis"><strong>CONSTRAINT must_be_different</strong></span> UNIQUE,
  258. name text,
  259. price numeric
  260. );
  261. </pre><p>
  262. </p><p>
  263. Adding a unique constraint will automatically create a unique B-tree
  264. index on the column or group of columns listed in the constraint.
  265. A uniqueness restriction covering only some rows cannot be written as
  266. a unique constraint, but it is possible to enforce such a restriction by
  267. creating a unique <a class="link" href="indexes-partial.html" title="11.8. Partial Indexes">partial index</a>.
  268. </p><a id="id-1.5.4.6.7.8" class="indexterm"></a><p>
  269. In general, a unique constraint is violated if there is more than
  270. one row in the table where the values of all of the
  271. columns included in the constraint are equal.
  272. However, two null values are never considered equal in this
  273. comparison. That means even in the presence of a
  274. unique constraint it is possible to store duplicate
  275. rows that contain a null value in at least one of the constrained
  276. columns. This behavior conforms to the SQL standard, but we have
  277. heard that other SQL databases might not follow this rule. So be
  278. careful when developing applications that are intended to be
  279. portable.
  280. </p></div><div class="sect2" id="DDL-CONSTRAINTS-PRIMARY-KEYS"><div class="titlepage"><div><div><h3 class="title">5.4.4. Primary Keys</h3></div></div></div><a id="id-1.5.4.6.8.2" class="indexterm"></a><a id="id-1.5.4.6.8.3" class="indexterm"></a><p>
  281. A primary key constraint indicates that a column, or group of columns,
  282. can be used as a unique identifier for rows in the table. This
  283. requires that the values be both unique and not null. So, the following
  284. two table definitions accept the same data:
  285. </p><pre class="programlisting">
  286. CREATE TABLE products (
  287. product_no integer UNIQUE NOT NULL,
  288. name text,
  289. price numeric
  290. );
  291. </pre><p>
  292. </p><pre class="programlisting">
  293. CREATE TABLE products (
  294. product_no integer <span class="emphasis"><strong>PRIMARY KEY</strong></span>,
  295. name text,
  296. price numeric
  297. );
  298. </pre><p>
  299. </p><p>
  300. Primary keys can span more than one column; the syntax
  301. is similar to unique constraints:
  302. </p><pre class="programlisting">
  303. CREATE TABLE example (
  304. a integer,
  305. b integer,
  306. c integer,
  307. <span class="emphasis"><strong>PRIMARY KEY (a, c)</strong></span>
  308. );
  309. </pre><p>
  310. </p><p>
  311. Adding a primary key will automatically create a unique B-tree index
  312. on the column or group of columns listed in the primary key, and will
  313. force the column(s) to be marked <code class="literal">NOT NULL</code>.
  314. </p><p>
  315. A table can have at most one primary key. (There can be any number
  316. of unique and not-null constraints, which are functionally almost the
  317. same thing, but only one can be identified as the primary key.)
  318. Relational database theory
  319. dictates that every table must have a primary key. This rule is
  320. not enforced by <span class="productname">PostgreSQL</span>, but it is
  321. usually best to follow it.
  322. </p><p>
  323. Primary keys are useful both for
  324. documentation purposes and for client applications. For example,
  325. a GUI application that allows modifying row values probably needs
  326. to know the primary key of a table to be able to identify rows
  327. uniquely. There are also various ways in which the database system
  328. makes use of a primary key if one has been declared; for example,
  329. the primary key defines the default target column(s) for foreign keys
  330. referencing its table.
  331. </p></div><div class="sect2" id="DDL-CONSTRAINTS-FK"><div class="titlepage"><div><div><h3 class="title">5.4.5. Foreign Keys</h3></div></div></div><a id="id-1.5.4.6.9.2" class="indexterm"></a><a id="id-1.5.4.6.9.3" class="indexterm"></a><a id="id-1.5.4.6.9.4" class="indexterm"></a><p>
  332. A foreign key constraint specifies that the values in a column (or
  333. a group of columns) must match the values appearing in some row
  334. of another table.
  335. We say this maintains the <em class="firstterm">referential
  336. integrity</em> between two related tables.
  337. </p><p>
  338. Say you have the product table that we have used several times already:
  339. </p><pre class="programlisting">
  340. CREATE TABLE products (
  341. product_no integer PRIMARY KEY,
  342. name text,
  343. price numeric
  344. );
  345. </pre><p>
  346. Let's also assume you have a table storing orders of those
  347. products. We want to ensure that the orders table only contains
  348. orders of products that actually exist. So we define a foreign
  349. key constraint in the orders table that references the products
  350. table:
  351. </p><pre class="programlisting">
  352. CREATE TABLE orders (
  353. order_id integer PRIMARY KEY,
  354. product_no integer <span class="emphasis"><strong>REFERENCES products (product_no)</strong></span>,
  355. quantity integer
  356. );
  357. </pre><p>
  358. Now it is impossible to create orders with non-NULL
  359. <code class="structfield">product_no</code> entries that do not appear in the
  360. products table.
  361. </p><p>
  362. We say that in this situation the orders table is the
  363. <em class="firstterm">referencing</em> table and the products table is
  364. the <em class="firstterm">referenced</em> table. Similarly, there are
  365. referencing and referenced columns.
  366. </p><p>
  367. You can also shorten the above command to:
  368. </p><pre class="programlisting">
  369. CREATE TABLE orders (
  370. order_id integer PRIMARY KEY,
  371. product_no integer <span class="emphasis"><strong>REFERENCES products</strong></span>,
  372. quantity integer
  373. );
  374. </pre><p>
  375. because in absence of a column list the primary key of the
  376. referenced table is used as the referenced column(s).
  377. </p><p>
  378. A foreign key can also constrain and reference a group of columns.
  379. As usual, it then needs to be written in table constraint form.
  380. Here is a contrived syntax example:
  381. </p><pre class="programlisting">
  382. CREATE TABLE t1 (
  383. a integer PRIMARY KEY,
  384. b integer,
  385. c integer,
  386. <span class="emphasis"><strong>FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)</strong></span>
  387. );
  388. </pre><p>
  389. Of course, the number and type of the constrained columns need to
  390. match the number and type of the referenced columns.
  391. </p><p>
  392. You can assign your own name for a foreign key constraint,
  393. in the usual way.
  394. </p><p>
  395. A table can have more than one foreign key constraint. This is
  396. used to implement many-to-many relationships between tables. Say
  397. you have tables about products and orders, but now you want to
  398. allow one order to contain possibly many products (which the
  399. structure above did not allow). You could use this table structure:
  400. </p><pre class="programlisting">
  401. CREATE TABLE products (
  402. product_no integer PRIMARY KEY,
  403. name text,
  404. price numeric
  405. );
  406. CREATE TABLE orders (
  407. order_id integer PRIMARY KEY,
  408. shipping_address text,
  409. ...
  410. );
  411. CREATE TABLE order_items (
  412. product_no integer REFERENCES products,
  413. order_id integer REFERENCES orders,
  414. quantity integer,
  415. PRIMARY KEY (product_no, order_id)
  416. );
  417. </pre><p>
  418. Notice that the primary key overlaps with the foreign keys in
  419. the last table.
  420. </p><a id="id-1.5.4.6.9.12" class="indexterm"></a><a id="id-1.5.4.6.9.13" class="indexterm"></a><p>
  421. We know that the foreign keys disallow creation of orders that
  422. do not relate to any products. But what if a product is removed
  423. after an order is created that references it? SQL allows you to
  424. handle that as well. Intuitively, we have a few options:
  425. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: disc; "><li class="listitem"><p>Disallow deleting a referenced product</p></li><li class="listitem"><p>Delete the orders as well</p></li><li class="listitem"><p>Something else?</p></li></ul></div><p>
  426. </p><p>
  427. To illustrate this, let's implement the following policy on the
  428. many-to-many relationship example above: when someone wants to
  429. remove a product that is still referenced by an order (via
  430. <code class="literal">order_items</code>), we disallow it. If someone
  431. removes an order, the order items are removed as well:
  432. </p><pre class="programlisting">
  433. CREATE TABLE products (
  434. product_no integer PRIMARY KEY,
  435. name text,
  436. price numeric
  437. );
  438. CREATE TABLE orders (
  439. order_id integer PRIMARY KEY,
  440. shipping_address text,
  441. ...
  442. );
  443. CREATE TABLE order_items (
  444. product_no integer REFERENCES products <span class="emphasis"><strong>ON DELETE RESTRICT</strong></span>,
  445. order_id integer REFERENCES orders <span class="emphasis"><strong>ON DELETE CASCADE</strong></span>,
  446. quantity integer,
  447. PRIMARY KEY (product_no, order_id)
  448. );
  449. </pre><p>
  450. </p><p>
  451. Restricting and cascading deletes are the two most common options.
  452. <code class="literal">RESTRICT</code> prevents deletion of a
  453. referenced row. <code class="literal">NO ACTION</code> means that if any
  454. referencing rows still exist when the constraint is checked, an error
  455. is raised; this is the default behavior if you do not specify anything.
  456. (The essential difference between these two choices is that
  457. <code class="literal">NO ACTION</code> allows the check to be deferred until
  458. later in the transaction, whereas <code class="literal">RESTRICT</code> does not.)
  459. <code class="literal">CASCADE</code> specifies that when a referenced row is deleted,
  460. row(s) referencing it should be automatically deleted as well.
  461. There are two other options:
  462. <code class="literal">SET NULL</code> and <code class="literal">SET DEFAULT</code>.
  463. These cause the referencing column(s) in the referencing row(s)
  464. to be set to nulls or their default
  465. values, respectively, when the referenced row is deleted.
  466. Note that these do not excuse you from observing any constraints.
  467. For example, if an action specifies <code class="literal">SET DEFAULT</code>
  468. but the default value would not satisfy the foreign key constraint, the
  469. operation will fail.
  470. </p><p>
  471. Analogous to <code class="literal">ON DELETE</code> there is also
  472. <code class="literal">ON UPDATE</code> which is invoked when a referenced
  473. column is changed (updated). The possible actions are the same.
  474. In this case, <code class="literal">CASCADE</code> means that the updated values of the
  475. referenced column(s) should be copied into the referencing row(s).
  476. </p><p>
  477. Normally, a referencing row need not satisfy the foreign key constraint
  478. if any of its referencing columns are null. If <code class="literal">MATCH FULL</code>
  479. is added to the foreign key declaration, a referencing row escapes
  480. satisfying the constraint only if all its referencing columns are null
  481. (so a mix of null and non-null values is guaranteed to fail a
  482. <code class="literal">MATCH FULL</code> constraint). If you don't want referencing rows
  483. to be able to avoid satisfying the foreign key constraint, declare the
  484. referencing column(s) as <code class="literal">NOT NULL</code>.
  485. </p><p>
  486. A foreign key must reference columns that either are a primary key or
  487. form a unique constraint. This means that the referenced columns always
  488. have an index (the one underlying the primary key or unique constraint);
  489. so checks on whether a referencing row has a match will be efficient.
  490. Since a <code class="command">DELETE</code> of a row from the referenced table
  491. or an <code class="command">UPDATE</code> of a referenced column will require
  492. a scan of the referencing table for rows matching the old value, it
  493. is often a good idea to index the referencing columns too. Because this
  494. is not always needed, and there are many choices available on how
  495. to index, declaration of a foreign key constraint does not
  496. automatically create an index on the referencing columns.
  497. </p><p>
  498. More information about updating and deleting data is in <a class="xref" href="dml.html" title="Chapter 6. Data Manipulation">Chapter 6</a>. Also see the description of foreign key constraint
  499. syntax in the reference documentation for
  500. <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>.
  501. </p></div><div class="sect2" id="DDL-CONSTRAINTS-EXCLUSION"><div class="titlepage"><div><div><h3 class="title">5.4.6. Exclusion Constraints</h3></div></div></div><a id="id-1.5.4.6.10.2" class="indexterm"></a><a id="id-1.5.4.6.10.3" class="indexterm"></a><p>
  502. Exclusion constraints ensure that if any two rows are compared on
  503. the specified columns or expressions using the specified operators,
  504. at least one of these operator comparisons will return false or null.
  505. The syntax is:
  506. </p><pre class="programlisting">
  507. CREATE TABLE circles (
  508. c circle,
  509. EXCLUDE USING gist (c WITH &amp;&amp;)
  510. );
  511. </pre><p>
  512. </p><p>
  513. See also <a class="link" href="sql-createtable.html#SQL-CREATETABLE-EXCLUDE"><code class="command">CREATE
  514. TABLE ... CONSTRAINT ... EXCLUDE</code></a> for details.
  515. </p><p>
  516. Adding an exclusion constraint will automatically create an index
  517. of the type specified in the constraint declaration.
  518. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ddl-generated-columns.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ddl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ddl-system-columns.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.3. Generated Columns </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.5. System Columns</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1