これから tutorial の 世界に 往きます
移動します そうして 現状を 認識すると
$ cd /usr/local/src/postgresql/postgresql-8.1.1/src/tutorial/
$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(3 rows)
tutorial に 使用する新しい データーベースを 作成します。
$ createdb CREATE DATABASE
データーベース の 名前を書いてあげないと ユーザーの 名前で作成されま す。この場合 ユーザーの名前は ”nyanco”です。
$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
nyanco | nyanco | EUC_JP
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(4 rows)
その前に これから使う ファイルを 見てみましょう まずどんなものか?
# file basics.s* basics.source: ASCII English text basics.sql: ASCII English text # ls -l basics.s* -rw-r--r-- 1 mm users 5743 Nov 30 2003 basics.source -rw-r--r-- 1 mm users 5743 Dec 22 21:01 basics.sql # diff -s basics.source basics.sql Files basics.source and basics.sql are identical
このファイルを使用して tutorial の 世界に 往くのですが 内容の 抜粋を 見てみましょう。
-- basics.sql-
-- Tutorial on the basics (table creation and data manipulation)
--
--
-- Copyright (c) 1994, Andrew Yu, University of California
--
-- $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
);
どうも 最初の "--" で 始まるのは コメントの ようです。 ほたら 取り掛 かります ”-s ” というのは”一時停止する”ということの ようです。
http://www.postgresql.jp/document/pg810doc/html/tutorial-sql.html
\iは、指定したファイルからコマンドを読み込みます。
-sオプションによって、それぞれの文をサーバに送る前に一時停止する、シン グルステップモードとなります。 本節で使用するコマンドはbasics.sourceファイル内にあります。
てなことの 様です。データーベースの 名前を指定せずに立ち上げると ユー ザーの 名前の データーベースを 取り扱う事になります。
$ psql -s
Welcome to psql 8.1.1, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
nyanco=>
nyanco=> \i basics.sql /* へい ここから 始まり はじまり です。 */
***(Single step mode: verify command)*******************************************
/* この部分は 表示されません ここから つまり 入力ファイル ”basics.sql” に有る物です */
-----------------------------
-- 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,
temp_hi int,
prcp real,
date date
);
***(press return to proceed or enter x and return to cancel)********************
/* わかり難くいえば ”weather”という名前の ”TABLE” を 作成しよう
項目 city というのは 文字 80 個 の 領域
項目 temp_lo というのは 整数 の 領域
項目 temp_hi というのは 整数 の 領域
項目 prcp というのは 実数 の 領域
項目 date というのは 日付け 型 の 領域
を 表します 取り敢えずは この様な 感じで良いでしょう。
都市の名前と 最低最高温度 降水量 と 其の日付け を 内容とする TABLE
を ”weather” という名前で もって 作成すると いうことです。当然此処の
データーは まだ 記入は していない 状態です */
CREATE TABLE
/* ”TABLE”が 出来ましたと いってます。 */
***(Single step mode: verify command)*******************************************
CREATE TABLE cities (
name varchar(80),
location point
);
***(press return to proceed or enter x and return to cancel)********************
/* このような 状態で 待機します つまり 次は ” cities ”
という ”TABLE ”を 作成し 其の内容は 云々 ということです */
CREATE TABLE
***(Single step mode: verify command)*******************************************
INSERT INTO weather
VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
***(press return to proceed or enter x and return to cancel)********************
/* ここで 再度 小休止 (待機)に なります。ENTER
キーにて 進みます 表現は ”press return to proceed” ということです
キーワード ”INSERT”です
INSERT :データーの 挿入
INTO : 何処に
weather : ”weather”という ”TABLE”に
VALUES : 内容は
CREATE TABLE weather (
city varchar(80),
temp_lo int,
temp_hi int,
prcp real,
date date
);
に 対応させて
'San Francisco', 46, 50, 0.25, '1994-11-27' を 挿入
都市 最低温度 最高温度 降水量 日付け
*/
INSERT 0 1
***(Single step mode: verify command)*******************************************
INSERT INTO cities
VALUES ('San Francisco', '(-194.0, 53.0)');
***(press return to proceed or enter x and return to cancel)********************
/* こんどは 別の ”cities”という ”TABLE”に
データーを 挿入 します 同様に
'San Francisco', '(-194.0, 53.0)'
都市 位置情報
この情報 は何を 表してるのだろう? 通常ならば
Coordinates
37°46′0″ N
122°26′0″ W
であろうが まあそれはさておき */
INSERT 0 1
***(Single step mode: verify command)*******************************************
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
***(press return to proceed or enter x and return to cancel)********************
/* 日付け の 異なるデータ */
INSERT 0 1
***(Single step mode: verify command)*******************************************
INSERT INTO weather (date, city, temp_hi, temp_lo)
VALUES ('1994-11-29', 'Hayward', 54, 37);
***(press return to proceed or enter x and return to cancel)********************
/* 都市の名前が 異なる 対応の 順序に 注意 */
INSERT 0 1
***(Single step mode: verify command)*******************************************
SELECT * FROM weather;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 43 | 57 | 0 | 1994-11-29
Hayward | 37 | 54 | | 1994-11-29
(3 rows)
/* ”weather””TABLE”の全てを 表示
SELECT 選びます
* 一切合財
FROM weather ここから:: ”weather” から
*/
***(Single step mode: verify command)*******************************************
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
***(press return to proceed or enter x and return to cancel)********************
city | temp_avg | date
---------------+----------+------------
San Francisco | 48 | 1994-11-27
San Francisco | 50 | 1994-11-29
Hayward | 45 | 1994-11-29
(3 rows)
/*
FROM weather : ”weather” という ”TABLE”から
city 都市 と
temp_avg という名前で (temp_hi+temp_lo)/2 最低と 最高の 平均を 表示
date 日時も 会わせて
という感じ
*/
***(Single step mode: verify command)*******************************************
SELECT *
FROM weather
WHERE city = 'San Francisco'
AND prcp > 0.0;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
(1 row)
/*
条件を 追記
FROM weather : ”weather” という ”TABLE”から
WHERE city 都市の名前は ”San Francisco”
降雨の 量 0.0 以上(正確には 降水量 が 0.0 を 越えるというのが 良いでしょう以上は 含むが 今回は 含まない )
*/
***(Single step mode: verify command)*******************************************
SELECT DISTINCT city
FROM weather
ORDER BY city;
***(press return to proceed or enter x and return to cancel)********************
city
---------------
Hayward
San Francisco
(2 rows)
***(Single step mode: verify command)*******************************************
/* まあ これは city に 関して 順番に 表示を
DISTINCT とあるから ”TABLE” というのは ”weather”です が 重複は 表示
しないと いう事です */
SELECT *
FROM weather, cities
WHERE city = name;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
(2 rows)
/* これは 何をしてるのでしょう?? 望んだ物でしょうか? */
***(Single step mode: verify command)*******************************************
SELECT city, temp_lo, temp_hi, prcp, date, location
FROM weather, cities
WHERE city = name;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date | location
---------------+---------+---------+------+------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | (-194,53)
(2 rows)
/* これも また 何をしてるのでしょう?? 望んだ物でしょうか? */
***(Single step mode: verify command)*******************************************
SELECT weather.city, weather.temp_lo, weather.temp_hi, weather.prcp, weather.date, cities.location
FROM weather, cities
WHERE cities.name = weather.city;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date | location
---------------+---------+---------+------+------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | (-194,53)
(2 rows)
/* おいおい? また 何をしてるのでしょう?? 望んだ物でしょうか? */
***(Single step mode: verify command)*******************************************
SELECT *
FROM weather JOIN cities ON (weather.city = cities.name);
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
(2 rows)
/* ううううん これもまた、何をしてるのでしょう?? 望んだ物でしょうか? */
***(Single step mode: verify command)*******************************************
SELECT *
FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
Hayward | 37 | 54 | | 1994-11-29 | |
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
(3 rows)
/* 何か抜けてるが いいのか?望んだ物でしょうか? */
***(Single step mode: verify command)*******************************************
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;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | city | temp_lo | temp_hi
---------------+---------+---------+---------------+---------+---------
San Francisco | 43 | 57 | San Francisco | 46 | 50
Hayward | 37 | 54 | San Francisco | 46 | 50
(2 rows)
/* これは これで 良いのかも? */
***(Single step mode: verify command)*******************************************
SELECT max(temp_lo)
FROM weather;
***(press return to proceed or enter x and return to cancel)********************
max
-----
46
(1 row)
/* そうですな! */
***(Single step mode: verify command)*******************************************
SELECT city FROM weather
WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
***(press return to proceed or enter x and return to cancel)********************
city
---------------
San Francisco
(1 row)
/* なんとなく そうか! */
***(Single step mode: verify command)*******************************************
SELECT city, max(temp_lo)
FROM weather
GROUP BY city;
***(press return to proceed or enter x and return to cancel)********************
city | max
---------------+-----
Hayward | 37
San Francisco | 46
(2 rows)
/* うううううんんーーー */
***(Single step mode: verify command)*******************************************
SELECT city, max(temp_lo)
FROM weather
GROUP BY city
HAVING max(temp_lo) < 40;
***(press return to proceed or enter x and return to cancel)********************
city | max
---------+-----
Hayward | 37
(1 row)
/* 再度見直す事 */
***(Single step mode: verify command)*******************************************
UPDATE weather
SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2
WHERE date > '1994-11-28';
***(press return to proceed or enter x and return to cancel)********************
UPDATE 2
***(Single step mode: verify command)*******************************************
/* そんでもって */
SELECT * FROM weather;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 41 | 55 | 0 | 1994-11-29
Hayward | 35 | 52 | | 1994-11-29
(3 rows)
/* そうなんですか?”1994-11-27”は関係なしに なるんですか */
***(Single step mode: verify command)*******************************************
DELETE FROM weather WHERE city = 'Hayward';
***(press return to proceed or enter x and return to cancel)********************
DELETE 1
***(Single step mode: verify command)*******************************************
SELECT * FROM weather;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 41 | 55 | 0 | 1994-11-29
(2 rows)
/* DROP では 有りません 最初は 十分に 取り扱いませう */
***(Single step mode: verify command)*******************************************
DELETE FROM weather;
***(press return to proceed or enter x and return to cancel)********************
DELETE 2
***(Single step mode: verify command)*******************************************
SELECT * FROM weather;
***(press return to proceed or enter x and return to cancel)********************
city | temp_lo | temp_hi | prcp | date
------+---------+---------+------+------
(0 rows)
/* 再度 DROP では 有りません 最初は 十分に 取り扱いませう */
***(Single step mode: verify command)*******************************************
DROP TABLE weather, cities;
***(press return to proceed or enter x and return to cancel)********************
DROP TABLE
nyanco=>
nyanco=> \q
もう一度 もう一度 もう一度 ですな 確認すると
$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
nyanco | nyanco | EUC_JP
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(4 rows)
$
おかたずけ
$ dropdb nyanco
DROP DATABASE
$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(3 rows)
もう2、3回 練習した方が 良いようですな!!
✈
にゃんたろう 拝!
2006年 4月29日 (土) 21:26:32 JST 作成