1.约束
1.检查约束
CREATE TABLE products (
product_no integer,
name text,
price numeric CHECK (price > 0)
);
2.非空约束
CREATE TABLE products (
product_no integer NOT NULL,
name text NOT NULL,
price numeric
);
3.唯一约束
CREATE TABLE products (
product_no integer UNIQUE,
name text,
price numeric
);
4.主键约束
唯一的并且非空
CREATE TABLE example (
a integer,
b integer,
c integer,
PRIMARY KEY (a, c)
);
5.外键约束
一个外键约束指定一列(或一组列)中的值必须匹配出现在另一个表中某些行的值。
CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric
);
CREATE TABLE orders (
order_id integer PRIMARY KEY,
product_no integer REFERENCES products (product_no),
quantity integer
);
6.排他约束
参考文献:
http://www.postgres.cn/docs/10/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS