of 40 /40
PostgreSQL 7.3.2 Tutorial The PostgreSQL Global Development Group

PostgreSQL 7.3.2 Tutorial · PDF filenew Tcl commands to interface Tcl programs with the Postgres95 backend. ... PostgreSQL Tutorial An informal introduction for new users. PostgreSQL

Embed Size (px)

Text of PostgreSQL 7.3.2 Tutorial · PDF filenew Tcl commands to interface Tcl programs with the...

  • PostgreSQL 7.3.2 Tutorial

    The PostgreSQL Global Development Group

  • PostgreSQL 7.3.2 Tutorialby The PostgreSQL Global Development GroupCopyright 1996-2002 by The PostgreSQL Global Development Group

    Legal Notice

    PostgreSQL is Copyright 1996-2002 by the PostgreSQL Global Development Group and is distributed under the terms of the license of theUniversity of California below.

    Postgres95 is Copyright 1994-5 by the Regents of the University of California.

    Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreementis hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.

    IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCI-DENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITSDOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IM-PLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HERE-UNDER IS ON AN AS-IS BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

  • Table of ContentsPreface .........................................................................................................................................................i

    1. What is PostgreSQL? ......................................................................................................................i2. A Short History of PostgreSQL ......................................................................................................i

    2.1. The Berkeley POSTGRES Project ................................................................................... ii2.2. Postgres95......................................................................................................................... ii2.3. PostgreSQL...................................................................................................................... iii

    3. Whats In This Book .....................................................................................................................iv4. Overview of Documentation Resources........................................................................................iv5. Terminology and Notation .............................................................................................................v6. Bug Reporting Guidelines.............................................................................................................vi

    6.1. Identifying Bugs ...............................................................................................................vi6.2. What to report...................................................................................................................vi6.3. Where to report bugs ..................................................................................................... viii

    1. Getting Started.......................................................................................................................................1

    1.1. Installation...................................................................................................................................11.2. Architectural Fundamentals ........................................................................................................11.3. Creating a Database ....................................................................................................................21.4. Accessing a Database..................................................................................................................3

    2. The SQL Language................................................................................................................................6

    2.1. Introduction.................................................................................................................................62.2. Concepts......................................................................................................................................62.3. Creating a New Table ..................................................................................................................62.4. Populating a Table With Rows....................................................................................................72.5. Querying a Table .........................................................................................................................82.6. Joins Between Tables ..................................................................................................................92.7. Aggregate Functions .................................................................................................................112.8. Updates......................................................................................................................................132.9. Deletions ...................................................................................................................................14

    3. Advanced Features...............................................................................................................................15

    3.1. Introduction...............................................................................................................................153.2. Views.........................................................................................................................................153.3. Foreign Keys .............................................................................................................................153.4. Transactions ..............................................................................................................................163.5. Inheritance.................................................................................................................................173.6. Conclusion ................................................................................................................................19

    Bibliography.............................................................................................................................................20

    Index..........................................................................................................................................................22

    iii

  • Preface

    1. What is PostgreSQL?PostgreSQL is an object-relational database management system (ORDBMS) based on POSTGRES,Version 4.21, developed at the University of California at Berkeley Computer Science Department. ThePOSTGRES project, led by Professor Michael Stonebraker, was sponsored by the Defense AdvancedResearch Projects Agency (DARPA), the Army Research Office (ARO), the National Science Foundation(NSF), and ESL, Inc.

    PostgreSQL is an open-source descendant of this original Berkeley code. It provides SQL92/SQL99 lan-guage support and other modern features.

    POSTGRES pioneered many of the object-relational concepts now becoming available in some commer-cial databases. Traditional relational database management systems (RDBMS) support a data model con-sisting of a collection of named relations, containing attributes of a specific type. In current commercialsystems, possible types include floating point numbers, integers, character strings, money, and dates. It iscommonly recognized that this model is inadequate for future data-processing applications. The relationalmodel successfully replaced previous models in part because of its Spartan simplicity. However, thissimplicity makes the implementation of certain applications very difficult. PostgreSQL offers substantialadditional power by incorporating the following additional concepts in such a way that users can easilyextend the system:

    inheritance data types functions

    Other features provide additional power and flexibility:

    constraints triggers rules transactional integrity

    These features put PostgreSQL into the category of databases referred to as object-relational. Note thatthis is distinct from those referred to as object-oriented, which in general are not as well suited to support-ing traditional relational database languages. So, although PostgreSQL has some object-oriented features,it is firmly in the relational database world. In fact, some commercial databases have recently incorporatedfeatures pioneered by PostgreSQL.

    1. http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/postgres.html

    i

  • Preface

    2. A Short History of PostgreSQLThe object-relational database management system now known as PostgreSQL (and briefly called Post-gres95) is derived from the POSTGRES package written at the University of California at Berkeley. Withover a decade of development behind it, PostgreSQL is the most advanced open-source database availableanywhere, offering multiversion concurrency control, supporting almost all SQL constructs (includingsubselects, transactions, and user-defined types and functions), and having a wide range of language bind-ings available (including C, C++, Java, Perl, Tcl, and Python).

    2.1. The Berkeley POSTGRES Project

    Implementation of the POSTGRES DBMS began in 1986. The initial concepts for the system were pre-sented in The design of POSTGRES and the definition of the initial data model appeared in The POST-GRES data model. The design of the rule system at that time was described in T