![]() However that does not help for the above case because the cities table does not contain the column state, and so the command will be rejected before the rule can be applied.Īll check constraints and not-null constraints on a parent table are automatically inherited by its children, unless explicitly specified otherwise with NO INHERIT clauses. In some cases it is possible to redirect the insertion using a rule (see Chapter 41). We might hope that the data would somehow be routed to the capitals table, but this does not happen: INSERT always inserts into exactly the table specified. INSERT INTO cities (name, population, elevation, state) In our example, the following INSERT statement will fail: Inheritance does not automatically propagate data from INSERT or COPY commands to other tables in the inheritance hierarchy. SELECT c.tableoid::regclass, c.name, c.elevation WHERE c.elevation > 500 AND c.tableoid = p.oid Īnother way to get the same effect is to use the regclass alias type, which will print the table OID symbolically: (If you try to reproduce this example, you will probably get different numeric OIDs.) By doing a join with pg_class you can see the actual table names: There is a system column called tableoid in each table which can tell you the originating table: In some cases you might wish to know which table a particular row originated from. However, this syntax is still supported for compatibility with older releases where the default could be changed. Writing * is not necessary, since this behavior is always the default. You can also write the table name with a trailing * to explicitly specify that descendant tables are included: Many of the commands that we have already discussed - SELECT, UPDATE and DELETE - support the ONLY keyword. Here the ONLY keyword indicates that the query should apply only to cities, and not any tables below cities in the inheritance hierarchy. On the other hand, the following query finds all the cities that are not state capitals and are situated at an elevation over 500 feet: Given the sample data from the PostgreSQL tutorial (see Section 2.1), this returns: For example, the following query finds the names of all cities, including state capitals, that are located at an elevation over 500 feet: In PostgreSQL, a table can inherit from zero or more other tables, and a query can reference either all rows of a table or all rows of a table plus all of its descendant tables. State capitals also have an extra column, state, that shows their state. In this case, the capitals table inherits all the columns of its parent table, cities. We define the capitals table so that it inherits from cities: However, what happens when we want to ask for data about a city, regardless of whether it is a capital or not? The inheritance feature can help to resolve this problem. This can be done by creating two tables, one for state capitals and one for cities that are not capitals. ![]() ![]() We want to be able to quickly retrieve the capital city for any particular state. Each state has many cities, but only one capital. Let's start with an example: suppose we are trying to build a data model for cities. (SQL:1999 and later define a type inheritance feature, which differs in many respects from the features described here.) PostgreSQL implements table inheritance, which can be a useful tool for database designers.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |