|
1 |
| -<!-- $PostgreSQL: pgsql/doc/src/sgml/rowtypes.sgml,v 2.1 2004/06/07 04:04:47 tgl Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/rowtypes.sgml,v 2.2 2004/06/09 19:08:14 tgl Exp $ --> |
2 | 2 |
|
3 | 3 | <sect1 id="rowtypes">
|
4 | 4 | <title>Composite Types</title>
|
@@ -66,6 +66,27 @@ SELECT price_extension(item, 10) FROM on_hand;
|
66 | 66 | </programlisting>
|
67 | 67 |
|
68 | 68 | </para>
|
| 69 | + |
| 70 | + <para> |
| 71 | + Whenever you create a table, a composite type is also automatically |
| 72 | + created, with the same name as the table, to represent the table's |
| 73 | + row type. For example, had we said |
| 74 | +<programlisting> |
| 75 | +CREATE TABLE inventory_item ( |
| 76 | + name text, |
| 77 | + supplier_id integer REFERENCES suppliers, |
| 78 | + price numeric CHECK (price > 0) |
| 79 | +); |
| 80 | +</programlisting> |
| 81 | + then the same <literal>inventory_item</> composite type shown above would |
| 82 | + come into being as a |
| 83 | + byproduct, and could be used just as above. Note however an important |
| 84 | + restriction of the current implementation: since no constraints are |
| 85 | + associated with a composite type, the constraints shown in the table |
| 86 | + definition <emphasis>do not apply</> to values of the composite type |
| 87 | + outside the table. (A partial workaround is to use domain |
| 88 | + types as members of composite types.) |
| 89 | + </para> |
69 | 90 | </sect2>
|
70 | 91 |
|
71 | 92 | <sect2>
|
@@ -178,6 +199,49 @@ SELECT (my_func(...)).field FROM ...
|
178 | 199 | </para>
|
179 | 200 | </sect2>
|
180 | 201 |
|
| 202 | + <sect2> |
| 203 | + <title>Modifying Composite Types</title> |
| 204 | + |
| 205 | + <para> |
| 206 | + Here are some examples of the proper syntax for inserting and updating |
| 207 | + composite columns. |
| 208 | + First, inserting or updating a whole column: |
| 209 | + |
| 210 | +<programlisting> |
| 211 | +INSERT INTO mytab (complex_col) VALUES((1.1,2.2)); |
| 212 | + |
| 213 | +UPDATE mytab SET complex_col = ROW(1.1,2.2) WHERE ...; |
| 214 | +</programlisting> |
| 215 | + |
| 216 | + The first example omits <literal>ROW</>, the second uses it; we |
| 217 | + could have done it either way. |
| 218 | + </para> |
| 219 | + |
| 220 | + <para> |
| 221 | + We can update an individual subfield of a composite column: |
| 222 | + |
| 223 | +<programlisting> |
| 224 | +UPDATE mytab SET complex_col.r = (complex_col).r + 1 WHERE ...; |
| 225 | +</programlisting> |
| 226 | + |
| 227 | + Notice here that we don't need to (and indeed cannot) |
| 228 | + put parentheses around the column name appearing just after |
| 229 | + <literal>SET</>, but we do need parentheses when referencing the same |
| 230 | + column in the expression to the right of the equal sign. |
| 231 | + </para> |
| 232 | + |
| 233 | + <para> |
| 234 | + And we can specify subfields as targets for <command>INSERT</>, too: |
| 235 | + |
| 236 | +<programlisting> |
| 237 | +INSERT INTO mytab (complex_col.r, complex_col.i) VALUES(1.1, 2.2); |
| 238 | +</programlisting> |
| 239 | + |
| 240 | + Had we not supplied values for all the subfields of the column, the |
| 241 | + remaining subfields would have been filled with NULLs. |
| 242 | + </para> |
| 243 | + </sect2> |
| 244 | + |
181 | 245 | <sect2>
|
182 | 246 | <title>Composite Type Input and Output Syntax</title>
|
183 | 247 |
|
|
0 commit comments