|
1 |
| -<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.29 2004/08/07 20:44:49 tgl Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.30 2004/08/08 21:33:11 tgl Exp $ --> |
2 | 2 |
|
3 | 3 | <chapter id="ddl">
|
4 | 4 | <title>Data Definition</title>
|
@@ -996,19 +996,12 @@ CREATE TABLE capitals (
|
996 | 996 | ) INHERITS (cities);
|
997 | 997 | </programlisting>
|
998 | 998 |
|
999 |
| - In this case, a row of capitals <firstterm>inherits</firstterm> all |
1000 |
| - attributes (name, population, and altitude) from its |
1001 |
| - parent, cities. The type of the attribute name is |
1002 |
| - <type>text</type>, a native <productname>PostgreSQL</productname> type |
1003 |
| - for variable length character strings. The type of the attribute |
1004 |
| - population is |
1005 |
| - <type>float</type>, a native <productname>PostgreSQL</productname> type for double precision |
1006 |
| - floating-point numbers. State capitals have an extra |
1007 |
| - attribute, state, that shows their state. In <productname>PostgreSQL</productname>, |
1008 |
| - a table can inherit from zero or more other tables, |
1009 |
| - and a query can reference either all rows of a |
1010 |
| - table or all rows of a table plus all of its |
1011 |
| - descendants. |
| 999 | + In this case, a row of capitals <firstterm>inherits</firstterm> all |
| 1000 | + attributes (name, population, and altitude) from its parent, cities. State |
| 1001 | + capitals have an extra attribute, state, that shows their state. In |
| 1002 | + <productname>PostgreSQL</productname>, a table can inherit from zero or |
| 1003 | + more other tables, and a query can reference either all rows of a table or |
| 1004 | + all rows of a table plus all of its descendants. |
1012 | 1005 |
|
1013 | 1006 | <note>
|
1014 | 1007 | <para>
|
@@ -1065,6 +1058,32 @@ SELECT name, altitude
|
1065 | 1058 | support this <quote>ONLY</quote> notation.
|
1066 | 1059 | </para>
|
1067 | 1060 |
|
| 1061 | + <note> |
| 1062 | + <title>Deprecated</title> |
| 1063 | + <para> |
| 1064 | + In previous versions of <productname>PostgreSQL</productname>, the |
| 1065 | + default behavior was not to include child tables in queries. This was |
| 1066 | + found to be error prone and is also in violation of the SQL99 |
| 1067 | + standard. Under the old syntax, to get the sub-tables you append |
| 1068 | + <literal>*</literal> to the table name. |
| 1069 | + For example |
| 1070 | +<programlisting> |
| 1071 | +SELECT * from cities*; |
| 1072 | +</programlisting> |
| 1073 | + You can still explicitly specify scanning child tables by appending |
| 1074 | + <literal>*</literal>, as well as explicitly specify not scanning child tables by |
| 1075 | + writing <quote>ONLY</quote>. But beginning in version 7.1, the default |
| 1076 | + behavior for an undecorated table name is to scan its child tables |
| 1077 | + too, whereas before the default was not to do so. To get the old |
| 1078 | + default behavior, set the configuration option |
| 1079 | + <literal>SQL_Inheritance</literal> to off, e.g., |
| 1080 | +<programlisting> |
| 1081 | +SET SQL_Inheritance TO OFF; |
| 1082 | +</programlisting> |
| 1083 | + or add a line in your <filename>postgresql.conf</filename> file. |
| 1084 | + </para> |
| 1085 | + </note> |
| 1086 | + |
1068 | 1087 | <para>
|
1069 | 1088 | In some cases you may wish to know which table a particular row
|
1070 | 1089 | originated from. There is a system column called
|
@@ -1109,39 +1128,51 @@ WHERE c.altitude > 500 and c.tableoid = p.oid;
|
1109 | 1128 |
|
1110 | 1129 | </para>
|
1111 | 1130 |
|
1112 |
| - <note> |
1113 |
| - <title>Deprecated</title> |
1114 |
| - <para> |
1115 |
| - In previous versions of <productname>PostgreSQL</productname>, the |
1116 |
| - default behavior was not to include child tables in queries. This was |
1117 |
| - found to be error prone and is also in violation of the SQL99 |
1118 |
| - standard. Under the old syntax, to get the sub-tables you append |
1119 |
| - <literal>*</literal> to the table name. |
1120 |
| - For example |
1121 |
| -<programlisting> |
1122 |
| -SELECT * from cities*; |
1123 |
| -</programlisting> |
1124 |
| - You can still explicitly specify scanning child tables by appending |
1125 |
| - <literal>*</literal>, as well as explicitly specify not scanning child tables by |
1126 |
| - writing <quote>ONLY</quote>. But beginning in version 7.1, the default |
1127 |
| - behavior for an undecorated table name is to scan its child tables |
1128 |
| - too, whereas before the default was not to do so. To get the old |
1129 |
| - default behavior, set the configuration option |
1130 |
| - <literal>SQL_Inheritance</literal> to off, e.g., |
1131 |
| -<programlisting> |
1132 |
| -SET SQL_Inheritance TO OFF; |
1133 |
| -</programlisting> |
1134 |
| - or add a line in your <filename>postgresql.conf</filename> file. |
1135 |
| - </para> |
1136 |
| - </note> |
1137 |
| - |
1138 | 1131 | <para>
|
1139 |
| - A limitation of the inheritance feature is that indexes (including |
| 1132 | + A serious limitation of the inheritance feature is that indexes (including |
1140 | 1133 | unique constraints) and foreign key constraints only apply to single
|
1141 |
| - tables, not to their inheritance children. Thus, in the above example, |
1142 |
| - specifying that another table's column <literal>REFERENCES cities(name)</> |
1143 |
| - would allow the other table to contain city names but not capital names. |
1144 |
| - This deficiency will probably be fixed in some future release. |
| 1134 | + tables, not to their inheritance children. This is true on both the |
| 1135 | + referencing and referenced sides of a foreign key constraint. Thus, |
| 1136 | + in the terms of the above example: |
| 1137 | + |
| 1138 | + <itemizedlist> |
| 1139 | + <listitem> |
| 1140 | + <para> |
| 1141 | + If we declared <structname>cities</>.<structfield>name</> to be |
| 1142 | + <literal>UNIQUE</> or a <literal>PRIMARY KEY</>, this would not stop the |
| 1143 | + <structname>capitals</> table from having rows with names duplicating |
| 1144 | + rows in <structname>cities</>. And those duplicate rows would by |
| 1145 | + default show up in SELECTs from <structname>cities</>. In fact, by |
| 1146 | + default <structname>capitals</> would have no unique constraint at all, |
| 1147 | + and so could contain multiple rows with the same name. |
| 1148 | + You could add a unique constraint to <structname>capitals</>, but this |
| 1149 | + would not prevent duplication compared to <structname>cities</>. |
| 1150 | + </para> |
| 1151 | + </listitem> |
| 1152 | + |
| 1153 | + <listitem> |
| 1154 | + <para> |
| 1155 | + Similarly, if we were to specify that |
| 1156 | + <structname>cities</>.<structfield>name</> <literal>REFERENCES</> some |
| 1157 | + other table, this constraint would not automatically propagate to |
| 1158 | + <structname>capitals</>. In this case you could work around it by |
| 1159 | + manually adding the same <literal>REFERENCES</> constraint to |
| 1160 | + <structname>capitals</>. |
| 1161 | + </para> |
| 1162 | + </listitem> |
| 1163 | + |
| 1164 | + <listitem> |
| 1165 | + <para> |
| 1166 | + Specifying that another table's column <literal>REFERENCES |
| 1167 | + cities(name)</> would allow the other table to contain city names, but |
| 1168 | + not capital names. There is no good workaround for this case. |
| 1169 | + </para> |
| 1170 | + </listitem> |
| 1171 | + </itemizedlist> |
| 1172 | + |
| 1173 | + These deficiencies will probably be fixed in some future release, |
| 1174 | + but in the meantime considerable care is needed in deciding whether |
| 1175 | + inheritance is useful for your problem. |
1145 | 1176 | </para>
|
1146 | 1177 | </sect1>
|
1147 | 1178 |
|
|
0 commit comments