PostgreSQL/Constraints/Check

Материал из SQL эксперт
Перейти к: навигация, поиск

Specifying three check constaints to a table

postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer,
postgres(#    price numeric,  CHECK (price > 0),
postgres(#    discounted_price numeric,
postgres(#    CHECK (discounted_price > 0),
postgres(#    CHECK (price > discounted_price)
postgres(# );
CREATE TABLE
postgres=#
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#
postgres=#



Use boolean operator in check statement

postgres=#
postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer,
postgres(#    name text,
postgres(#    price numeric CHECK (price > 0),
postgres(#    discounted_price numeric,
postgres(#    CHECK (discounted_price > 0 AND price > discounted_price)
postgres(# );
CREATE TABLE
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#