public inbox for [email protected]  
help / color / mirror / Atom feed
Column and table constraints - anally retentive comments
2+ messages / 2 participants
[nested] [flat]

* Column and table constraints - anally retentive comments
@ 2002-11-15 15:18 Richard Huxton <[email protected]>
  2002-11-17 13:24 ` Re: Column and table constraints - anally retentive comments Peter Eisentraut <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Richard Huxton @ 2002-11-15 15:18 UTC (permalink / raw)
  To: 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



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Column and table constraints - anally retentive comments
  2002-11-15 15:18 Column and table constraints - anally retentive comments Richard Huxton <[email protected]>
@ 2002-11-17 13:24 ` Peter Eisentraut <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Peter Eisentraut @ 2002-11-17 13:24 UTC (permalink / raw)
  To: Richard Huxton <[email protected]>; +Cc: pgsql-docs

Richard Huxton writes:

> 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.

It's a matter of taste. ;-)  Which does not say that any one choice
corresponds to the actual taste of the majority.

Note that the section you refer to specifically aims to illustrate that
column and table constraints are more or less interchangeable, which does
not mean that it is always a good idea to change them around beyond
recognition.

That said, if you have a better way to word it, please say so.

-- 
Peter Eisentraut   [email protected]





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-11-17 13:24 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-11-15 15:18 Column and table constraints - anally retentive comments Richard Huxton <[email protected]>
2002-11-17 13:24 ` Peter Eisentraut <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox