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, 那么在 ...
随机推荐
- 获取platformVersion、deviceName、appPackage
命令获取如下: 手机与电脑连接 devicename: adb devices platformversion : adb shell getprop ro.build.version.re ...
- 常用HQL(Hibernate Query Language)查询
查询一个对象(实体类必须有一个不带参数的构造方法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Test public void test01() ...
- spring只是一个框架
想跟着 spring in action 4 系统的研究下spring,结果发现忘了怎么建一个spring项目. 关键是,不知道该建一个什么项目,Java项目?Maven项目(Java项目?Web项目 ...
- 常用CSS缩写语法总结(转)
使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.css缩写的主要规则如下: 颜色 16进制的色彩值,如果每两位的值相同,可以缩写一半,例如:#000000可以缩写为#000;#336699可以缩 ...
- SSL 证书配置nginx
ssl.conf文件: server { listen 443; server_name www.domain.com; # 改为绑定证书的域名 ssl on; ssl_certificate 1_w ...
- [转]ASP.NET MVC 5 -从控制器访问数据模型
在本节中,您将创建一个新的MoviesController类,并在这个Controller类里编写代码来取得电影数据,并使用视图模板将数据展示在浏览器里. 在开始下一步前,先Build一下应用程序(生 ...
- UVa 10450 - World Cup Noise
题目:构造一个01串,使得当中的1不相邻,问长度为n的串有多少中. 分析:数学,递推数列. 设长度为n的串有n个.则有递推关系:f(n)= f(n-1)+ f(n-2): 长度为n的结束可能是0或者1 ...
- 打地鼠游戏iOS源代码项目
打地鼠游戏源代码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源代码.这也是一款高质量的打地鼠游戏源代码.能够拥有逐步上升的关卡的设置,大家能够在关卡时设置一些商业化的模式来盈利的,很完美的 ...
- swift - UISegmentedControl 和 UIWebView 的用法
这两个用法比较简单: 具体代码如下: 一.UISegmentedControl 1.UISegmentedControl的声明 var segment = UISegmentedControl() 2 ...
- ionic安装及测试
官方教程: http://ionicframework.com/getting-started/ 官方教程写得比较简单,简单来说就是 1)安装nodejs(安装方法:http://www.cnblog ...