Commit c9ff1a5a authored by Thomas G. Lockhart's avatar Thomas G. Lockhart

Update references to char2 type by using char(2).

Thanks to Garr Updegraff <garru@uci.edu> for the tip.
parent f11bdb9d
This diff is collapsed.
<Chapter Id="inherit"> <chapter id="inherit">
<Title>Inheritance</Title> <title>Inheritance</title>
<Para> <para>
Let's create two classes. The capitals class contains Let's create two classes. The capitals class contains
state capitals which are also cities. Naturally, the state capitals which are also cities. Naturally, the
capitals class should inherit from cities. capitals class should inherit from cities.
<ProgramListing> <programlisting>
CREATE TABLE cities ( CREATE TABLE cities (
name text, name text,
population float, population float,
altitude int -- (in ft) altitude int -- (in ft)
); );
CREATE TABLE capitals ( CREATE TABLE capitals (
state char2 state char(2)
) INHERITS (cities); ) INHERITS (cities);
</ProgramListing> </programlisting>
In this case, an instance of capitals <firstterm>inherits</firstterm> all
attributes (name, population, and altitude) from its
parent, cities. The type of the attribute name is
<type>text</type>, a native <productname>Postgres</productname> type for variable length
ASCII strings. The type of the attribute population is
<type>float</type>, a native <productname>Postgres</productname> type for double precision
floating point numbers. State capitals have an extra
attribute, state, that shows their state. In <productname>Postgres</productname>,
a class can inherit from zero or more other classes,
and a query can reference either all instances of a
class or all instances of a class plus all of its
descendants.
<note>
<para>
The inheritance hierarchy is a actually a directed acyclic graph.
</para>
</note>
In this case, an instance of capitals <FirstTerm>inherits</FirstTerm> all For example, the following query finds
attributes (name, population, and altitude) from its all the cities that are situated at an attitude of 500ft or higher:
parent, cities. The type of the attribute name is
<Type>text</Type>, a native <ProductName>Postgres</ProductName> type for variable length <programlisting>
ASCII strings. The type of the attribute population is
<Type>float</Type>, a native <ProductName>Postgres</ProductName> type for double precision
floating point numbers. State capitals have an extra
attribute, state, that shows their state. In <ProductName>Postgres</ProductName>,
a class can inherit from zero or more other classes,
and a query can reference either all instances of a
class or all instances of a class plus all of its
descendants.
<Note>
<Para>
The inheritance hierarchy is a actually a directed acyclic graph.
</Para>
</Note>
For example, the following query finds
all the cities that are situated at an attitude of 500ft or higher:
<ProgramListing>
SELECT name, altitude SELECT name, altitude
FROM cities FROM cities
WHERE altitude &gt; 500; WHERE altitude &gt; 500;
...@@ -50,23 +52,23 @@ SELECT name, altitude ...@@ -50,23 +52,23 @@ SELECT name, altitude
+----------+----------+ +----------+----------+
|Mariposa | 1953 | |Mariposa | 1953 |
+----------+----------+ +----------+----------+
</ProgramListing> </programlisting>
</para> </para>
<Para> <para>
On the other hand, to find the names of all cities, On the other hand, to find the names of all cities,
including state capitals, that are located at an altitude including state capitals, that are located at an altitude
over 500ft, the query is: over 500ft, the query is:
<ProgramListing> <programlisting>
SELECT c.name, c.altitude SELECT c.name, c.altitude
FROM cities* c FROM cities* c
WHERE c.altitude > 500; WHERE c.altitude > 500;
</ProgramListing> </programlisting>
which returns:
which returns: <programlisting>
<ProgramListing>
+----------+----------+ +----------+----------+
|name | altitude | |name | altitude |
+----------+----------+ +----------+----------+
...@@ -76,13 +78,31 @@ SELECT c.name, c.altitude ...@@ -76,13 +78,31 @@ SELECT c.name, c.altitude
+----------+----------+ +----------+----------+
|Madison | 845 | |Madison | 845 |
+----------+----------+ +----------+----------+
</ProgramListing> </programlisting>
Here the <Quote>*</Quote> after cities indicates that the query should Here the <quote>*</quote> after cities indicates that the query should
be run over cities and all classes below cities in the be run over cities and all classes below cities in the
inheritance hierarchy. Many of the commands that we inheritance hierarchy. Many of the commands that we
have already discussed -- <Command>select</Command>, <Command>update</Command> and <Command>delete</Command> -- have already discussed -- <command>SELECT</command>,
support this <Quote>*</Quote> notation, as do others, like <Command>alter</Command>. <command>UPDATE</command> and <command>DELETE</command> --
</Para> support this <quote>*</quote> notation, as do others, like
<command>ALTER TABLE</command>.
</para>
</chapter>
</Chapter> <!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->
...@@ -737,14 +737,14 @@ ...@@ -737,14 +737,14 @@
As an example: As an example:
<programlisting> <programlisting>
PgDatabase data; PgDatabase data;
data.Exec("create table foo (a int4, b char16, d float8)"); data.Exec("create table foo (a int4, b char(16), d float8)");
data.Exec("copy foo from stdin"); data.Exec("copy foo from stdin");
data.putline("3\etHello World\et4.5\en"); data.putline("3\etHello World\et4.5\en");
data.putline("4\etGoodbye World\et7.11\en"); data.putline("4\etGoodbye World\et7.11\en");
&amp;... &amp;...
data.putline(".\en"); data.putline(".\en");
data.endcopy(); data.endcopy();
</programlisting> </programlisting>
</para> </para>
</sect1> </sect1>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment