Postgre SQL http www ns kogakuin ac jpct

  • Slides: 43
Download presentation
Postgre. SQL http: //www. ns. kogakuin. ac. jp/~ct 13140/Prog. 2009/ Postgre. SQL-1

Postgre. SQL http: //www. ns. kogakuin. ac. jp/~ct 13140/Prog. 2009/ Postgre. SQL-1

操作 2/22 • 作成した自分用データベースに接続する. /usr/local/pgsql/bin/psql ct 13140 db Password: ←ここでパスワードを入力し「Enter」を押す Welcome to psql 8.

操作 2/22 • 作成した自分用データベースに接続する. /usr/local/pgsql/bin/psql ct 13140 db Password: ←ここでパスワードを入力し「Enter」を押す Welcome to psql 8. 2. 5, the Postgre. SQL 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 ct 13140 db=> ←このように表されればデータベースに接続中. Postgre. SQL-18

操作 4/22 • 再度自分用データベースに接続する. /usr/local/pgsql/bin/psql ct 13140 db Password: ←ここでパスワードを入力し「Enter」を押す Welcome to psql 8.

操作 4/22 • 再度自分用データベースに接続する. /usr/local/pgsql/bin/psql ct 13140 db Password: ←ここでパスワードを入力し「Enter」を押す Welcome to psql 8. 2. 5, the Postgre. SQL interactive (略) ct 13140 db=> ←このように表されればデータベースに接続中. Postgre. SQL-20

