|
| 1 | +<HTML> |
| 2 | +<HEAD> |
| 3 | + <TITLE>The POSTGRES95 User Manual - ADVANCED POSTGRES SQL FEATURES</TITLE> |
| 4 | +</HEAD> |
| 5 | + |
| 6 | +<BODY> |
| 7 | + |
| 8 | +<font size=-1> |
| 9 | +<A HREF="pg95user.html">[ TOC ]</A> |
| 10 | +<A HREF="query.html">[ Previous ]</A> |
| 11 | +<A HREF="extend.html">[ Next ]</A> |
| 12 | +</font> |
| 13 | +<HR> |
| 14 | +<H1>5. ADVANCED POSTGRES <B>SQL</B> FEATURES</H1> |
| 15 | +<HR> |
| 16 | + Having covered the basics of using POSTGRES <B>SQL</B> to |
| 17 | + access your data, we will now discuss those features of |
| 18 | + POSTGRES that distinguish it from conventional data |
| 19 | + managers. These features include inheritance, time |
| 20 | + travel and non-atomic data values (array- and |
| 21 | + set-valued attributes). |
| 22 | + Examples in this section can also be found in |
| 23 | + <CODE>advance.sql</CODE> in the tutorial directory. (Refer to the |
| 24 | + introduction of the <A HREF="query.html">previous chapter</A> for how to use |
| 25 | + it.) |
| 26 | + |
| 27 | +<H2><A NAME="inheritance">5.1. Inheritance</A></H2> |
| 28 | + Let's create two classes. The capitals class contains |
| 29 | + state capitals which are also cities. Naturally, the |
| 30 | + capitals class should inherit from cities. |
| 31 | + |
| 32 | +<pre> CREATE TABLE cities ( |
| 33 | + name text, |
| 34 | + population float, |
| 35 | + altitude int -- (in ft) |
| 36 | + ); |
| 37 | + |
| 38 | + CREATE TABLE capitals ( |
| 39 | + state char2 |
| 40 | + ) INHERITS (cities); |
| 41 | +</pre> |
| 42 | + In this case, an instance of capitals <B>inherits</B> all |
| 43 | + attributes (name, population, and altitude) from its |
| 44 | + parent, cities. The type of the attribute name is |
| 45 | + <B>text</B>, a built-in POSTGRES type for variable length |
| 46 | + ASCII strings. The type of the attribute population is |
| 47 | + <B>float4</B>, a built-in POSTGRES type for double precision |
| 48 | + floating point numbres. State capitals have an extra |
| 49 | + attribute, state, that shows their state. In POSTGRES, |
| 50 | + a class can inherit from zero or more other classes,<A HREF="#4"><font size=-1>[4]</font></A> |
| 51 | + and a query can reference either all instances of a |
| 52 | + class or all instances of a class plus all of its |
| 53 | + descendants. For example, the following query finds |
| 54 | + all the cities that are situated at an attitude of 500 |
| 55 | + 'ft or higher: |
| 56 | + |
| 57 | +<pre> SELECT name, altitude |
| 58 | + FROM cities |
| 59 | + WHERE altitude > 500; |
| 60 | + |
| 61 | + |
| 62 | + +----------+----------+ |
| 63 | + |name | altitude | |
| 64 | + +----------+----------+ |
| 65 | + |Las Vegas | 2174 | |
| 66 | + +----------+----------+ |
| 67 | + |Mariposa | 1953 | |
| 68 | + +----------+----------+ |
| 69 | +</pre> |
| 70 | + On the other hand, to find the names of all cities, |
| 71 | + including state capitals, that are located at an altitude |
| 72 | + over 500 'ft, the query is: |
| 73 | + |
| 74 | +<pre> SELECT c.name, c.altitude |
| 75 | + FROM cities* c |
| 76 | + WHERE c.altitude > 500; |
| 77 | +</pre> |
| 78 | + which returns: |
| 79 | + |
| 80 | +<pre> +----------+----------+ |
| 81 | + |name | altitude | |
| 82 | + +----------+----------+ |
| 83 | + |Las Vegas | 2174 | |
| 84 | + +----------+----------+ |
| 85 | + |Mariposa | 1953 | |
| 86 | + +----------+----------+ |
| 87 | + |Madison | 845 | |
| 88 | + +----------+----------+ |
| 89 | +</pre> |
| 90 | + Here the * after cities indicates that the query should |
| 91 | + be run over cities and all classes below cities in the |
| 92 | + inheritance hierarchy. Many of the commands that we |
| 93 | + have already discussed -- select, update and delete -- |
| 94 | + support this * notation, as do others, like alter command. |
| 95 | + |
| 96 | +<H2><A NAME="time-travel">5.2. Time Travel</A></H2> |
| 97 | + POSTGRES supports the notion of time travel. This feature |
| 98 | + allows a user to run historical queries. For |
| 99 | + example, to find the current population of Mariposa |
| 100 | + city, one would query: |
| 101 | + |
| 102 | +<pre> SELECT * FROM cities WHERE name = 'Mariposa'; |
| 103 | + |
| 104 | + +---------+------------+----------+ |
| 105 | + |name | population | altitude | |
| 106 | + +---------+------------+----------+ |
| 107 | + |Mariposa | 1320 | 1953 | |
| 108 | + +---------+------------+----------+ |
| 109 | +</pre> |
| 110 | + POSTGRES will automatically find the version of Mariposa's |
| 111 | + record valid at the current time. |
| 112 | + One can also give a time range. For example to see the |
| 113 | + past and present populations of Mariposa, one would |
| 114 | + query: |
| 115 | + |
| 116 | +<pre> SELECT name, population |
| 117 | + FROM cities['epoch', 'now'] |
| 118 | + WHERE name = 'Mariposa'; |
| 119 | +</pre> |
| 120 | + where "epoch" indicates the beginning of the system |
| 121 | + clock.<A HREF="#5"><font size=-1>[5]</font></A> If you have executed all of the examples so |
| 122 | + far, then the above query returns: |
| 123 | + |
| 124 | +<pre> +---------+------------+ |
| 125 | + |name | population | |
| 126 | + +---------+------------+ |
| 127 | + |Mariposa | 1200 | |
| 128 | + +---------+------------+ |
| 129 | + |Mariposa | 1320 | |
| 130 | + +---------+------------+ |
| 131 | +</pre> |
| 132 | + The default beginning of a time range is the earliest |
| 133 | + time representable by the system and the default end is |
| 134 | + the current time; thus, the above time range can be |
| 135 | + abbreviated as ``[,].'' |
| 136 | + |
| 137 | +<H2><A NAME="non-atomic-values">5.3. Non-Atomic Values</A></H2> |
| 138 | + One of the tenets of the relational model is that the |
| 139 | + attributes of a relation are atomic. POSTGRES does not |
| 140 | + have this restriction; attributes can themselves contain |
| 141 | + sub-values that can be accessed from the query |
| 142 | + language. For example, you can create attributes that |
| 143 | + are arrays of base types. |
| 144 | + |
| 145 | +<H3><A NAME="arrays">5.3.1. Arrays</A></H3> |
| 146 | + POSTGRES allows attributes of an instance to be defined |
| 147 | + as fixed-length or variable-length multi-dimensional |
| 148 | + arrays. Arrays of any base type or user-defined type |
| 149 | + can be created. To illustrate their use, we first create a |
| 150 | + class with arrays of base types. |
| 151 | + |
| 152 | +<pre> * CREATE TABLE SAL_EMP ( |
| 153 | + name text, |
| 154 | + pay_by_quarter int4[], |
| 155 | + schedule char16[][] |
| 156 | + ); |
| 157 | +</pre> |
| 158 | + The above query will create a class named SAL_EMP with |
| 159 | + a <B>text</B> string (name), a one-dimensional array of <B>int4</B> |
| 160 | + (pay_by_quarter), which represents the employee's |
| 161 | + salary by quarter and a two-dimensional array of <B>char16</B> |
| 162 | + (schedule), which represents the employee's weekly |
| 163 | + schedule. Now we do some <B>INSERTS</B>s; note that when |
| 164 | + appending to an array, we enclose the values within |
| 165 | + braces and separate them by commas. If you know <B>C</B>, |
| 166 | + this is not unlike the syntax for initializing structures. |
| 167 | + |
| 168 | +<pre> INSERT INTO SAL_EMP |
| 169 | + VALUES ('Bill', |
| 170 | + '{10000, 10000, 10000, 10000}', |
| 171 | + '{{"meeting", "lunch"}, {}}'); |
| 172 | + |
| 173 | + INSERT INTO SAL_EMP |
| 174 | + VALUES ('Carol', |
| 175 | + '{20000, 25000, 25000, 25000}', |
| 176 | + '{{"talk", "consult"}, {"meeting"}}'); |
| 177 | +</pre> |
| 178 | + By default, POSTGRES uses the "one-based" numbering |
| 179 | + convention for arrays -- that is, an array of n elements starts with array[1] and ends with array[n]. |
| 180 | + Now, we can run some queries on SAL_EMP. First, we |
| 181 | + show how to access a single element of an array at a |
| 182 | + time. This query retrieves the names of the employees |
| 183 | + whose pay changed in the second quarter: |
| 184 | + |
| 185 | +<pre> * SELECT name |
| 186 | + FROM SAL_EMP |
| 187 | + WHERE SAL_EMP.pay_by_quarter[1] <> |
| 188 | + SAL_EMP.pay_by_quarter[2]; |
| 189 | + |
| 190 | + +------+ |
| 191 | + |name | |
| 192 | + +------+ |
| 193 | + |Carol | |
| 194 | + +------+ |
| 195 | +</pre> |
| 196 | + This query retrieves the third quarter pay of all |
| 197 | + employees: |
| 198 | + |
| 199 | +<pre> * SELECT SAL_EMP.pay_by_quarter[3] FROM SAL_EMP; |
| 200 | + |
| 201 | + |
| 202 | + +---------------+ |
| 203 | + |pay_by_quarter | |
| 204 | + +---------------+ |
| 205 | + |10000 | |
| 206 | + +---------------+ |
| 207 | + |25000 | |
| 208 | + +---------------+ |
| 209 | +</pre> |
| 210 | + We can also access arbitrary slices of an array, or |
| 211 | + subarrays. This query retrieves the first item on |
| 212 | + Bill's schedule for the first two days of the week. |
| 213 | + |
| 214 | +<pre> * SELECT SAL_EMP.schedule[1:2][1:1] |
| 215 | + FROM SAL_EMP |
| 216 | + WHERE SAL_EMP.name = 'Bill'; |
| 217 | + |
| 218 | + +-------------------+ |
| 219 | + |schedule | |
| 220 | + +-------------------+ |
| 221 | + |{{"meeting"},{""}} | |
| 222 | + +-------------------+ |
| 223 | + |
| 224 | +</pre> |
| 225 | +<p> |
| 226 | +<HR> |
| 227 | +<A NAME="4"><B>4.</B></A> i.e., the inheritance hierarchy is a directed acyclic |
| 228 | +graph.<br> |
| 229 | +<A NAME="5"><B>5.</B></A> On UNIX systems, this is always midnight, January 1, |
| 230 | +1970 GMT.<br> |
| 231 | +<HR> |
| 232 | +<font size=-1> |
| 233 | +<A HREF="pg95user.html">[ TOC ]</A> |
| 234 | +<A HREF="query.html">[ Previous ]</A> |
| 235 | +<A HREF="extend.html">[ Next ]</A> |
| 236 | +</font> |
| 237 | + |
0 commit comments