Search Postgresql Archives

Re: How to tell PostgreSQL about a relationship

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 26 okt 2008, at 10.44, Thomas wrote:
Currently I have 3 tables:

Product(id, title, price)
Item(id, product_id, order_id, quantity)
Order(id, amount, paid)

So how do I tell PgSQL that
when I remove a given order, it should remove all associated items
also?

Use a foreign key constraint with the appropriate action:

CREATE TABLE Item (
  id SERIAL PRIMARY KEY,
product_id INTEGER NOT NULL REFERENCES Product(id) ON DELETE RESTRICT ON UPDATE CASCADE, order_id INTEGER NOT NULL REFERENCES Order(id) ON DELETE CASCADE ON UPDATE CASCADE,
  quantity NUMERIC(5,2) NOT NULL
);


For more info, see the docs: http://www.postgresql.org/docs/8.3/ interactive/ddl-constraints.html#DDL-CONSTRAINTS-FK



Sincerely,

Niklas Johansson




--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux