postgresql----继承表INHERITS PARENT TABLE
2016-06-21 21:47
465 查看
使用INHERITS创建的新表会继承一个或多个父表,子表只会继承父表的表结构和NOT NULL,DEFAULT,CHECK三种约束,主键,外键和唯一键以及索引不会被继承,所以修改父表的结构(增删字段),NOT NULL,DEFAULT和CHECK约束会自动同步子表修改。
示例1.
示例2.
示例3.
除继承父表之外,创建子表时可以增加自己的字段
示例4.解除继承
示例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 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
示例2.
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
示例3.
除继承父表之外,创建子表时可以增加自己的字段
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
示例4.解除继承
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)
相关文章推荐
- 跟着SY速学Ubuntu系列三:Ubuntu16编译安装Memcached&&简单使用
- MongoDB应用详解
- Java操作MongoDB增删改查工具Demo
- MySql避免重复插入记录方法(ignore,Replace,ON DUPLICATE KEY UPDATE)
- redis -java客户端jedis
- Oracle用户、授权、角色管理
- MySQL 中处理 Null 时要注意两个陷阱
- MySQL 5.7新特性介绍
- 数据库视频总结(三)
- postgresql----TEMPORARY TABLE和UNLOGGED TABLE
- Memcached的基础
- MySQL主从数据库同步延迟问题解决(转)
- oracle lag与lead分析函数简介
- 如何把一个sql文件里的内容通过php代码执行
- 简单实现jdbc连接数据库通用工具类(代码)
- MySql 改变表中已有字段的长度
- MySql 为表中已有字段设置默认值
- python 写的sqli_lab显错式注入小代码
- sqli_lab
- 【MySQL】笔记