postgresql----继承表INHERITS PARENT TABLE
使用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 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)
postgresql----继承表INHERITS PARENT TABLE的更多相关文章
- sql:sql server,MySQL,PostgreSQL的表,视图,存储过程结构查询
sql server 2005: --SQL SERVER 2005 生成代码需要知道的SQL语句 use LibrarySystem --查询当前数据库所有表和其的主键字段,字段类型,长度,是否为空 ...
- mybatis使用注解往postgresql数据库表insert数据[主键自增]的写法
建表SQL: DROP TABLE IF EXISTS person; CREATE TABLE person( person_id serial PRIMARY KEY NOT NULL, pers ...
- PostgreSQL创建表及约束
创建表 语法: create table table_name ( column_name type column_constraint, table_constraint table_constra ...
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
Oracle创建表语句(Create table)语法详解及示例 2010-06-28 13:59:13| 分类: Oracle PL/SQL|字号 订阅 创建表(Create table)语法详解 ...
- Postgresql实战经验之alter table 开小差了
Postgresql实战经验之alter table 开小差了 今天需要将一张有数据的表中一个字段varchar 类型转换为timestamp类型,但是pg的alter table 语句却开小差,出现 ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...
- 聚簇(Cluster)和聚簇表(Cluster Table)
聚簇(Cluster)和聚簇表(Cluster Table) 时间:2010-03-13 23:12来源:OralanDBA.CN 作者:AlanSawyer 点击:157次 1.创建聚簇 icmad ...
- Postgresql两表联结更新
Postgresql两表联合更新近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:update d_routetripset name=b.name , descrip ...
- Innodb parent table open时导致crash
case描述: innodb中,父表和子表通过foreign constraint进行关联, 因为在更新数据时需要check 外键constraint,如果父表被大量的子表reference, 那么在 ...
随机推荐
- 【转】Microsoft .Net Remoting之Marshal、Disconnect与生命周期以及跟踪服务
Marshal.Disconnect与生命周期以及跟踪服务 一.远程对象的激活 在Remoting中有三种激活方式,一般的实现是通过RemotingServices类的静态方法来完成.工作过程事实上是 ...
- android BroadCastRecevier笔记
学习android的Broadcast,笔记记录于此. BroadCastRecevier用于接受其他应用发送的广播. BroadCastReceiver工作,需要2步. 创建Broadcast Re ...
- 学习:erlang开源项目。
一.RabbitMQ:AMQP消息服务器 . 二.ejabberd是的Jabber / XMPP协议的即时通讯服务器. 三.cowboy/mochiweb.
- 谈谈我对Android View事件分发的理解
写这篇博客的缘由.近期因为项目中用到相似一个LinearLayout中水平布局中,有一个TextView和Button,然后对该LinearLayout布局设置点击事件.点击TextView能够触发该 ...
- VMware Host Agent服务不能正常启动
VMware Host Agent服务不能正常启动 原因及解决方法 一直都在用VMWare Server 2.0,其他都还好,就是隔三差五的会有些小问题,比如VMware Host Agent服务不能 ...
- C语言对文件的操作函数用法详解2
fopen(打开文件) 相关函数 open,fclose 表头文件 #include<stdio.h> 定义函数 FILE * fopen(const char * path,const ...
- 第四章 Spring.Net 如何管理您的类___对象的初始化方式
在 Spring.Net 中对象初始化的方式分为两种: ① 急切实例化,也就是说 Spring.Net 容器初始化的时候将对象先实例化出来. ② 延迟实例化,也就是说我们在调用 GetObject 方 ...
- chrome JS 总结
1. chrome 的 console 中不能添加本地文件 2. 下面的代码是在亚马逊的商品页面上添加一个 image, 点击之后触发 alert 函数. 其中 cBoxInner 是人工寻找到的标签 ...
- swift学习笔记之控制流
控制流: 1.if语句 let count = { print("yes") }else{ print("no") } 2.switch语句 (1)Swift中 ...
- Discuz 模板标签说明
Discuz 模板标签说明 Discuz! 的模板采用近似 PHP 表达式的语法,基本都是可识别的HTML,但涉及到变量和动态内容时,基本形式下: <!-{ 代码内容 }-> 逻辑元素包围 ...