您的位置:首页 > 数据库

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

2017-10-30 00:00 661 查看
摘要: 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.

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

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);

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

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);

);
数据插入成功。

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

先创建表。

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

插入正常数据

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);

数据库脚本执行成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息