博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
postgresql----继承表INHERITS PARENT TABLE
阅读量:7003 次
发布时间:2019-06-27

本文共 3360 字,大约阅读时间需要 11 分钟。

 

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

示例1.

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 TABLEtest=# alter table tbl_inherits_parent add constraint uk_tbl_inherits_parent_b_d unique (b,d);ALTER TABLEtest=# create table tbl_inherits_partition() inherits (tbl_inherits_parent);CREATE TABLEtest=# \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 nullCheck constraints:    "tbl_inherits_parent_c_check" CHECK (c > 0)Inherits: tbl_inherits_parent

 

示例2.

test=# alter table tbl_inherits_parent add column e int not null default 0;ALTER TABLEtest=# alter table tbl_inherits_parent alter column b set default 'try me';ALTER TABLEtest=# \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 0Check constraints:    "tbl_inherits_parent_c_check" CHECK (c > 0)Inherits: tbl_inherits_parent

 

示例3.

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

test=# create table tbl_inherits_partition1(f int) inherits (tbl_inherits_parent);CREATE TABLEtest=# \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

 

示例4.解除继承

test=# alter table tbl_inherits_partition1 no inherit tbl_inherits_parent;ALTER TABLEtest=# \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

你可能感兴趣的文章
激励自己的话
查看>>
IOS 实现界面本地化(国际化)
查看>>
陶哲轩实分析命题 11.10.7
查看>>
《陶哲轩实分析》引理17.2.4证明_导数的唯一性
查看>>
站立会议5
查看>>
python中的常用模块(2)
查看>>
登陆的键盘敲击事件
查看>>
执行计划基础 统计信息
查看>>
python MD5加密方法
查看>>
mysql连接jdbc查询代码
查看>>
SpringMVC10数据验证
查看>>
处理异常Error resolving template [/login], template might not exist or might not be accessible by......
查看>>
洛谷 P1147 连续自然数和 Label:等差数列
查看>>
线程间的同步和通信机制
查看>>
Python脚本实现值更新事件赋值过程记录日志监控
查看>>
[bzoj 1503][NOI 2004]郁闷的出纳员
查看>>
Java课程上机实验1_ConnectionManager
查看>>
node.js中通过dgram数据报模块创建UDP服务器和客户端
查看>>
FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283
查看>>
外痔田螺用法
查看>>