head	1.6;
access;
symbols
	REL9_0_0:1.6
	REL9_1_ALPHA1:1.6
	REL9_0_RC1:1.6
	REL9_0_BETA4:1.6
	REL9_0_STABLE:1.6.0.6
	REL9_0_BETA3:1.6
	REL9_0_BETA2:1.6
	REL7_4_29:1.4
	REL8_0_25:1.5
	REL8_1_21:1.5
	REL8_2_17:1.5
	REL8_3_11:1.5
	REL8_4_4:1.5.12.1
	REL9_0_BETA1:1.6
	REL9_0_ALPHA5_BRANCH:1.6.0.4
	REL9_0_ALPHA5:1.6
	REL7_4_28:1.4
	REL8_0_24:1.5
	REL8_1_20:1.5
	REL8_2_16:1.5
	REL8_3_10:1.5
	REL8_4_3:1.5.12.1
	REL9_0_ALPHA4:1.6
	REL9_0_ALPHA4_BRANCH:1.6.0.2
	REL8_5_ALPHA3:1.5
	REL8_5_ALPHA3_BRANCH:1.5.0.18
	REL7_4_27:1.4
	REL8_0_23:1.5
	REL8_1_19:1.5
	REL8_2_15:1.5
	REL8_3_9:1.5
	REL8_4_2:1.5
	REL8_5_ALPHA2:1.5
	REL8_5_ALPHA2_BRANCH:1.5.0.16
	REL7_4_26:1.4
	REL8_0_22:1.5
	REL8_1_18:1.5
	REL8_2_14:1.5
	REL8_3_8:1.5
	REL8_4_1:1.5
	REL8_5_ALPHA1:1.5
	REL8_5_ALPHA1_BRANCH:1.5.0.14
	REL8_4_STABLE:1.5.0.12
	REL8_4_0:1.5
	REL8_4_RC2:1.5
	REL8_4_RC1:1.5
	REL8_4_BETA2:1.5
	REL8_4_BETA1:1.5
	REL7_4_25:1.4
	REL8_0_21:1.5
	REL8_1_17:1.5
	REL8_2_13:1.5
	REL8_3_7:1.5
	REL7_4_24:1.4
	REL8_0_20:1.5
	REL8_1_16:1.5
	REL8_2_12:1.5
	REL8_3_6:1.5
	REL7_4_23:1.4
	REL8_0_19:1.5
	REL8_1_15:1.5
	REL8_2_11:1.5
	REL8_3_5:1.5
	REL7_4_22:1.4
	REL8_0_18:1.5
	REL8_1_14:1.5
	REL8_2_10:1.5
	REL8_3_4:1.5
	REL7_4_21:1.4
	REL8_0_17:1.5
	REL8_1_13:1.5
	REL8_2_9:1.5
	REL8_3_3:1.5
	REL7_4_20:1.4
	REL8_0_16:1.5
	REL8_1_12:1.5
	REL8_2_8:1.5
	REL8_3_2:1.5
	REL8_2_7:1.5
	REL8_3_1:1.5
	REL8_3_STABLE:1.5.0.10
	REL8_3_0:1.5
	REL8_3_RC2:1.5
	REL7_3_21:1.4
	REL7_4_19:1.4
	REL8_0_15:1.5
	REL8_1_11:1.5
	REL8_2_6:1.5
	REL8_3_RC1:1.5
	REL8_3_BETA4:1.5
	REL8_3_BETA3:1.5
	REL8_3_BETA2:1.5
	REL8_3_BETA1:1.5
	REL7_3_20:1.4
	REL7_4_18:1.4
	REL8_0_14:1.5
	REL8_1_10:1.5
	REL8_2_5:1.5
	REL7_3_19:1.4
	REL7_4_17:1.4
	REL8_0_13:1.5
	REL8_1_9:1.5
	REL8_2_4:1.5
	REL8_0_12:1.5
	REL8_1_8:1.5
	REL8_2_3:1.5
	REL7_3_18:1.4
	REL7_4_16:1.4
	REL8_0_11:1.5
	REL8_1_7:1.5
	REL8_2_2:1.5
	REL8_0_10:1.5
	REL8_1_6:1.5
	REL8_2_1:1.5
	REL7_4_15:1.4
	REL7_3_17:1.4
	REL8_2_STABLE:1.5.0.8
	REL8_2_0:1.5
	REL8_2_RC1:1.5
	REL8_2_BETA3:1.5
	REL8_2_BETA2:1.5
	REL8_1_5:1.5
	REL8_0_9:1.5
	REL7_4_14:1.4
	REL7_3_16:1.4
	REL8_2_BETA1:1.5
	REL7_3_15:1.4
	REL7_4_13:1.4
	REL8_0_8:1.5
	REL8_1_4:1.5
	REL7_3_14:1.4
	REL7_4_12:1.4
	REL8_0_7:1.5
	REL8_1_3:1.5
	REL7_3_13:1.4
	REL7_4_11:1.4
	REL8_0_6:1.5
	REL8_1_2:1.5
	REL7_3_12:1.4
	REL7_4_10:1.4
	REL8_0_5:1.5
	REL8_1_1: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_1_STABLE:1.3.0.6
	REL7_1_BETA:1.3
	REL7_1_BETA3:1.3
	REL7_1_BETA2:1.3
	REL7_1:1.3
	REL7_0_PATCHES:1.3.0.4
	REL7_0:1.3
	REL6_5_PATCHES:1.3.0.2
	REL6_5:1.3
	REL6_4:1.2.0.2
	release-6-3:1.2
	REL2_0B:1.1.1.1.0.4
	REL2_0:1.1.1.1
	Release_2_0_0:1.1.1.1
	Release_1_0_3:1.1.1.1.0.2
	Release_2_0:1.1.1.1
	Release_1_0_2:1.1.1.1
	PG95-1_01:1.1.1.1
	PG95_DIST:1.1.1;
locks; strict;
comment	@# @;


1.6
date	2010.02.02.18.52.02;	author momjian;	state Exp;
branches;
next	1.5;

1.5
date	2003.11.29.22.41.33;	author pgsql;	state Exp;
branches
	1.5.12.1;
next	1.4;

1.4
date	2001.09.02.23.27.50;	author petere;	state Exp;
branches;
next	1.3;

1.3
date	99.07.08.15.27.01;	author momjian;	state Exp;
branches;
next	1.2;

1.2
date	97.06.25.19.59.57;	author momjian;	state Exp;
branches;
next	1.1;

1.1
date	96.07.09.06.22.34;	author scrappy;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	96.07.09.06.22.34;	author scrappy;	state Exp;
branches;
next	;

1.5.12.1
date	2010.02.02.18.52.06;	author momjian;	state Exp;
branches;
next	;


desc
@@


1.6
log
@Remove copyright mention of Andrew Yu, per author's permission.

Backpatch to 8.4.X.
@
text
@---------------------------------------------------------------------------
--
-- basics.sql-
--    Tutorial on the basics (table creation and data manipulation)
--
--
-- $PostgreSQL: pgsql/src/tutorial/basics.source,v 1.5 2003/11/29 22:41:33 pgsql Exp $
--
---------------------------------------------------------------------------

-----------------------------
-- Creating a New Table:
--	A CREATE TABLE is used to create base tables.  PostgreSQL has
--	its own set of built-in types.  (Note that SQL is case-
--	insensitive.)
-----------------------------

CREATE TABLE weather (
	city		varchar(80),
	temp_lo		int,		-- low temperature
	temp_hi		int,		-- high temperature
	prcp		real,		-- precipitation
	date		date
);

CREATE TABLE cities (
	name		varchar(80),
	location	point
);


-----------------------------
-- Populating a Table With Rows:
--	An INSERT statement is used to insert a new row into a table.  There 
--	are several ways you can specify what columns the data should go to.
-----------------------------

-- 1. The simplest case is when the list of value correspond to the order of
--    the columns specified in CREATE TABLE.

INSERT INTO weather 
    VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');

INSERT INTO cities 
    VALUES ('San Francisco', '(-194.0, 53.0)');

