PostgreSQL的学习心得和知识总结(十)|PostgreSQL约束的修改创建和约束删除(结合系统表)

注:昨天我们已经基本上把PostgreSQL数据库的约束在创建表时的约束创建和查看约束过了一遍,今天我们着重关注于主键约束、外键约束、唯一约束和检查约束,看一下这四种重要约束的修改和删除操作。(结合PostgreSQL的系统表预先做好四种约束的启动和停止的准备)(作为对昨天内容的补充,约束的详细概念请见博客PostgreSQL的学习心得和知识总结(九)|PostgreSQL约束的定义创建及特性说明

主键约束

主键约束

主键是用于在表中唯一标识行的列或列组。从技术上讲,主键约束是非空约束和UNIQUE约束的组合效果。

1、使用列约束设置主键

--说明:只能设置一列作为主键,主键默认名称为tablename_pkey。
uxdb=# create table test(uid serial primary key,uname varchar(50),upwd varchar(40));
CREATE TABLE
uxdb=# \d test
                                    Table "public.test"
 Column |         Type          | Collation | Nullable |              Default              
--------+-----------------------+-----------+----------+-----------------------------------
 uid    | integer               |           | not null | nextval('test_uid_seq'::regclass)
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (uid)

如上,我们建立了一张表:表名test;并在字段uid建了主键(有了主键索引和主键约束)。
OK,我们下面来看一下数据库的系统表来得到一些我们想要的信息:

uxdb=# select * from ux_tables where tablename = 'test';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test      | uxdb       |            | t          | f        | f           | f
(1 row)

uxdb=# select oid from ux_class where relname='test';
  oid  
-------
 16386
(1 row)

uxdb=# select  contype,conname,conrelid  from ux_constraint where conrelid=16386;
 contype |  conname  | conrelid 
---------+-----------+----------
 p       | test_pkey |    16386
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16386;
 contype |  conname  | conrelid | conindid 
---------+-----------+----------+----------
 p       | test_pkey |    16386 |    16390
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16386;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16390 |        1 | t           | t            | t          | t
(1 row)

uxdb=#
  • 表的oid为16386
  • 从约束表ux_constraint里面得到约束名字叫做test_pkey;类型是p 就是主键索引;索引oid是16390
  • 从索引表ux_index里面得到(依次):索引oid、索引作用的列数、是唯一索引、是主键索引、索引对查询有效、索引还活着

2、使用表级约束设置主键

--说明:使用[表]级约束设置主键,可以设置一列或多列作为主键,主键默认名称为tablename_pkey,constraint myp_key_test2可省略。
uxdb=# create table test2(uid serial ,uname varchar(50),upwd varchar(40),constraint myp_key_test2 primary key(uid));
CREATE TABLE
uxdb=# \d test2
                                    Table "public.test2"
 Column |         Type          | Collation | Nullable |              Default               
--------+-----------------------+-----------+----------+------------------------------------
 uid    | integer               |           | not null | nextval('test2_uid_seq'::regclass)
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "myp_key_test2" PRIMARY KEY, btree (uid)

uxdb=# select * from ux_tables where tablename = 'test2';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test2     | uxdb       |            | t          | f        | f           | f
(1 row)

uxdb=# select oid from ux_class where relname='test2';
  oid  
-------
 16394
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16394;
 contype |    conname    | conrelid | conindid 
---------+---------------+----------+----------
 p       | myp_key_test2 |    16394 |    16398
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16394;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16398 |        1 | t           | t            | t          | t
(1 row)

uxdb=#

--信息基本上和上面一样

3、通过修改表结构设置主键

--语法:alter table table_name add [constraint constraint_name] primary key(column_1, column_2);

--说明:通过修改表结构设置主键,可以设置一列或多列作为主键,可以指定主键名称。
uxdb=# create table test3(uid serial ,uname varchar(50),upwd varchar(40));
CREATE TABLE
uxdb=# \d test3
                                    Table "public.test3"
 Column |         Type          | Collation | Nullable |              Default               
--------+-----------------------+-----------+----------+------------------------------------
 uid    | integer               |           | not null | nextval('test3_uid_seq'::regclass)
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 

uxdb=# alter table test3 add constraint myp_key_test3 primary key(uid);
ALTER TABLE
uxdb=# \d test3
                                    Table "public.test3"
 Column |         Type          | Collation | Nullable |              Default               
--------+-----------------------+-----------+----------+------------------------------------
 uid    | integer               |           | not null | nextval('test3_uid_seq'::regclass)
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "myp_key_test3" PRIMARY KEY, btree (uid)

uxdb=# select * from ux_tables where tablename = 'test3';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test3     | uxdb       |            | t          | f        | f           | f
(1 row)

uxdb=# select oid from ux_class where relname='test3';
  oid  
-------
 16402
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16402;
 contype |    conname    | conrelid | conindid 
---------+---------------+----------+----------
 p       | myp_key_test3 |    16402 |    16406
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16402;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16406 |        1 | t           | t            | t          | t
(1 row)

uxdb=#
--这样的方式等同于把前面(一次创建OK)拆分了(分为先后两步)

4、往已有表添加自增的主键字段

--创建没有任何主键的表
uxdb=# create table test4(uname varchar(50),upwd varchar(40));
CREATE TABLE
uxdb=# insert into test4 values ("song1",123);
ERROR:  column "song1" does not exist
LINE 1: insert into test4 values ("song1",123);
                                  ^
uxdb=# insert into test4 values ('song1','123');
INSERT 0 1
uxdb=# insert into test4 values ('song2','456');
INSERT 0 1
uxdb=# select * from test4;
 uname | upwd 
-------+------
 song1 | 123
 song2 | 456
(2 rows)
--我们要添加一个名为uid的自增主键到test4表
uxdb=# alter table test4 add column "uid" serial primary key;
ALTER TABLE
uxdb=# select * from test4;
 uname | upwd | uid 
-------+------+-----
 song1 | 123  |   1
 song2 | 456  |   2
(2 rows)

uxdb=# select oid from ux_class where relname='test4';
  oid  
-------
 16408
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16408;
 contype |  conname   | conrelid | conindid 
---------+------------+----------+----------
 p       | test4_pkey |    16408 |    16414
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16408;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16414 |        1 | t           | t            | t          | t
(1 row)

uxdb=# \d test4
                                    Table "public.test4"
 Column |         Type          | Collation | Nullable |              Default               
--------+-----------------------+-----------+----------+------------------------------------
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
 uid    | integer               |           | not null | nextval('test4_uid_seq'::regclass)
Indexes:
    "test4_pkey" PRIMARY KEY, btree (uid)

uxdb=#
--和上面几种大同小异

5、删除主键约束

uxdb=# \d test4
                                    Table "public.test4"
 Column |         Type          | Collation | Nullable |              Default               
--------+-----------------------+-----------+----------+------------------------------------
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
 uid    | integer               |           | not null | nextval('test4_uid_seq'::regclass)
Indexes:
    "test4_pkey" PRIMARY KEY, btree (uid)

uxdb=# alter table test4 drop constraint test4_pkey;
ALTER TABLE
uxdb=# \d test4
                                    Table "public.test4"
 Column |         Type          | Collation | Nullable |              Default               
--------+-----------------------+-----------+----------+------------------------------------
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
 uid    | integer               |           | not null | nextval('test4_uid_seq'::regclass)

uxdb=# select * from test4;
 uname | upwd | uid 
-------+------+-----
 song1 | 123  |   1
 song2 | 456  |   2
(2 rows)

uxdb=# insert into test4 values ('song3','666',1);
INSERT 0 1
uxdb=# select * from test4;
 uname | upwd | uid 
-------+------+-----
 song1 | 123  |   1
 song2 | 456  |   2
 song3 | 666  |   1
(3 rows)

uxdb=#
uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16408;
 contype | conname | conrelid | conindid 
---------+---------+----------+----------
(0 rows)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16408;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select oid from ux_class where relname='test4';
  oid  
-------
 16408
(1 row)

uxdb=#
--删除主键约束之后,表结构就没有什么索引和约束了

外键约束

外键约束

外键约束主要是用来维护子表(引用表)和父表(被引用表)之间的引用完整性。

1、使用列约束设置外键

uxdb=# create table test(uid int,uname varchar(50),upwd varchar(40),primary key(uid));
CREATE TABLE
uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           | not null | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (uid)

--说明:外键默认名称为tablename_columnname_fkey
uxdb=# create table test_info(uid int primary key references test,uname varchar(50),upwd varchar(40));
CREATE TABLE
uxdb=# \d test_
test_info       test_info_pkey  test_pkey       
uxdb=# \d test_info_pkey 
 Index "public.test_info_pkey"
 Column |  Type   | Definition 
--------+---------+------------
 uid    | integer | uid
primary key, btree, for table "public.test_info"

uxdb=# \d test_info
                    Table "public.test_info"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           | not null | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_info_pkey" PRIMARY KEY, btree (uid)
Foreign-key constraints:
    "test_info_uid_fkey" FOREIGN KEY (uid) REFERENCES test(uid)

uxdb=# \d test_pkey 
   Index "public.test_pkey"
 Column |  Type   | Definition 
--------+---------+------------
 uid    | integer | uid
primary key, btree, for table "public.test"

uxdb=#

下面先看一下父表的结构:

uxdb=# select * from ux_tables where tablename = 'test';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test      | uxdb       |            | t          | f        | t           | f
(1 row)

uxdb=# select oid from ux_class where relname='test';
  oid  
-------
 16384
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16384;
 contype |  conname  | conrelid | conindid 
---------+-----------+----------+----------
 p       | test_pkey |    16384 |    16387
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16384;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16387 |        1 | t           | t            | t          | t
(1 row)

再看一下子表的结构:

uxdb=# select * from ux_tables where tablename = 'test_info';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test_info | uxdb       |            | t          | f        | t           | f
(1 row)

uxdb=# select oid from ux_class where relname='test_info';
  oid  
-------
 16389
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16389;
 contype |      conname       | conrelid | conindid 
---------+--------------------+----------+----------
 p       | test_info_pkey     |    16389 |    16392
 f       | test_info_uid_fkey |    16389 |    16387
(2 rows)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16389;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16392 |        1 | t           | t            | t          | t
(1 row)

uxdb=#
uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16389;
            tgname            | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
------------------------------+--------+--------+-----------+--------------+---------------+---------------+--------------
 RI_ConstraintTrigger_c_16397 |   1644 |      5 | O         | t            |         16384 |         16387 |        16394
 RI_ConstraintTrigger_c_16398 |   1645 |     17 | O         | t            |         16384 |         16387 |        16394
(2 rows)

2、使用表约束设置外键

--说明:外键默认名称为tablename_columnname_fkey
uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           | not null | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (uid)

uxdb=# create table test_info(uid int,uname varchar(50),upwd varchar(40),foreign key(uid) references test);
CREATE TABLE
uxdb=# \d test_info
                    Table "public.test_info"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Foreign-key constraints:
    "test_info_uid_fkey" FOREIGN KEY (uid) REFERENCES test(uid)

uxdb=#

此时的子表只有一个外键(关联父表的主键)(没有什么索引),其结构如下:

uxdb=# select oid from ux_class where relname='test_info';
  oid  
-------
 16399
(1 row)

uxdb=# select * from ux_tables where tablename = 'test_info';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test_info | uxdb       |            | f          | f        | t           | f
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16399;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16399;
            tgname            | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
------------------------------+--------+--------+-----------+--------------+---------------+---------------+--------------
 RI_ConstraintTrigger_c_16405 |   1644 |      5 | O         | t            |         16384 |         16387 |        16402
 RI_ConstraintTrigger_c_16406 |   1645 |     17 | O         | t            |         16384 |         16387 |        16402
(2 rows)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16399;
 contype |      conname       | conrelid | conindid 
---------+--------------------+----------+----------
 f       | test_info_uid_fkey |    16399 |    16387
(1 row)

uxdb=# O o 0

注:我们可以看到对一个表创建外键约束时,其内部实现是对外键相关的两个表创建触发器(为外键创建的 ),然后在更新表时触发触发器去进行外键的检测。

触发器系统表里面的oid=16384 是指test表:

uxdb=# select oid from ux_class where relname='test';
  oid  
-------
 16384
(1 row)

而子表里面是没有支持什么索引的,但是在触发器系统表里面出现的字段tgconstrindid =16387是指的是父表的主键索引;

uxdb=# select oid from ux_class where relname='test';
  oid  
-------
 16384
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16384;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16387 |        1 | t           | t            | t          | t
(1 row)

uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           | not null | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (uid)
Referenced by:
    TABLE "test_info" CONSTRAINT "test_info_uid_fkey" FOREIGN KEY (uid) REFERENCES test(uid)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16384;
 contype |  conname  | conrelid | conindid 
---------+-----------+----------+----------
 p       | test_pkey |    16384 |    16387
(1 row)

uxdb=#

3、通过修改表结构设置外键

--语法:alter table table_name add [constraint constraint_name] foreign key(column_1) references TableName(ColumnName);

uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           | not null | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (uid)

uxdb=# create table test_info(uid int,uname varchar(50),upwd varchar(40));
CREATE TABLE
uxdb=# \d test_info 
                    Table "public.test_info"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
uxdb=# select oid from ux_class where relname='test_info';
  oid  
-------
 16407
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16407;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16407;
 contype | conname | conrelid | conindid 
---------+---------+----------+----------
(0 rows)

--说明:通过修改表结构设置外键,可以指定外键名称。
uxdb=# alter table test_info add constraint myfkey foreign key(uid) references test;
ALTER TABLE
uxdb=# \d test_info 
                    Table "public.test_info"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Foreign-key constraints:
    "myfkey" FOREIGN KEY (uid) REFERENCES test(uid)

uxdb=#
uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16407;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16407;
 contype | conname | conrelid | conindid 
---------+---------+----------+----------
 f       | myfkey  |    16407 |    16387
(1 row)

uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16407;
            tgname            | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
------------------------------+--------+--------+-----------+--------------+---------------+---------------+--------------
 RI_ConstraintTrigger_c_16418 |   1644 |      5 | O         | t            |         16384 |         16387 |        16415
 RI_ConstraintTrigger_c_16419 |   1645 |     17 | O         | t            |         16384 |         16387 |        16415
(2 rows)

uxdb=#
--这个和上面大同小异

4、删除外键约束(同删除其他约束一样,使用同一语法

uxdb=# \d test_info 
                    Table "public.test_info"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Foreign-key constraints:
    "myfkey" FOREIGN KEY (uid) REFERENCES test(uid)

uxdb=# alter table test_info drop constraint myfkey ;
ALTER TABLE
uxdb=# \d test_info 
                    Table "public.test_info"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 

uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16407;
 tgname | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
--------+--------+--------+-----------+--------------+---------------+---------------+--------------
(0 rows)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16407;
 contype | conname | conrelid | conindid 
---------+---------+----------+----------
(0 rows)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16407;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=#

唯一约束

这个可以参见主键约束!(有相似之处,但是也有些许区别)
所谓唯一约束:确保[列]或[列组]中的值在表中是唯一的。

唯一约束

1、使用列约束设置唯一约束

--生成2个列的唯一约束test_uid_key 
uxdb=# create table test(uid int unique ,uname varchar(50),upwd varchar(40));
CREATE TABLE
uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_uid_key" UNIQUE CONSTRAINT, btree (uid)

uxdb=#

看看系统表结构显示:

uxdb=# create table test(uid int unique ,uname varchar(50),upwd varchar(40));
CREATE TABLE
uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_uid_key" UNIQUE CONSTRAINT, btree (uid)

uxdb=# select oid from ux_class where relname='test';
  oid  
-------
 16420
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16420;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
      16423 |        1 | t           | f            | t          | t
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16420;
 contype |   conname    | conrelid | conindid 
---------+--------------+----------+----------
 u       | test_uid_key |    16420 |    16423
(1 row)

uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16420;
 tgname | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
--------+--------+--------+-----------+--------------+---------------+---------------+--------------
(0 rows)

uxdb=# select * from ux_tables where tablename = 'test';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test      | uxdb       |            | t          | f        | f           | f
(1 row)

uxdb=#
--只有一个index(唯一性索引)

2、使用表级约束设置唯一约束
3、通过修改表结构设置唯一约束

道理差不多,就不写了

4、删除约束

uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 
Indexes:
    "test_uid_key" UNIQUE CONSTRAINT, btree (uid)

uxdb=# alter table test drop constraint test_uid_key;
ALTER TABLE
uxdb=# \d test
                       Table "public.test"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 uid    | integer               |           |          | 
 uname  | character varying(50) |           |          | 
 upwd   | character varying(40) |           |          | 

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16420;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16420;
 contype | conname | conrelid | conindid 
---------+---------+----------+----------
(0 rows)

uxdb=#

检查约束

所谓检查约束:基于布尔表达式约束表中列的值。

检查约束

1、使用列约束设置检查约束

uxdb=# create table test(uid int check(uid>0));
CREATE TABLE
uxdb=# \d test
                Table "public.test"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 uid    | integer |           |          | 
Check constraints:
    "test_uid_check" CHECK (uid > 0)

uxdb=# select * from ux_tables where tablename = 'test';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test      | uxdb       |            | f          | f        | f           | f
(1 row)

uxdb=# select oid from ux_class where relname='test';
  oid  
-------
 16425
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16425;
 contype |    conname     | conrelid | conindid 
---------+----------------+----------+----------
 c       | test_uid_check |    16425 |        0
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16425;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16425;
 tgname | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
--------+--------+--------+-----------+--------------+---------------+---------------+--------------
(0 rows)

uxdb=#

2、使用表约束设置检查约束

3、通过修改表结构设置检查约束

uxdb=# create table test(uid int);
CREATE TABLE
uxdb=# \d test 
                Table "public.test"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 uid    | integer |           |          | 

uxdb=# select * from ux_tables where tablename = 'test';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | test      | uxdb       |            | f          | f        | f           | f
(1 row)

uxdb=# select oid from ux_class where relname='test';
  oid  
-------
 16429
(1 row)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16429;
 contype | conname | conrelid | conindid 
---------+---------+----------+----------
(0 rows)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16429;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16429;
 tgname | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
--------+--------+--------+-----------+--------------+---------------+---------------+--------------
(0 rows)

uxdb=# alter table test add constraint mycheck check(uid>0);
ALTER TABLE
uxdb=# \d test 
                Table "public.test"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 uid    | integer |           |          | 
Check constraints:
    "mycheck" CHECK (uid > 0)

uxdb=# select  contype,conname,conrelid,conindid  from ux_constraint where conrelid=16429;
 contype | conname | conrelid | conindid 
---------+---------+----------+----------
 c       | mycheck |    16429 |        0
(1 row)

uxdb=# select indexrelid,indnatts,indisunique,indisprimary,indisvalid,indislive from ux_index where indrelid=16429;
 indexrelid | indnatts | indisunique | indisprimary | indisvalid | indislive 
------------+----------+-------------+--------------+------------+-----------
(0 rows)

uxdb=# select tgname,tgfoid,tgtype,tgenabled,tgisinternal,tgconstrrelid,tgconstrindid,tgconstraint from ux_trigger where tgrelid=16429;
 tgname | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint 
--------+--------+--------+-----------+--------------+---------------+---------------+--------------
(0 rows)

uxdb=#

猜你喜欢

转载自blog.csdn.net/weixin_43949535/article/details/106540614