Skip site navigation (1) Skip section navigation (2)

5.3.5. Foreign Keys (The SQL Language) possible enhance

From: Grzegorz Szpetkowski <gszpetkowski(at)gmail(dot)com>
To: pgsql-docs(at)postgresql(dot)org
Subject: 5.3.5. Foreign Keys (The SQL Language) possible enhance
Date: 2011-05-07 01:50:07
Message-ID: BANLkTimbdXudnU+fV236f8aQ3iHdta_MOA@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-docs
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

Responses

pgsql-docs by date

Next:From: Khusro JaleelDate: 2011-05-07 16:40:22
Subject: Error in SSL config documentation?
Previous:From: Tom LaneDate: 2011-05-07 00:18:13
Subject: Re: should pg_basebackup be listed as a server application?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group