操作 6/22 • 表usertblを作る(データベース接続中) . ct 13140 db=> CREATE TABLE usertbl ( ct 13140

操作 6/22 • 表usertblを作る(データベース接続中) . ct 13140 db=> CREATE TABLE usertbl ( ct 13140 db(> id integer, ct 13140 db(> name varchar(100), ct 13140 db(> email varchar(100) ct 13140 db(> ); このように CREATE TABLE 表示されれば成功. ct 13140 db=> 使用したSQL文は CREATE TABLE usertbl (id integer, name varchar(100), email varchar(100)); 途中の改行はあってもなくてもよい. 最後の;(セミコロン)を忘れない様に. Postgre. SQL-22

操作 7/22 • 再度,存在する表を調べる(データベース 接続中). ct 13140 db=> d List of relations Schema |

操作 7/22 • 再度,存在する表を調べる(データベース 接続中). ct 13140 db=> d List of relations Schema | Name | Type | Owner --------+-------+----public | usertbl | table | ct 13140 (1 row) ct 13140 db=> 「操作 7」で作成し た表が表示されれ れば成功. Postgre. SQL-23

操作 10/22 • 表にデータを入力する(データベース接続中). ct 13140 db=> INSERT INTO usertbl VALUES (1, 'abe', 'abe@kantei.

操作 10/22 • 表にデータを入力する(データベース接続中). ct 13140 db=> INSERT INTO usertbl VALUES (1, 'abe', 'abe@kantei. go. jp'); INSERT 0 1 ct 13140 db=> INSERT INTO usertbl VALUES (2, 'koizumi', 'koizumi@kantei. go. jp'); INSERT 0 1 ct 13140 db=> SELECT * FROM usertbl; id | name | email ----+-------------0 | fukuda@kantei. go. jp 1 | abe@kantei. go. jp 2 | koizumi@kantei. go. jp (3 rows) ct 13140 db=> Postgre. SQL-26

操作 11/22 • 表diarytblを作る(データベース接続中). ct 13140 db=> CREATE TABLE diarytbl (diaryid Integer, userid Integer,

操作 11/22 • 表diarytblを作る(データベース接続中). ct 13140 db=> CREATE TABLE diarytbl (diaryid Integer, userid Integer, year Integer, month Integer, day Integer, txt varchar(1000)); CREATE TABLE ct 13140 db=> d List of relations Schema | Name | Type | Owner --------+-------+----public | diarytbl | table | ct 13140 public | usertbl | table | ct 13140 (2 rows) 日記記事1個ごとに 1行の表とする. diaryid: 日記記事の通し番号 ct 13140 db=> Userid: 日記を書いたユーザのID year, month, day: 日記の日付 txt: 日記本文 Postgre. SQL-27

操作 12/22 • 表diarytblにデータを挿入する(データベース接続中). ct 13140 db=> INSERT INTO diarytbl VALUES (0, 0, 2007,

操作 12/22 • 表diarytblにデータを挿入する(データベース接続中). ct 13140 db=> INSERT INTO diarytbl VALUES (0, 0, 2007, 1, 1, 'gantan deshita'); INSERT 0 1 ct 13140 db=> INSERT INTO diarytbl VALUES (1, 0, 2007, 7, 29, 'senkyo deshita'); INSERT 0 1 ct 13140 db=> INSERT INTO diarytbl VALUES (2, 2, 2005, 8, 8, 'kaisan shita'); INSERT 0 1 ct 13140 db=> SELECT * FROM diarytbl; diaryid | userid | year | month | day | txt -----+----+-------+----------0 | 2007 | 1 | gantan deshita 1 | 0 | 2007 | 29 | senkyo deshita 2 | 2005 | 8 | kaisan shita (3 rows) ct 13140 db=> Postgre. SQL-28

操作 13/22 • 現状の確認(データベース接続中). ct 13140 db=> SELECT * FROM usertbl; id | name

操作 13/22 • 現状の確認(データベース接続中). ct 13140 db=> SELECT * FROM usertbl; id | name | email ----+-------------0 | fukuda@kantei. go. jp 1 | abe@kantei. go. jp 2 | koizumi@kantei. go. jp (3 rows) ct 13140 db=> SELECT * FROM diarytbl; diaryid | userid | year | month | day | txt -----+----+-------+----------0 | 2007 | 1 | gantan deshita 1 | 0 | 2007 | 29 | senkyo deshita 2 | 2005 | 8 | kaisan shita (3 rows) ct 13140 db=> Postgre. SQL-29

操作 14/22 • 現状の確認(データベース接続中). ct 13140 db=> SELECT * FROM diarytbl WHERE userid=0; diaryid

操作 14/22 • 現状の確認(データベース接続中). ct 13140 db=> SELECT * FROM diarytbl WHERE userid=0; diaryid | userid | year | month | day | txt -----+----+-------+----------0 | 2007 | 1 | gantan deshita 1 | 0 | 2007 | 29 | senkyo deshita (2 rows) ct 13140 db=> Postgre. SQL-30

操作 15/22 • 現状の確認(データベース接続中). ct 13140 db=> UPDATE diarytbl SET txt='ganjitu deshita' WHERE diaryid=0;

操作 15/22 • 現状の確認(データベース接続中). ct 13140 db=> UPDATE diarytbl SET txt='ganjitu deshita' WHERE diaryid=0; UPDATE 1 ct 13140 db=> SELECT * FROM diarytbl; diaryid | userid | year | month | day | txt -----+----+-------+----------1 | 0 | 2007 | 29 | senkyo deshita 2 | 2005 | 8 | kaisan shita 0 | 2007 | 1 | ganjitu deshita (3 rows) ct 13140 db=> 表内の列の順番は全く保証されていないので, RDBMSが勝手に決めてくる. Postgre. SQL-31

操作 16/22 • 内部結合(データベース接続中). ct 13140 db=> SELECT * FROM usertbl INNER JOIN diarytbl

操作 16/22 • 内部結合(データベース接続中). ct 13140 db=> SELECT * FROM usertbl INNER JOIN diarytbl ON usertbl. id = diarytbl. userid; id | name | email | diaryid | userid | year | month | day | txt ----+--------------+--------+-------+----------0 | fukuda@kantei. go. jp | 0 | 2007 | 1 | ganjitu deshita 0 | fukuda@kantei. go. jp | 1 | 0 | 2007 | 29 | senkyo deshita 2 | koizumi@kantei. go. jp | 2 | 2005 | 8 | kaisan shita (3 rows) ct 13140 db=> Postgre. SQL-32

操作 17/22 • 表内のデータの削除(データベース接続中). ct 13140 db=> DELETE FROM diarytbl WHERE diaryid=0; DELETE 1

操作 17/22 • 表内のデータの削除(データベース接続中). ct 13140 db=> DELETE FROM diarytbl WHERE diaryid=0; DELETE 1 ct 13140 db=> SELECT * FROM diarytbl; diaryid | userid | year | month | day | txt -----+----+-------+----------1 | 0 | 2007 | 29 | senkyo deshita 2 | 2005 | 8 | kaisan shita (2 rows) ct 13140 db=> Postgre. SQL-33

操作 18/22 • 表の削除(データベース接続中). ct 13140 db=> DROP TABLE diarytbl; DROP TABLE ct 13140

操作 18/22 • 表の削除(データベース接続中). ct 13140 db=> DROP TABLE diarytbl; DROP TABLE ct 13140 db=> d List of relations Schema | Name | Type | Owner --------+-------+----public | usertbl | table | ct 13140 (1 row) ct 13140 db=> 表を削除すると, 表内のデータは全て消失するので注意. Postgre. SQL-34

操作 21/22 • 自分用データベースを再度作成する. /usr/local/pgsql/bin/createdb ct 13140 db Password: ←ここでパスワードを入力し「Enter」を押す CREATE DATABASE • 自分用データベースに再度接続.

操作 21/22 • 自分用データベースを再度作成する. /usr/local/pgsql/bin/createdb ct 13140 db Password: ←ここでパスワードを入力し「Enter」を押す CREATE DATABASE • 自分用データベースに再度接続. /usr/local/pgsql/bin/psql ct 13140 db Password: ←ここでパスワードを入力し「Enter」を押す Welcome to psql 8. 2. 5, the Postgre. SQL interactive terminal. (略) ct 13140 db=> d No relations found. ct 13140 db=> 空の(表が1個もない)データベースが 作成された. Postgre. SQL-37