<!-- doc/src/sgml/query.sgml -->
<chapter id="tutorial-sql">
<title>The <acronym>SQL</acronym> Language</title>
<sect1 id="tutorial-sql-intro">
<title>Introduction</title>
<para>
This chapter provides an overview of how to use
<acronym>SQL</acronym> to perform simple operations. This
tutorial is only intended to give you an introduction and is in no
way a complete tutorial on <acronym>SQL</acronym>. Numerous books
have been written on <acronym>SQL</acronym>, including <xref
linkend="melt93"/> and <xref linkend="date97"/>.
You should be aware that some <productname>PostgreSQL</productname>
language features are extensions to the standard.
</para>
<para>
In the examples that follow, we assume that you have created a
database named <literal>mydb</literal>, as described in the previous
chapter, and have been able to start <application>psql</application>.
</para>
<para>
Examples in this manual can also be found in the
<productname>PostgreSQL</productname> source distribution
in the directory <filename>src/tutorial/</filename>. (Binary
distributions of <productname>PostgreSQL</productname> might not
provide those files.) To use those
files, first change to that directory and run <application>make</application>:
<screen>
<prompt>$</prompt> <userinput>cd <replaceable>...</replaceable>/src/tutorial</userinput>
<prompt>$</prompt> <userinput>make</userinput>
</screen>
This creates the scripts and compiles the C files containing user-defined
functions and types. Then, to start the tutorial, do the following:
<screen>
<prompt>$</prompt> <userinput>psql -s mydb</userinput>
<computeroutput>
...
</computeroutput>
<prompt>mydb=></prompt> <userinput>\i basics.sql</userinput>
</screen>
The <literal>\i</literal> command reads in commands from the
specified file. <command>psql</command>'s <literal>-s</literal> option puts you in
single step mode which pauses before sending each statement to the
server. The commands used in this section are in the file
<filename>basics.sql</filename>.
</para>
</sect1>
<sect1 id="tutorial-concepts">
<title>Concepts</title>
<para>
<indexterm><primary>relational database</primary></indexterm>
<indexterm><primary>hierarchical database</primary></indexterm>
<indexterm><primary>object-oriented database</primary></indexterm>
<indexterm><primary>relation</primary></indexterm>
<indexterm><primary>table</primary></indexterm>
<productname>PostgreSQL</productname> is a <firstterm>relational
database management system</firstterm> (<acronym>RDBMS</acronym>).
That means it is a system for managing data stored in
<firstterm>relations</firstterm>. Relation is essentially a
mathematical term for <firstterm>table</firstterm>. The notion of
storing data in tables is so commonplace today that it might
seem inherently obvious, but there are a number of other ways of
organizing databases. Files and directories on Unix-like
operating systems form an example of a hierarchical database. A
more modern development is the object-oriented database.
</para>
<para>
<indexterm><primary>row</primary></indexterm>
<indexterm><primary>column</primary></indexterm>
Each table is a named collection of <firstterm>rows</firstterm>.
Each row of a given table has the same set of named
<firstterm>columns</firstterm>,
and each column is of a specific data type. Whereas columns have
a fixed order in each row, it is important to remember that SQL
does not guarantee the order of the rows within the table in any
way (although they can be explicitly sorted for display).
</para>
<para>
<indexterm><primary>database cluster</primary></indexterm>
<indexterm><primary>cluster</primary><secondary>of databases</secondary><see>database cluster</see></indexterm>
Tables are grouped into databases, and