CREATE TABLE Products (
product_no integer,
name text,
price numeric DEFAULT 9.99
);
DROP TABLE Products;
CREATE TABLE Products (
products_no SERIAL
);
DROP TABLE Products;
CREATE TABLE Products (
product_no SERIAL,
name text,
price numeric CHECK(price > 0)
);
DROP TABLE Products;
CREATE TABLE Products (
product__no SERIAL,
name text,
price numeric CONSTRAINT positive_price CHECK(price > 0)
);
DROP TABLE Products;
CREATE TABLE Products (
product_no SERIAL,
name text,
price numeric CONSTRAINT positive_price CHECK(price > 0),
discounted_price numeric CONSTRAINT positive_discounted_price CHECK(discounted_price > 0),
CONSTRAINT price_gt_dicounted CHECK(price > discounted_price)
);
DROP TABLE Products;
CREATE TABLE Products (
product_no integer NOT NULL,
name text NOT NULL,
price numeric NOT NULL
);
DROP TABLE Products;
CREATE TABLE Products (
product_no integer,
name text UNIQUE,
price numeric
);
DROP TABLE Products;
CREATE TABLE Products (
product_no integer,
name text,
price numeric,
UNIQUE(name)
);
DROP TABLE Products;
CREATE TABLE Products (
product_no integer,
name text,
price numeric,
pos integer,
UNIQUE(name,pos)
);
DROP TABLE Products;
CREATE TABLE Products (
product_no integer,
name text NOT NULL,
price numeric,
pos integer NOT NULL,
CONSTRAINT product_unique UNIQUE(name,pos)
);
DROP TABLE Products;
CREATE TABLE Products (
product_no integer,
name text UNIQUE,
price numeric UNIQUE,
pos integer,
PRIMARY KEY (name,pos),
UNIQUE(name,price)
);
CREATE TABLE Pro_Record (
pro_name text REFERENCES Products(name),
price numeric
--or use under method
);
DROP TABLE Pro_Record;
CREATE TABLE Pro_Record (
pro_name text,
price numeric,
FOREIGN KEY(pro_name,price) REFERENCES Products(name,price)
);
DROP TABLE Pro_Record;
DROP TABLE Products;
DROP TABLE Student_Record;
DROP TABLE Student;
DROP TABLE DR1;
DROP TABLE DR2;
DROP TABLE R1;
DROP TABLE R2;
CREATE TABLE Student (
number numeric(8,0) PRIMARY KEY
--maybe have a lot....
);
CREATE TABLE Student_Record (
stu_number numeric(8,0) REFERENCES Student(number) --equal to NO ACTION
);
INSERT INTO Student(number) VALUES (20161000),(20162000),(20163000),(20164000);
SELECT * FROM Student;
SELECT * FROM Student_Record;
INSERT INTO Student_Record(stu_number) VALUES (20161000),(20162000),(20163000),(20164000);
DELETE FROM Student WHERE number = 20161000;
CREATE TABLE R1 (
code numeric(8,0) PRIMARY KEY
);
CREATE TABLE DR1 (
code numeric(8,0) REFERENCES R1(code) ON DELETE RESTRICT
);
CREATE TABLE R2 (
code numeric(8,0) PRIMARY KEY
);
CREATE TABLE DR2 (
code numeric(8,0) REFERENCES R2(code) ON DELETE CASCADE
);
INSERT INTO R1(code) VALUES (20161000);
INSERT INTO DR1(code) VALUES (20161000);
INSERT INTO R2(code) VALUES (20161000),(20162000);
INSERT INTO DR2(code) VALUES (20161000);
DELETE FROM R1 WHERE code = 20161000;
DELETE FROM R2 WHERE code = 20161000;
SELECT * FROM R1;
SELECT * FROM R2;
SELECT * FROM DR1;
SELECT * FROM DR2;
CREATE TABLE Price (
price INT,
EXCLUDE USING btree(price WITH =) --保证插入的价格不相同
);
INSERT INTO Price(price) VALUES (200);
INSERT INTO Price(price) VALUES (300);
INSERT INTO Price(price) VALUES (200);
SELECT * FROM Price;
DROP TABLE Price;
约束种类 |
作用 |
语句 |
检查约束/字段约束 |
通过在字段类型后用[CONSTRAINT CHECK_NAME] CHECK(expression)添加字段约束,使得避免使表达式为假的脏数据(若无明确指定则无法避免NULL) |
[CONSTRAINT CHECK_NAME] CHECK(expression) |
检查约束/表约束 |
通过在表定义中添加[CONSTRAINT CHECK_NAME] CHECK(expression)添加约束,表约束可以引用表中多个字段,而字段约束只能引用自身字段 |
[CONSTRAINT CHECK_NAME] CHECK(expression) |
唯一约束 |
通过设定唯一约束,使得字段不得相同(NULL除外) |
[CONSTRAINT CHECK_NAME] UNIQUE(a,b….) |
非空约束 |
使得字段不得为空 |
NOT NULL |
主键 |
设置主键使得字段内各值不能相同(包括NULL) |
[CONSTRAINT CHECK_NAME]PRIMARY KEY(a,b…) |
外键 |
设置外键可以使得表A和表B具有参照性,含外键的表里元素必须是引用表的存在的 |
[CONSTRAINT FOREIGN_KEY_NAME] FOREIGN KEY (a,b…) REFERENCES B(a,b…) {[ON DELETE][ON UPDATE] [NO ACTION / RESTRICT / CASCADE]}/{SET DEFAULT / SET NULL} |
排他约束 |
避免表中任意两行使得字段操作表达式为真,若为真则操作出错,为假和NULL可以 |
[CONSTAINT CHECK_NAME] EXCLUDE UISNG 搜索途径名(字段名 WITH 操作符) |