Saturday 19 January 2019

postgres 5 inheritance

CREATE TABLE cities (
    name            text,
    population      float,
    altitude        int     -- in feet
);

CREATE TABLE capitals (
    state           char(2)
) INHERITS (cities);

insert into cities(name, altitude) values
('Las Vegas', 2174),('Mariposa', 1953);

insert into capitals(name, altitude) values
('Madison', 845);

select * from cities;

select * from only cities;

SELECT c.tableoid::regclass, c.name, c.altitude FROM cities c

ALTER TABLE will propagate any changes in column data definitions and check constraints down the inheritance hierarchy.

 dropping columns that are depended on by other tables is only possible when using the CASCADE option.

  if we were to specify that cities.name REFERENCES some other table, this constraint would not automatically propagate to capitals. In this case you could work around it by manually adding the same REFERENCES constraint to capitals.
 
  Specifying that another table's column REFERENCES cities(name) would allow the other table to contain city names, but not capital names

No comments:

Post a Comment