5.4.1. Check Constraints

5.4.1.检查约束

A check constraint is the most generic constraint type. It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression. For instance, to require positive product prices, you could use:

检查约束是最通用的约束类型。你可以通过它定义某一列中的值必须符合布尔表达式(真值)。例如,你可以这样设置以仅接受正的产品价格:

CREATE TABLE products (

product_no integer,

name text,

price numeric CHECK (price > 0)

);

As you see, the constraint definition comes after the data type, just like default value definitions.Default values and constraints can be listed in any order. A check constraint consists of the key word CHECK followed by an expression in parentheses. The check constraint expression should involve the column thus constrained, otherwise the constraint would not make too much sense.

正如上所见,约束定义紧跟在数据类型之后,类似于默认值定义。默认值和约束的顺序可随意。检查约束由关键词CHECK以及用括号括起来的一个表达式组成。检查约束表达式中需要包含约束的列,否则的话,约束就没有任何意义了。

You can also give the constraint a separate name. This clarifies error messages and allows you to refer to the constraint when you need to change it. The syntax is:

当然,也可以单独给约束定义个名字。这会在错误信息中很清晰,而且当你想更改约束的时候,可以有效的指向该约束。语法如下:

CREATE TABLE products (

product_no integer,

name text,

price numeric CONSTRAINT positive_price CHECK (price > 0)

);

So, to specify a named constraint, use the key word CONSTRAINT followed by an identifier followed by the constraint definition. (If you don't specify a constraint name in this way, the system chooses a name for you.)

因此,使用关键字CONSTRAINT后加标识符及约束定义来定义一个命名的约束。(如果没有明确定义约束名称,则系统会自动生成名称。)

A check constraint can also refer to several columns. Say you store a regular price and a discounted price, and you want to ensure that the discounted price is lower than the regular price:

检查约束也可涉及多列。假设表中有正常价格和打折价格,而且需要确保打折价格低于正常价格,则:

CREATE TABLE products (

product_no integer,

name text,

price numeric CHECK (price > 0),

discounted_price numeric CHECK (discounted_price > 0),

CHECK (price > discounted_price)

);

The first two constraints should look familiar. The third one uses a new syntax. It is not attached to a particular column, instead it appears as a separate item in the comma-separated column list. Column definitions and these constraint definitions can be listed in mixed order.

前两个约束看起来挺熟(刚开始讲过的语法)。第三个约束使用了个新的语法。它没有与特定的列绑定,而是在列的列表中以逗号分隔作为了单独的项目。列定义和这些约束定义的顺序可随意。

We say that the first two constraints are column constraints, whereas the third one is a table constraint because it is written separately from any one column definition. Column constraints can also be written as table constraints, while the reverse is not necessarily possible, since a column constraint is supposed to refer to only the column it is attached to. (PostgreSQL doesn't enforce that rule, but you should follow it if you want your table definitions to work with other database systems.) The above example could also be written as:

前两个约束称为列级约束,而第三个约束,因为它独立于列的定义而被称为表级约束。列级约束也可以写成表级约束的样子,但是反过来却不可行,因为列级约束仅应关联到它约束的列。(PostgreSQL并不强制遵守此规则,但是如果你想创建的表在其他数据库系统也可用,则最好遵守此规则。)上例也可写成:

CREATE TABLE products (

product_no integer,

name text,

price numeric,

CHECK (price > 0),

discounted_price numeric,

CHECK (discounted_price > 0),

CHECK (price > discounted_price)

);

or even:

或者:

CREATE TABLE products (

product_no integer,

name text,

price numeric CHECK (price > 0),

discounted_price numeric,

CHECK (discounted_price > 0 AND price > discounted_price)

);

It's a matter of taste.

这只是个人习惯的问题。

Names can be assigned to table constraints in the same way as column constraints:

表级约束也可像列级约束那样赋予名字:

CREATE TABLE products (

product_no integer,

name text,

price numeric,

CHECK (price > 0),

discounted_price numeric,

CHECK (discounted_price > 0),

CONSTRAINT valid_discount CHECK (price > discounted_price)

);

It should be noted that a check constraint is satisfied if the check expression evaluates to true or the null value. Since most expressions will evaluate to the null value if any operand is null, they will not prevent null values in the constrained columns. To ensure that a column does not contain null values, the not-null constraint described in the next section can be used.

需注意,如果检查表达式为真或为null值,则满足检查约束。因为大多数表达式在操作数为null时会求值为null,那么约束列中会可以存储null值。如果需要确保列中没有null值,则可以使用下节介绍的非空约束。

注:

PostgreSQL不支持引用非受检查的新(或更新)行的check约束。虽然违反此规定的CHECK约束在简单的场景下可用,但是却不能保证数据库一直满足此约束(特别是后续对检查行的修改)。这会导致数据库dump或reload失败。即使数据库中数据完全满足约束条件,也有可能会因为数据载入的顺序问题而导致无法满足约束而载入失败。如果可以,请使用UNIQUE、EXCLUDE或者FOREIGN KEY约束实现跨行或跨表的约束。

如果仅是想实现在数据插入时进行一次检查,那么可以考虑使用触发器实现。(此方式避免了dump和reload问题,因为pg_dump在载入数据之前都不会触发触发器。)

注:

PostgreSQL假定CHECK约束的条件是不变的,也即,对于同一输入行一定是相同结果的。此假设仅针对在出入或更新数据的CHECK检查。

但有个特例,就是CHECK约束限制中引用了用户自定义函数,而后面函数定义修改了。PostgreSQL不限制这种操作,但这种操作可能会导表中会有违反CHECK约束的行。同样会导致随后的dump或reload失败。推荐的解决方案是,首先使用ALTER TABLE删掉该CHECK约束,然后修订函数定义,重加约束,然后会重新检查表中所有数据。

mydb=# create table test_cons(id numeric,name varchar(100));

CREATE TABLE

mydb=# insert into test_cons values(1,'one');

INSERT 0 1

mydb=# alter table test_cons add constraint cons_chk check (id > 1);

ERROR:  check constraint "cons_chk" of relation "test_cons" is violated by some row

5.4.1. Check Constraints相关推荐

  1. oracle中check约束性别,关于Oracle Check类型约束的导入与启用

    今天有朋友问,Oracle的Check约束在indexfile中是否存在,导入后没有检查到. 我测试了一下,事实证明是可以的,Oracle的Check Constraints可以通过imp,使用ind ...

  2. 5.3. Constraints

    5.3. Constraints 5.3.约束 Data types are a way to limit the kind of data that can be stored in a table ...

  3. constraints

    1 .约束的类型 Table 5-1 Types of Constraints Constraint Type Description See Also NOT NULL Allows or disa ...

  4. ORACLE建表练习

    1,学生表 1 -- Create table 2 create table T_HQ_XS 3 ( 4 xueh VARCHAR2(10) not null, 5 xingm VARCHAR2(20 ...

  5. Oracle优化器:星型转换

    Oracle 8i中引入了星型转换(star transformation)的优化器新特性以便更有效地处理星型查询.星型查询语句多用于基于星型模型设计的数据仓库应用中.星型模型的称谓源于该种模型以图形 ...

  6. Django源码分析7:migrate命令的浅析

    django源码分析 本文环境python3.5.2,django1.10.x系列 django源码分析-migrate命令分析 Django项目中提供了,通过migrations操作数据库的结构的命 ...

  7. Azure SQL Database (19) Stretch Database 概览

    <Windows Azure Platform 系列文章目录>  Azure SQL Database (19) Stretch Database 概览      Azure SQL Da ...

  8. PostgreSQL从继承到分区(三)

    2019独角兽企业重金招聘Python工程师标准>>> 三.Pg_partman 3.1 介绍 pg_partman是基于PostgreSQL分区开发的一个分区表管理工具,通过多个引 ...

  9. android sqlite 中 创建表 不要使用 IF NOT EXISTS + TA...

    2019独角兽企业重金招聘Python工程师标准>>> android sqlite 中 创建表 不要使用 "IF NOT EXISTS " + TABLE_NA ...

  10. 《SQL Server 2012 T-SQL基础》读书笔记 - 1.背景

    几个缩写的全称:Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language ...

最新文章

  1. 一起聊聊好玩的Openresty
  2. http协议头信息详解
  3. 程序员面试题100题第17题——字符串转化为整数
  4. Bitmap 索引 vs. B-tree 索引:如何选择以及何时使用?——2-5
  5. 后处理安装_你所不了解的国六后处理
  6. C# 子类实例化基类 基类使用不了子类的方法_C#学习笔记09--构造方法/析构方法/继承/多态/封装...
  7. Cisco ASA 5500系列防火墙 Series Adaptive Security Appliances
  8. anaconda 怎么安装xlrd_Anaconda 安装 tensorflow 和 keras
  9. python最大约数是_python – 找到最大的公约数(赋值错误,我迫切需要你的帮助)
  10. 【java】java 并发编程 ArrayBlockingQueue
  11. 21世纪商业运作平台:云计算(Cloud Computing)
  12. oracle的rac环境,RAC环境数据库的备份
  13. php yield 个人小解_PHP5.5新特性之yield理解与用法实例分析
  14. 生信-使用NCBI进行目的基因的引物设计
  15. kubernetes 网络callico和flannel两种网络
  16. C++ 解析pcap文件
  17. 计算机ps屏幕背景颜色怎么换,win7系统ps背景颜色怎么换?七步教您轻松处理
  18. (离散)令R={m|m=a+b√2,a,b∈Q,+为普通加法},定义映射g:R→  R 为g(a+b√2)= a-b√2,试证:g是/R,+/到/R,+/的自同构映射
  19. My interest is the rules/ways to implement, go and insist with interest!!!
  20. 解决Chrome中打不开Google搜索结果链接

热门文章

  1. mysql宾馆客房管理系统视频_java swing mysql实现的酒店宾馆管理系统项目源码附带视频指导运行教程...
  2. 智鼎逻辑推理题及答案_PreTalent职场说|2020常见校招笔试题型解析
  3. 4_蒙特卡罗算法求圆周率PI
  4. [基于Python的微信公众号后台开发:1]配置对接阿里云服务器
  5. SAP系统环境整体架构设计总结
  6. SAP-SD-学习笔记0923
  7. ad软件画pcb方法总结_AD软件中导入BRD的PCB文件总结分享,,,,
  8. 【Windows】treamview完全卸载
  9. 有关于阿里云的历史-阿里云这群疯子
  10. [RK3399][Android7.1] 移植笔记 --- 音频Codec RT5640添加