Inheritance
Creating children tables
Section titled “Creating children tables”CREATE TABLE users (username text, email text);CREATE TABLE simple_users () INHERITS (users);CREATE TABLE users_with_password (password text) INHERITS (users);Our three tables look like this:
|Column|Type |---|---|---|--- |username|text |email|text
simple_users
Section titled “simple_users”|Column|Type |---|---|---|--- |username|text |email|text
users_with_password
Section titled “users_with_password”|Column|Type |---|---|---|--- |username|text |email|text |password|text
Altering tables
Section titled “Altering tables”Let’s create two simple tables:
CREATE TABLE users (username text, email text);CREATE TABLE simple_users () INHERITS (users);Adding columns
Section titled “Adding columns”ALTER TABLE simple_users ADD COLUMN password text;simple_users
Section titled “simple_users”|Column|Type |---|---|---|--- |username|text |email|text |password|text
Adding the same column to the parent table will merge the definition of both columns:
ALTER TABLE users ADD COLUMN password text;NOTICE: merging definition of column “password” for child “simple_users”
Dropping columns
Section titled “Dropping columns”Using our altered tables:
ALTER TABLE users DROP COLUMN password;|Column|Type |---|---|---|--- |username|text |email|text
simple_users
Section titled “simple_users”|Column|Type |---|---|---|--- |username|text |email|text |password|text
Since we first added the column to simple_users, PostgreSQL makes sure this column isn’t dropped.
Now if we had another child table, its password column would, of course, have been dropped.
Remarks
Section titled “Remarks”An explanation as to why you would want to use inheritance in PostgreSQL is available here: http://stackoverflow.com/a/3075248/653378