postgresql----继承表INHERITS PARENT TABLE
来源:互联网 发布:天下霸唱抄袭证据知乎 编辑:程序博客网 时间:2024/05/29 20:05
使用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)
阅读全文
0 0
- postgresql----继承表INHERITS PARENT TABLE
- 02.Javascript中的继承----Inherits
- Inherits
- NodeJs通过util.inherits实现类继承
- node.js利用 inherits实现继承
- 【PostgreSQL-9.6.3】表继承
- Postgresql之无法删除表(drop table)
- VB.net 接口 Interface 与继承 Inherits(转载)
- Node.js的原型继承函数 util.inherits
- PostgreSQL(表的继承和分区)
- postgresql sql create table
- postgresql cluster table
- postgresql 通过数据字典建表,不用create TABLE
- PostgreSQL 动态表复制(CREATE TABLE...LIKE)
- single-table inheritance 单表继承
- PostgreSQL change unlogged table to logged table
- postgreSQL 的 dynamic hash table
- PostgreSQL View the table structure
- 多线程master-worker设计
- K-means算法、高斯混合模型 matlab
- 【程序员周边】程序员系列表情包
- BitmapUtil
- 驱动编程(一),NT
- postgresql----继承表INHERITS PARENT TABLE
- 小程序页面布局之Flexbox布局
- 8. 类型声明,方法,接口
- QQ第三方登录
- 用分层结构打造微 MVC 框架
- 关于Qt的QAxWidget
- PL/SQL 快捷键设计以及添加快捷键 .
- vue icon 图标库
- **Linux环境下部署WEB项目出现的BUG集锦**