PostgreSQL: Advanced SQL: Inheritance

  • Object orientation feature
  • If B inherits A then:
    • B gets all columns of A
    • SELECT/UPDATE/INSERT/DELETE to B also affect entries in A
  • Example: Table 'subscribers' cannot be modified, but email address must be stored in a normalised fashion
01 CREATE TABLE subscribers (
02      usrid int,
03      address text,
04      primary key(usrid));
05
06 CREATE TABLE esubscribers (
07      email text)
08      INHERITS(subscribers);
09
10 INSERT INTO subscribers VALUES(1,'1 some st');
11 INSERT INTO esubscribers VALUES(2,'2 some st','foo@bar.com');
12
13 select * from subscribers ;
14  usrid |  address
15 -------+-----------
16      1 | 1 some st
17      2 | 2 some st
18 (2 rows)
19
20 template1=# select * from esubscribers ;
21  usrid |  address  |    email
22 -------+-----------+-------------
23      1 | 2 some st | foo@bar.com
24 (1 row)
25
Prev

Next

Page 32