@@ -10,14 +10,13 @@ This must become a chapter on array behavior. Volunteers? - thomas 1998-01-12
1010</Para>
1111
1212<Para>
13- <ProductName>Postgres</ProductName> allows attributes of an instance to be defined
14- as fixed-length or variable-length multi-dimensional
15- arrays. Arrays of any base type or user-defined type
16- can be created. To illustrate their use, we first create a
17- class with arrays of base types.
13+ <ProductName>Postgres</ProductName> allows attributes of a class
14+ to be defined as variable-length multi-dimensional
15+ arrays. Arrays of any built-in type or user-defined type
16+ can be created. To illustrate their use, we create this class:
1817
1918<ProgramListing>
20- CREATE TABLESAL_EMP (
19+ CREATE TABLEsal_emp (
2120 name text,
2221 pay_by_quarter int4[],
2322 schedule text[][]
@@ -26,31 +25,29 @@ CREATE TABLE SAL_EMP (
2625</Para>
2726
2827<Para>
29- The above query will create a class namedSAL_EMP with
28+ The above query will create a class named<FirstTerm>sal_emp</FirstTerm> with
3029 a <FirstTerm>text</FirstTerm> string (name), a one-dimensional array of <FirstTerm>int4</FirstTerm>
3130 (pay_by_quarter), which represents the employee's
32- salary by quarter and a two-dimensional array of <FirstTerm>text</FirstTerm>
31+ salary by quarter, and a two-dimensional array of <FirstTerm>text</FirstTerm>
3332 (schedule), which represents the employee's weekly
3433 schedule. Now we do some <FirstTerm>INSERTS</FirstTerm>s; note that when
3534 appending to an array, we enclose the values within
3635 braces and separate them by commas. If you know <FirstTerm>C</FirstTerm>,
3736 this is not unlike the syntax for initializing structures.
3837
3938<ProgramListing>
40- INSERT INTOSAL_EMP
39+ INSERT INTOsal_emp
4140 VALUES ('Bill',
4241 '{10000, 10000, 10000, 10000}',
4342 '{{"meeting", "lunch"}, {}}');
4443
45- INSERT INTOSAL_EMP
44+ INSERT INTOsal_emp
4645 VALUES ('Carol',
4746 '{20000, 25000, 25000, 25000}',
4847 '{{"talk", "consult"}, {"meeting"}}');
4948</ProgramListing>
5049
51- By default, <ProductName>Postgres</ProductName> uses the "one-based" numbering
52- convention for arrays -- that is, an array of n elements starts with array[1] and ends with array[n].
53- Now, we can run some queries on SAL_EMP. First, we
50+ Now, we can run some queries on sal_emp. First, we
5451 show how to access a single element of an array at a
5552 time. This query retrieves the names of the employees
5653 whose pay changed in the second quarter:
@@ -63,6 +60,10 @@ SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];
6360 Carol
6461(1 row)
6562</ProgramListing>
63+
64+ <ProductName>Postgres</ProductName> uses the "one-based" numbering
65+ convention for arrays --- that is, an array of n elements starts with
66+ array[1] and ends with array[n].
6667</Para>
6768
6869<Para>
@@ -82,8 +83,10 @@ SELECT pay_by_quarter[3] FROM sal_emp;
8283
8384<Para>
8485 We can also access arbitrary slices of an array, or
85- subarrays. This query retrieves the first item on
86- Bill's schedule for the first two days of the week.
86+ subarrays. An array slice is denoted by writing
87+ "lower subscript : upper subscript" for one or more array
88+ dimensions. This query retrieves the first item on
89+ Bill's schedule for the first two days of the week:
8790
8891<ProgramListing>
8992SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';
@@ -93,6 +96,59 @@ SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';
9396 {{"meeting"},{""}}
9497(1 row)
9598</ProgramListing>
99+
100+ We could also have written
101+
102+ <ProgramListing>
103+ SELECT schedule[1:2][1] FROM sal_emp WHERE name = 'Bill';
104+ </ProgramListing>
105+
106+ with the same result.
107+ </Para>
108+
109+ <Para>
110+ An array value can be replaced completely:
111+
112+ <ProgramListing>
113+ UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
114+ WHERE name = 'Carol';
115+ </ProgramListing>
116+
117+ or updated at a single entry:
118+
119+ <ProgramListing>
120+ UPDATE sal_emp SET pay_by_quarter[4] = 15000
121+ WHERE name = 'Bill';
122+ </ProgramListing>
123+
124+ or updated in a slice:
125+
126+ <ProgramListing>
127+ UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}'
128+ WHERE name = 'Carol';
129+ </ProgramListing>
130+ </Para>
131+
132+ <Para>
133+ It is not currently possible to resize an array value except by
134+ complete replacement; for example, we couldn't change a four-
135+ element array value to a five-element value with a single
136+ assignment to array[5].
137+ </Para>
138+
139+ <Para>
140+ The syntax for CREATE TABLE allows fixed-length arrays to be
141+ defined:
142+
143+ <ProgramListing>
144+ CREATE TABLE tictactoe (
145+ squares int4[3][3]
146+ );
147+ </ProgramListing>
148+
149+ However, the current implementation does not enforce the array
150+ size limits --- the behavior is the same as for arrays of
151+ unspecified length.
96152</Para>
97153
98154</Chapter>