关于postgresql:PostgreSQL-表的继承

这几天学习了一下PostgreSQL数据库,从装置到简略的应用都平平淡淡,然而上面的这个性能还是给了我一点小惊喜。

表的继承
简略来说就是能够用继承的办法把一张表的列和数据传递给另一张表,子表能够比父表列多的一种构造。

上面用一个小例子来理解一下。

–创立父表并插入两条数据
mydb=# create table father_table(c1 integer,c2 varchar(10));
CREATE TABLE
mydb=# insert into father_table values(1,’101′);
INSERT 0 1
mydb=# insert into father_table values(2,’201′);
INSERT 0 1

–创立子表并插入两条数据
mydb=# create table child_table(c3 varchar(10)) inherits (father_table);
CREATE TABLE
mydb=# insert into child_table values(3,’301′,’302′);
INSERT 0 1
mydb=# insert into child_table values(4,’401′,’402′);
INSERT 0 1

–查看父表构造
mydb=# \d father_table

               Table "public.father_table"
Column Type Collation Nullable Default
c1 integer
c2 character varying(10)

Number of child tables: 1 (Use \d+ to list them.)

–查看子表构造
mydb=# \d child_table

               Table "public.child_table"
Column Type Collation Nullable Default
c1 integer
c2 character varying(10)
c3 character varying(10)

Inherits: father_table

–检索父表所有记录
mydb=# select * from father_table;

c1 c2
1 101
2 201
3 301
4 401

(4 rows)

–检索父表所有记录
mydb=# select * from child_table;

c1 c2 c3
3 301 302
4 401 402

(2 rows)

–检索只属于父表的记录
mydb=# select * from only father_table;

c1 c2
1 101
2 201

(2 rows)

–检索只属于子表的记录
mydb=# select * from only child_table;

c1 c2 c3
3 301 302
4 401 402

(2 rows)

例子很简略,一看就明确了继承表的应用办法,我就不赘述了。

当初大家想一下ORACLE数据库怎么来实现下面的利用呢?
我在下一篇里分享给大家。

2021/06/17 @ Dalian

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理