postgresql----数据库表约束----NOT NULL,DEFAULT,CHECK
数据库表有NOT NULL,DEFAULT,CHECK,UNIQUE,PRIMARY KEY,FOREIGN KEY六种约束。
一、NOT NULL ---- 非空约束
NULL表示没有数据,不表示具体的数值,所以在数据库中NULL是不等于NULL的。判断表中的一个单元格是不是NULL使用的是IS NULL或者IS NOT NULL,而不是=NULL或者!=NULL,当一个字段设置NOT NULL约束后,INSERT时必须给该字段赋值,否则拒绝写入。在一些程序语言(如C)查询结果中出现NULL有可能会直接作为空指针,如果使用不当,会直接导致程序崩溃。所以一个字段要尽可能的设置NOT NULL约束,或者DEFAULT约束,当然OUTER JOIN的结果也有可能引入NULL,所以开发过程中要尽可能的做好保护。
1.设置NOT NULL约束的字段INSERT必须赋值,没有NOT NULL约束的字段INSERT没有赋值,会自动填充NULL。
/*
postgres=# create database test with template = template0 encoding='UTF8' lc_collate='C' lc_ctype='C';
CREATE DATABASE
postgres=#
postgres=#
postgres=#
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table tbl_null (a int not null,b varchar(12));
CREATE TABLE
test=# insert into tbl_null (a,b) values(1,'1');
INSERT 0 1
test=# insert into tbl_null (a) values(2);
INSERT 0 1
test=# insert into tbl_null (b) values('3');
ERROR: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null, 3).
test=# select * from tbl_null;
a | b
---+---
1 | 1
2 |
(2 rows)
*/
2.NOT NULL约束增加
已存在的字段设置NOT NULL约束前必须先删除为NULL的数据行。
/*
test=# alter table tbl_null alter COLUMN b set not null;
ERROR: column "b" contains null values
test=# delete from tbl_null where b is null;
DELETE 1
test=# alter table tbl_null alter COLUMN b set not null;
ALTER TABLE
test=# \d tbl_null
Table "public.tbl_null"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer | not null
b | character varying(12) | not null test=# select * from tbl_null ;
a | b
---+---
1 | 1
(1 row)
*/
3.删除NOT NULL约束
/*
test=# alter table tbl_null alter COLUMN b drop not null;
ALTER TABLE
test=# \d tbl_null
Table "public.tbl_null"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer | not null
b | character varying(12) |
*/
二、DEFAULT ---- 默认值
INSERT没有赋值的字段默认填充NULL(前提是该字段没有NOT NULL约束),设置DEFAULT默认值,INSERT没有赋值会默认填充该默认值。尤其是设置NOT NULL约束的字段,如果给定一个DEFAULT约束,即使INSERT没有给字段赋值也不会出错。
1.设置DEFAULT约束,既可以在创建表时直接设置,也可以在创建表后修改字段,字段新增默认值约束可以不用考虑已有数据。
/*
test=# create table tbl_default(a int not null,b varchar(12) not null default 'try me');
CREATE TABLE
test=# \d tbl_default
Table "public.tbl_default"
Column | Type | Modifiers
--------+-----------------------+----------------------------------------------
a | integer | not null
b | character varying(12) | not null default 'try me'::character varying test=# drop table tbl_default ;
DROP TABLE
test=# create table tbl_default(a int not null,b varchar(12) not null);
CREATE TABLE
test=# alter table tbl_default alter COLUMN b set default 'try me';
ALTER TABLE
test=# \d tbl_default
Table "public.tbl_default"
Column | Type | Modifiers
--------+-----------------------+----------------------------------------------
a | integer | not null
b | character varying(12) | not null default 'try me'::character varying
*/
2.INSERT时赋值使用赋值填充,否则使用默认值填充。
/*
test=# insert into tbl_default (a,b) values(1,'aloha');
INSERT 0 1
test=# insert into tbl_default (a) values(2);
INSERT 0 1
test=# select * from tbl_default ;
a | b
---+--------
1 | aloha
2 | try me
(2 rows)
*/
3.默认值约束的修改与删除,修改默认值直接新设置一个默认值即可。
/*
test=# alter table tbl_default alter COLUMN b set default 'my god';
ALTER TABLE
test=# \d tbl_default
Table "public.tbl_default"
Column | Type | Modifiers
--------+-----------------------+----------------------------------------------
a | integer | not null
b | character varying(12) | not null default 'my god'::character varying test=# alter table tbl_default alter COLUMN b drop default;
ALTER TABLE
test=# \d tbl_default
Table "public.tbl_default"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer | not null
b | character varying(12) | not null */
三、CHECK ---- 检查约束
INSERT,UPDATE时检查字段值是否满足CHECK条件,若不满足则拒绝写入。
1.CHECK约束的设置
/*
test=# create table tbl_check(a int not null check (a>0),b varchar(12) not null check (b in ('ab','Ab','aB','AB')));
CREATE TABLE
test=# drop table tbl_check ;
DROP TABLE
test=# create table tbl_check
test-# (
test(# a int not null,
test(# b varchar(12) not null,
test(# constraint ck_tbl_check_a check (a > 0),
test(# constraint ck_tbl_check_b check (b in ('ab','aB','Ab','AB'))
test(# );
CREATE TABLE
test=# create table tbl_check
(
a int not null,
b varchar(12) not null);
CREATE TABLE
test=# alter table tbl_check add constraint ck_tbl_check_a check (a > 0);
ALTER TABLE
test=# alter table tbl_check add constraint ck_tbl_check_b check (b in ('ab','aB','Ab','AB'));
ALTER TABLE
test=# \d tbl_check
Table "public.tbl_check"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer | not null
b | character varying(12) | not null
Check constraints:
"ck_tbl_check_a" CHECK (a > 0)
"ck_tbl_check_b" CHECK (b::text = ANY (ARRAY['ab'::character varying, 'aB'::character varying, 'Ab'::character varying, 'AB'::character varying]::text[]))
*/
2.以上表tbl_check为例,INSERT时a的值必须是大于0的整数,b的值只能在'ab','aB','Ab','AB'范围内。
/*
test=# insert into tbl_check (a,b) values(1,'ab');
INSERT 0 1
test=# insert into tbl_check (a,b) values(-1,'ab');
ERROR: new row for relation "tbl_check" violates check constraint "ck_tbl_check_a"
DETAIL: Failing row contains (-1, ab).
test=# insert into tbl_check (a,b) values(1,'ac');
ERROR: new row for relation "tbl_check" violates check constraint "ck_tbl_check_b"
DETAIL: Failing row contains (1, ac).
*/
3.CHECK约束的删除
/*
test=# alter table tbl_check drop constraint ck_tbl_check_a;
ALTER TABLE
test=# insert into tbl_check (a,b) values(-1,'ab');
INSERT 0 1 */
4.CHECK约束的增加
新增CHECK约束必须首先删除已存在的不满足约束的数据
/*
test=# alter table tbl_check add constraint ck_tbl_check_a check (a > 0);
ERROR: check constraint "ck_tbl_check_a" is violated by some row
test=# delete from tbl_check where a <= 0;
DELETE 1
test=# alter table tbl_check add constraint ck_tbl_check_a check (a > 0);
ALTER TABLE
*/
postgresql----数据库表约束----NOT NULL,DEFAULT,CHECK的更多相关文章
- 关于数据库主从表、主键PRIMARY KEY 外键约束 FOREIGN KEY 约束----NOT NULL,DEFAULT,CHECK
如果由两个列共同组成主键,而且一个子表将主键作为可为空值的外键来继承,就可能得到错误的数据.可在一个外键列中插入有效的值,但在另一个外键列中插入空值.然后,可添加一个数据表检查约束,在可为空的外键中检 ...
- postgresql数据库primary key约束/not null约束/unique约束及default值的添加与删除、列的新增/删除/重命名/数据类型的更改
如果在建表时没有加primary key约束.not null约束.unique约束.default值,而是创建完表之后在某个字段添加的话 1.primary key约束的添加与删除 给red_pac ...
- PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库
最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...
- 访问GitLab的PostgreSQL数据库,查询、修改、替换等操作
1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...
- 访问GitLab的PostgreSQL数据库-(3)
1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...
- 访问GitLab的PostgreSQL数据库
1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...
- postgresql 数据库,模式,表空间的关系
数据库与模式模式(schema)是对数据库(database)逻辑分割在数据库创建的同时,就已经默认为数据库创建了一个模式--public,这也是该数据库的默认模式.所有为此数据库创建的对象(表.函数 ...
- Solr 4.4.0利用dataimporthandler导入postgresql数据库表
将数据库edbstore的edbtore schema下的customers表导入到solr 1. 首先查看customers表字段信息 edbstore=> \d customers Tabl ...
- Oracle数据库迁移至PostgreSQL数据库问题及解决
Oracle数据库迁移PostgreSQL数据库问题及解决 目录 如何计划迁移数据库(现状及问题分析) 统计系统表及表功能 解耦公共表 建立数据库 迁移表结构 导入表数据 改SQL语法 保证数据时效性 ...
随机推荐
- WCF(二)
摘自:http://www.cnblogs.com/yank/p/3666271.html WCF入门教程(二)从零做起-创建WCF服务 通过最基本的操作看到最简单的WCF如何实现的.这是VS的SDK ...
- 不可在 for 循环体内修改循环变量,防止 for 循环失去控制
不可在 for 循环体内修改循环变量,防止 for 循环失去控制. #include <iostream> /* run this program using the console pa ...
- erlang随机数问题
一.计算机的随机数的老问题,伪随机数. random:seed() random:uniform(N) 如果seed是相同的,则第M次执行 random:uniform(N) .M.N相同,则得到的随 ...
- ueditor1.4.3配置过程(包含单独上传文件以及图片的使用),ueditor1.4.3上传配置(转 http://www.bkjia.com/webzh/1001016.html)
这里使用的是ueditor1.4.3的jsp版本的UTF-8版本. 首先下载相应的ueditor,将ueditor文件夹直接拷贝到项目中,文件结构如下所示: 然后将项目要用的jar包导入到lib目录下 ...
- Jmeter零起点学习
什么是JMeter Apache JMeter是一个开源的Java桌面软件.设计的目的就是进行C/S架构软件的负载测试.随着发展,有很多人也用来进行一些静态资源或者动态资源的性能测试.可以支持的测 ...
- html5实现刮刮卡效果
通过Canvas实现的可刮涂层效果. 修改img.src时涂层也会自动适应新图片的尺寸. 修改layer函数可更改涂层样式. 涂层: 可刮效果: 以下是HTML源代码(已增加移动设备支持): 1 2 ...
- php中一些常用的语句收集
清空数据表 truncate 表名; http://blog.knowsky.com/234205.htm 常用的SQL语句实例 http://blog.csdn.net/vericlong ...
- mysql中,root用户密码被遗忘,该如何进行重置?
需求描述: 在mysql的测试环境中,有时候会遇到一段时间之后root用户的密码被遗忘的情况, 这个时候,就是需要对root密码进行重置,不过,在生产环境中,这种情况还是很少见. 环境描述: 操作系统 ...
- UVA 1341 - Different Digits(数论)
UVA 1341 - Different Digits 题目链接 题意:给定一个正整数n.求一个kn使得kn上用的数字最少.假设同样,则输出值最小的 思路: 首先利用鸽笼原理证明最多须要2个数字去组成 ...
- Extjs学习笔记--(三,调试技巧)
FireFox 1.firedebug(略) 2.illuminations 在illuminations页面可也看到缩写的extjs的代码,同时可以进行相应的调试 3,Firedebug AutoC ...