ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL:表继承

PostgreSQL:表继承

PostgreSQL 作者:Ryan_Bai 时间:2020-12-14 15:01:49 0 删除 编辑

使用 INHERITS 创建的新表会继承一个或多个父表,子表只会继承父表的表结构和 NOT NULL,DEFAULT,CHECK 三种约束,主键,外键和唯一键以及索引不会被继承,所以修改父表的结构(增删字段),NOT NULL,DEFAULT 和 CHECK 约束会自动同步子表修改。

示例

创建父表,用于测试

create table tbl_inherits_parent(
    a int not null,
    b varchar(32) not null default 'Got u',
    c int check (c > 0),
    d date not null
);

示例一

创建继承表,查看索引继承

test=# alter table tbl_inherits_parent add constraint pk_tbl_inherits_parent_a primary key(a);
ALTER TABLE
 
test=# alter table tbl_inherits_parent add constraint uk_tbl_inherits_parent_b_d unique (b,d);
ALTER TABLE
 
test=# create table tbl_inherits_partition() inherits (tbl_inherits_parent);
CREATE TABLE
 
test=# \d tbl_inherits_partition 
                    Table "public.tbl_inherits_partition"
 Column |         Type          |                  Modifiers                  
--------+-----------------------+---------------------------------------------
 a      | integer               | not null
 b      | character varying(32) | not null default 'Got u'::character varying
 c      | integer               | 
 d      | date                  | not null
Check constraints:
    "tbl_inherits_parent_c_check" CHECK (c > 0)
Inherits: tbl_inherits_parent

示例二

查看列属性继承

test=# alter table tbl_inherits_parent add column e int not null default 0;
ALTER TABLE
 
test=# alter table tbl_inherits_parent alter column b set default 'try me';
ALTER TABLE
 
test=# \d tbl_inherits_partition 
                     Table "public.tbl_inherits_partition"
 Column |         Type          |                  Modifiers                   
--------+-----------------------+----------------------------------------------
 a      | integer               | not null
 b      | character varying(32) | not null default 'try me'::character varying
 c      | integer               | 
 d      | date                  | not null
 e      | integer               | not null default 0
Check constraints:
    "tbl_inherits_parent_c_check" CHECK (c > 0)
Inherits: tbl_inherits_parent

示例三

除继承父表之外,创建子表时可以增加自己的字段

test=# create table tbl_inherits_partition1(f int) inherits (tbl_inherits_parent);
CREATE TABLE
 
test=# \d tbl_inherits_partition1 
                    Table "public.tbl_inherits_partition1"
 Column |         Type          |                  Modifiers                   
--------+-----------------------+----------------------------------------------
 a      | integer               | not null
 b      | character varying(32) | not null default 'try me'::character varying
 c      | integer               | 
 d      | date                  | not null
 e      | integer               | not null default 0
 f      | integer               | 
Check constraints:
    "tbl_inherits_parent_c_check" CHECK (c > 0)
Inherits: tbl_inherits_parent

示例四

解除继承

test=# alter table tbl_inherits_partition1 no inherit tbl_inherits_parent;
ALTER TABLE
 
test=# \d tbl_inherits_partition1 
                    Table "public.tbl_inherits_partition1"
 Column |         Type          |                  Modifiers                   
--------+-----------------------+----------------------------------------------
 a      | integer               | not null
 b      | character varying(32) | not null default 'try me'::character varying
 c      | integer               | 
 d      | date                  | not null
 e      | integer               | not null default 0
 f      | integer               | 
Check constraints:
    "tbl_inherits_parent_c_check" CHECK (c > 0)


转载:https://www.cnblogs.com/alianbog/p/5605129.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31490526/viewspace-2742055/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: PostgreSQL:临时表
请登录后发表评论 登录
全部评论
Oracle ACE Pro; PostgreSQL ACE; Oracle 10g OCE、OCA、OCP; Oracle 11g OCP、OCM; MySQL 5.6 OCP; Oracle、MySQL OCP 讲师; PostgreSQL PGCE 获得者;

注册时间:2017-09-18

  • 博文量
    347
  • 访问量
    730416