After learning the PostgreSQL database these days, the installation and simple use of the database are not very good, but the following function still gives me a little surprise.
In simple terms, table inheritance is a structure in which the columns and data of one table can be passed to another table by inheritance, and the child table can have more columns than the parent table.
Let’s use a small example to understand.
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)
The example is simple enough to see how inheritance tables work, so I won’t repeat it.
Now you want to think about how Oracle database to implement the above application? I will share it with you in the next post.
2021/06/17 @ Dalian