Column and table constraints - anally retentive comments

From: Richard Huxton <dev(at)archonet(dot)com>
To: pgsql-docs(at)postgresql(dot)org
Subject: Column and table constraints - anally retentive comments
Date: 2002-11-15 15:18:26
Message-ID: 200211151518.26707.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

Probably just me, but in
http://developer.postgresql.org/docs/postgres/ddl-constraints.html#AEN1793

---begin extract---
price numeric CHECK (price > 0),
discounted_price numeric CHECK (discounted_price > 0),
CHECK (price > discounted_price)
);

The first two constraints should look familiar. The third one uses a new
syntax. It is not attached to a particular column, instead it appears as a
separate item in the comma-separated column list. Column definitions and
these constraint definitions can be listed in mixed order.

We say that the first two constraints are column constraints, whereas the
third one is a table constraint because it is written separately from the
column definitions. Column constraints can also be written as table
constraints, while the reverse is not necessarily possible. The above example
could also be written as

CREATE TABLE products (
product_no integer,
name text,
price numeric,
CHECK (price > 0),
discounted_price numeric,
CHECK (discounted_price > 0),
CHECK (price > discounted_price)
);

or even

CREATE TABLE products (
product_no integer,
name text,
price numeric CHECK (price > 0),
discounted_price numeric,
CHECK (discounted_price > 0 AND price > discounted_price)
);

It's a matter of taste.
---end extract---

Not quite taste I'd have thought. Column (value) related constraints should be
written as such, not disconnected from their column. Same principle as
declaring variables near to where they are needed.

In the example given it would make more sense to have a domain "product_price"
with a constraint on it. Now, I realise we can't do the constraint in 7.3 but
later on people will find it easier to maintain if we encourage good
behaviour.

--
Richard Huxton

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Neil Conway 2002-11-15 22:50:55 Re: A question
Previous Message x 2002-11-15 00:55:52 A question