PostgreSQL数据库平常学习笔记13-约束

PostgreSQL常见约束有检查约束、非空约束、惟一约束、主键约束和外键约束等,部分约束类型还能够指定多列,指定多列本文不介绍过多,请参考PostgreSQL数据库手册。下文将一一详加介绍。数据库

首先咱们介绍检查约束。检查约束要求数据必须知足某些条件,例如数值必须大于或小于某些值,或在某字段等。测试

---建立员工信息表
---性别值为0或1
create table "workerInformation"(
    workerid int,
    "workerName" varchar(30),
    "workerBirthday" date,
    "workerSex" int check ("workerSex"=0 or "workerSex"=1)
);

#CREATE TABLE

#Query returned successfully in 139 msec.

下面咱们尝试插入正常数据。code

insert into "public"."workerInformation"(workerid,"workerName","workerBirthday","workerSex") values(1,'顾留芳','0670-1-1',1);
insert into "public"."workerInformation"(workerid,"workerName","workerBirthday","workerSex") values(2,'林业平','0770-1-1',1);

下面插入错误数据,须要指出表中某列名有大小写等特殊状况须要表名加英文双引号""。orm

insert into "public"."workerInformation"(workerid,"workerName","workerBirthday","workerSex") values(3,'徐长卿','0770-1-1',2);
#ERROR:  new row for relation "workerInformation" violates check constraint  "workerInformation_workerSex_check" 
#DETAIL:  Failing row contains (3, 徐长卿, 0770-01-01, 2).
#SQL 状态:23514

数据库有如上错误提示,下面修改成正确数据。产品

insert into "public"."workerInformation"(workerid,"workerName","workerBirthday","workerSex") values(3,'徐长卿','0865-1-1',1);

); 数据插入成功。it

下面介绍非空约束。非空约束很容易理解,就是要求数据不能为空,列内须要有数据。声明非空列使用关键字 not null (或 NOT NULL)。io

先建立表。table

CREATE TABLE products (
    "productNO" integer NOT NULL,
    name text NOT NULL,
    price numeric
);

插入正常数据form

INSERT INTO "public".products VALUES(1,'统一老坛酸菜牛肉面',3.6);
INSERT INTO "public".products VALUES(2,'',2);

插入以下错误数据软件

INSERT INTO "public".products VALUES(2,,2);

软件会提示错误,插入容许为空数据时,该列值须要声明值为NULL,不然软件也会提示错误。

接上文代码,连续屡次执行代码

---执行2以上
INSERT INTO "public".products VALUES(3,'',2);

执行查询语句

SELECT * FROM "public".products;

会发现有多行相同数据。

"相同列"

删除原表并新建新表。

---"productNO"惟一约束
CREATE TABLE products (
    "productNO" integer UNIQUE,
    name text,
    price numeric
);

执行测试代码。

---执行2以上
INSERT INTO "public".products VALUES(3,'',2);

第一次正常执行代码后,第二次执行代码软件会产生错误信息。

#ERROR:  duplicate key value violates unique constraint "products_productNO_key"
#DETAIL:  Key ("productNO")=(3) already exists.
#SQL 状态:23505

主键约束,顾名思义就是指定主键。

再次删除测试表products。建立新表并建立主键productNO。

---"productNO"建立主键productNO
CREATE TABLE products (
    "productNO" integer UNIQUE PRIMARY KEY,
    name TEXT,
    price NUMERIC
);

惟一约束是指当前列不能有重复值。

---建立表并设置"productNO" 列惟一约束
CREATE TABLE products (
    "productNO" integer NOT NULL UNIQUE,
    name text NOT NULL,
    price numeric
);
---另外一种惟一约束写法
CREATE TABLE products (
    "productNO" INTEGER NOT NULL,
    name TEXT NOT NULL,
    price NUMERIC,
    UNIQUE("productNO")
);
---先建立表
CREATE TABLE products (
    "productNO" INTEGER NOT NULL,
    name TEXT NOT NULL,
    price NUMERIC
);
---后设置约束
ALTER TABLE "public"."products" ADD UNIQUE("productNO");

外键约束就是指某一列必须匹配另外一个表某列,用于维持两个关联表数据完整性。删除表须要先删除引用表,再删除被引用表。

建立有外键表orders 。

---建立订单表orders 
CREATE TABLE orders (
    "orderID" integer PRIMARY KEY,
    "productNO" integer REFERENCES products ("productNO"),
    quantity integer
);

插入错误值后错误信息以下:

---产品编号为3商品不存在
INSERT INTO "public".orders VALUES(1,3,1);

#ERROR:  insert or update on table "orders" violates foreign key constraint "orders_productNO_fkey"
#DETAIL:  Key (productNO)=(3) is not present in table "products".
#SQL 状态:23503

改为正确信息后

INSERT INTO "public".orders VALUES(1,2,1);

数据库脚本执行成功。

相关文章
相关标签/搜索