Обсуждение: Column and table constraints - anally retentive comments

Поиск
Список
Период
Сортировка

Column and table constraints - anally retentive comments

От
Richard Huxton
Дата:
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

Re: Column and table constraints - anally retentive comments

От
Peter Eisentraut
Дата:
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   peter_e@gmx.net