Subject: 5.3.5. Foreign Keys (The SQL Language) possible enhance

From: Grzegorz Szpetkowski <[email-masked]>
Date: Sat, 7 May 2011 03:50:07 +0200

I have some remark about

"Now it is impossible to create orders with product_no entries that do
not appear in the products table."

http://www.postgresql.org/docs/9.0/static/ddl-constraints.html#DDL-CONSTRAINTS-FK

Let' see:

CREATE TABLE products (
    product_no integer PRIMARY KEY,
    name text,
    price numeric
);

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    product_no integer REFERENCES products (product_no),
    quantity integer
);

INSERT INTO products VALUES (1, 'Bosch vacuum cleaner', 10);
INSERT INTO orders VALUES (1, 1, 1);
INSERT INTO orders VALUES (2, NULL, 5);

There is still possibility to add product_no (exactly NULL) value,
which does not appear (cannot because of primary key nature) in
products table. To get "full solution" you need create orders table as

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    product_no integer REFERENCES products (product_no) NOT NULL,
    quantity integer
);

Regards,
Grzegorz Szpetkowski

---

From: Robert Haas <[email-masked]>
Date: Thu, 19 May 2011 16:17:10 -0400

> http://www.postgresql.org/docs/9.0/static/ddl-constraints.html#DDL-CONSTRAINTS-FK
[...]
>    product_no integer REFERENCES products (product_no) NOT NULL,

I don't think we should change the example, but we could probably
clarify the wording.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

---

From: Bruce Momjian <[email-masked]>
Date: Sun, 11 Sep 2011 22:09:46 -0400 (EDT)

Robert Haas wrote:
> > ? ?order_id integer PRIMARY KEY,
> > ? ?product_no integer REFERENCES products (product_no) NOT NULL,
> > ? ?quantity integer
> > );
> 

Any ideas on how to clarify the wording?

-- 
  Bruce Momjian  <[email-masked]>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +

---

From: Robert Haas <[email-masked]>
Date: Thu, 15 Sep 2011 06:42:47 -0500

> Any ideas on how to clarify the wording?

Maybe something like this:

Now every product_no that appears in the orders table must also appear
in the products table.  Foreign key constraints are not checked for
NULL values, so product_no may also be NULL; we could prohibit this by
declaring the column NOT NULL.

---

From: Bruce Momjian <[email-masked]>
Date: Wed, 12 Oct 2011 16:59:04 -0400 (EDT)

> I don't think we should change the example, but we could probably

Done with the attached patch.

  + It's impossible for everything to be true. +
diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml
new file mode 100644
index c624fc2..ea840fb
*** a/doc/src/sgml/ddl.sgml
--- b/doc/src/sgml/ddl.sgml
*************** CREATE TABLE orders (
*** 688,694 ****
      quantity integer
  );
  </programlisting>
!     Now it is impossible to create orders with
      <structfield>product_no</structfield> entries that do not appear in the
      products table.
     </para>
--- 688,694 ----
      quantity integer
  );
  </programlisting>
!     Now it is impossible to create orders with non-NULL
      <structfield>product_no</structfield> entries that do not appear in the
      products table.
     </para>