-- 2. You can also specify what column the values correspond to.  (The columns
--    can be specified in any order.  You may also omit any number of columns,
--    e.g., unknown precipitation below.

INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
    VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');

INSERT INTO weather (date, city, temp_hi, temp_lo)
    VALUES ('1994-11-29', 'Hayward', 54, 37);


-----------------------------
-- Querying a Table:
--	A SELECT statement is used for retrieving data.  The basic syntax is
--	SELECT columns FROM tables WHERE predicates.
-----------------------------

-- A simple one would be:

SELECT * FROM weather;

-- You may also specify expressions in the target list.  (The 'AS column'
-- specifies the column name of the result.  It is optional.)

SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;

-- If you want to retrieve rows that satisfy certain condition (i.e., a
-- restriction), specify the condition in WHERE.  The following retrieves
-- the weather of San Francisco on rainy days.

SELECT *
    FROM weather
    WHERE city = 'San Francisco' 
        AND prcp > 0.0;

-- Here is a more complicated one.  Duplicates are removed when DISTINCT is
-- specified. ORDER BY specifies the column to sort on.  (Just to make sure the
-- following won't confuse you, DISTINCT and ORDER BY can be used separately.)

SELECT DISTINCT city
    FROM weather
    ORDER BY city;


-----------------------------
-- Joins Between Tables:
--	queries can access multiple tables at once or access the same table
--	in such a way that multiple instances of the table are being processed
--	at the same time.
-----------------------------

-- The following joins the weather table and the cities table.

SELECT *
    FROM weather, cities
    WHERE city = name;

-- This prevents a duplicate city name column:

SELECT city, temp_lo, temp_hi, prcp, date, location
    FROM weather, cities
    WHERE city = name;

-- since the column names are all different, we don't have to specify the
-- table name. If you want to be clear, you can do the following. They give
-- identical results, of course.

SELECT weather.city, weather.temp_lo, weather.temp_hi, weather.prcp, weather.date, cities.location
    FROM weather, cities
    WHERE cities.name = weather.city;

-- JOIN syntax

SELECT *
    FROM weather JOIN cities ON (weather.city = cities.name);

-- Outer join

SELECT *
    FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);

-- Suppose we want to find all the records that are in the temperature range
-- of other records.  W1 and W2 are aliases for weather.

SELECT W1.city, W1.temp_lo, W1.temp_hi, 
       W2.city, W2.temp_lo, W2.temp_hi
FROM weather W1, weather W2
WHERE W1.temp_lo < W2.temp_lo 
   and W1.temp_hi > W2.temp_hi;


-----------------------------
-- Aggregate Functions
-----------------------------

SELECT max(temp_lo)
    FROM weather;

SELECT city FROM weather
    WHERE temp_lo = (SELECT max(temp_lo) FROM weather);

-- Aggregate with GROUP BY
SELECT city, max(temp_lo)
    FROM weather 
    GROUP BY city;

-- ... and HAVING
SELECT city, max(temp_lo)
    FROM weather
    GROUP BY city
    HAVING max(temp_lo) < 40;


-----------------------------
-- Updates:
--	An UPDATE statement is used for updating data.
-----------------------------

-- Suppose you discover the temperature readings are all off by 2 degrees as
-- of Nov 28, you may update the data as follow:

UPDATE weather
    SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2
    WHERE date > '1994-11-28';

SELECT * FROM weather;


-----------------------------
-- Deletions:
--	A DELETE statement is used for deleting rows from a table.
-----------------------------

-- Suppose you are no longer interested in the weather of Hayward, then you can
-- do the following to delete those rows from the table.

DELETE FROM weather WHERE city = 'Hayward';

SELECT * FROM weather;

-- You can also delete all the rows in a table by doing the following.  (This
-- is different from DROP TABLE which removes the table in addition to the 
-- removing the rows.)

DELETE FROM weather;

SELECT * FROM weather;


-----------------------------
-- Removing the tables:
--	DROP TABLE is used to remove tables.  After you have done this, you
--      can no longer use those tables.
-----------------------------

DROP TABLE weather, cities;
@


1.5
log
@
make sure the $Id tags are converted to $PostgreSQL as well ...
@
text
@d7 1
a7 3
-- Copyright (c) 1994, Andrew Yu, University of California
--
-- $PostgreSQL: basics.source,v 1.4 2001/09/02 23:27:50 petere Exp $
@


1.5.12.1
log
@Remove copyright mention of Andrew Yu, per author's permission.

Backpatch to 8.4.X.
@
text
@d7 3
a9 1
-- $PostgreSQL: pgsql/src/tutorial/basics.source,v 1.5 2003/11/29 22:41:33 pgsql Exp $
@


1.4
log
@New blood and fresh air for tutorial
@
text
@d9 1
a9 1
-- $Id: basics.source,v 1.3 1999/07/08 15:27:01 momjian Exp $
@


1.3
log
@Rename example temp to mytemp.
@
text
@d9 1
a9 1
-- $Id: basics.source,v 1.2 1997/06/25 19:59:57 momjian Exp $
d14 4
a17 4
-- Creating a table:
--	a CREATE TABLE is used to create base tables. POSTGRES SQL has
--	its own set of built-in types. (Note that keywords are case-
--	insensitive but identifiers are case-sensitive.)
d24 1
a24 1
	prcp		float8,		-- precipitation
d33 1
d35 2
a36 2
-- Inserting data:
--	an INSERT statement is used to insert a new row into a table. There 
d40 1
a40 1
-- 1. the simplest case is when the list of value correspond to the order of
d44 1
a44 1
   VALUES ('San Francisco', 46, 50, 0.25, '11/27/1994');
d47 1
a47 1
   VALUES ('San Francisco', '(-194.0, 53.0)');
d49 3
a51 3
-- 2. you can also specify what column the values correspond to. (The columns
--    can be specified in any order. You may also omit any number of columns.
--    eg. unknown precipitation below)
d54 1
a54 1
   VALUES ('San Francisco', 43, 57, 0.0, '11/29/1994');
d57 1
a57 1
   VALUES ('11/29/1994', 'Hayward', 54, 37);
d61 3
a63 3
-- Retrieving data:
--	a SELECT statement is used for retrieving data. The basic syntax is
--		SELECT columns FROM tables WHERE predicates
d66 1
a66 1
-- a simple one would be
d70 2
a71 2
-- you may also specify expressions in the target list (the 'AS column'
-- specifies the column name of the result. It is optional.)
d75 2
a76 2
-- if you want to retrieve rows that satisfy certain condition (ie. a
-- restriction), specify the condition in WHERE. The following retrieves
d80 3
a82 3
FROM weather
WHERE city = 'San Francisco' 
   and prcp > 0.0;
d84 2
a85 2
-- here is a more complicated one. Duplicates are removed when DISTINCT is
-- specified. ORDER BY specifies the column to sort on. (Just to make sure the
d89 3
a91 2
FROM weather
ORDER BY city;
d94 4
a97 3
-- Retrieving data into other classes:
--	a SELECT ... INTO statement can be used to retrieve data into
--	another class.
d100 1
a100 4
SELECT * INTO TABLE mytemp 
FROM weather
WHERE city = 'San Francisco' 
   and prcp > 0.0;
d102 3
a104 1
SELECT * from mytemp;
d106 15
a120 3
-----------------------------
-- Aggregates
-----------------------------
d122 2
a123 2
SELECT max(temp_lo)
FROM weather;
d125 1
a125 4
-- Aggregate with GROUP BY
SELECT city, max(temp_lo)
FROM weather 
GROUP BY city;
d127 2
a128 6
-----------------------------
-- Joining tables:
--	queries can access multiple tables at once or access the same table
--	in such a way that multiple instances of the table are being processed
--	at the same time.
-----------------------------
d130 2
a131 2
-- suppose we want to find all the records that are in the temperature range
-- of other records. W1 and W2 are aliases for weather.
a138 2
-- let's join two tables. The following joins the weather table
-- and the cities table.
d140 14
a153 3
SELECT city, location, prcp, date
FROM weather, cities
WHERE name = city;
d155 5
a159 3
-- since the column names are all different, we don't have to specify the
-- table name. If you want to be clear, you can do the following. They give
-- identical results, of course.
a160 3
SELECT w.city, c.location, w.prcp, w.date
FROM weather w, cities c
WHERE c.name = w.city;
d163 2
a164 2
-- Updating data:
--	an UPDATE statement is used for updating data. 
d167 1
a167 1
-- suppose you discover the temperature readings are all off by 2 degrees as
d171 2
a172 2
  SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2
  WHERE date > '11/28/1994';
d174 1
a174 1
SELECT * from weather;
d178 2
a179 2
-- Deleting data:
--	a DELETE statement is used for deleting rows from a table.
d182 2
a183 2
-- suppose you are no longer interested in the weather of Hayward, you can
-- do the following to delete those rows from the table
d187 1
a187 1
SELECT * from weather;
d189 1
a189 1
-- you can also delete all the rows in a table by doing the following. (This
d195 2
a196 1
SELECT * from weather;
d200 1
a200 1
--	DROP TABLE is used to remove tables. After you have done this, you
d204 1
a204 1
DROP TABLE weather, cities, mytemp;
@


1.2
log
@Typo changes in tests/ examples.
@
text
@d9 1
a9 1
-- $Id: basics.source,v 1.1.1.1 1996/07/09 06:22:34 scrappy Exp $
d97 1
a97 1
SELECT * INTO TABLE temp 
d102 1
a102 1
SELECT * from temp;
d188 1
a188 1
DROP TABLE weather, cities, temp;
@


1.1
log
@Initial revision
@
text
@d9 1
a9 1
-- $Id: basics.source,v 1.1.1.1 1996/07/09 05:32:54 scrappy Exp $
d26 1
a26 1
)
d43 1
a43 1
   VALUES ('San Francisco', 46, 50, 0.25, '11/27/1994')
d53 1
a53 1
   VALUES ('San Francisco', 43, 57, 0.0, '11/29/1994')
@


1.1.1.1
log
@Postgres95 1.01 Distribution - Virgin Sources
@
text
@@
