example PostgreSQL: switch from sh to SQL.

This commit is contained in:
Kei Hibino 2017-01-28 17:03:31 +09:00
parent 01acb337f1
commit fe6da06983
4 changed files with 40 additions and 69 deletions

View File

@ -1,61 +0,0 @@
#! /bin/sh
create_user_table='
CREATE TABLE EXAMPLE1.user (
id integer NOT NULL,
name VARCHAR(128),
PRIMARY KEY(id)
)
'
create_group_table='
CREATE TABLE EXAMPLE1.group (
id integer NOT NULL,
name VARCHAR(128),
PRIMARY KEY(id)
)
'
create_membership_table='
CREATE TABLE EXAMPLE1.membership (
user_id integer NOT NULL,
group_id integer NOT NULL
)
'
set -x
psql -c "CREATE SCHEMA EXAMPLE1" testdb
psql -c "$create_user_table" testdb
psql -c "$create_group_table" testdb
psql -c "$create_membership_table" testdb
insertUser () {
psql -c "INSERT INTO EXAMPLE1.user (id, name) VALUES ($1, '$2')" testdb
}
insertGroup () {
psql -c "INSERT INTO EXAMPLE1.group (id, name) VALUES ($1, '$2')" testdb
}
insertMembership() {
psql -c "INSERT INTO EXAMPLE1.membership (user_id, group_id) VALUES ($1, $2)" testdb
}
insertUser 1 'Kei Hibino'
insertUser 2 'Kazu Yamamoto'
insertUser 3 'Shouhei Murayama'
insertUser 255 '<New-comer>'
insertGroup 1 'Haskell'
insertGroup 2 'C++'
insertGroup 3 'Java'
insertMembership 1 1
insertMembership 2 1
insertMembership 3 1
insertMembership 1 3
insertMembership 3 3

View File

@ -0,0 +1,36 @@
CREATE SCHEMA EXAMPLE1;
CREATE TABLE EXAMPLE1.user (
id integer NOT NULL,
name VARCHAR(128),
PRIMARY KEY(id)
);
CREATE TABLE EXAMPLE1.group (
id integer NOT NULL,
name VARCHAR(128),
PRIMARY KEY(id)
);
CREATE TABLE EXAMPLE1.membership (
user_id integer NOT NULL,
group_id integer NOT NULL
);
INSERT INTO EXAMPLE1.user (id, name) VALUES (1, 'Kei Hibino');
INSERT INTO EXAMPLE1.user (id, name) VALUES (2, 'Kazu Yamamoto');
INSERT INTO EXAMPLE1.user (id, name) VALUES (3, 'Shouhei Murayama');
INSERT INTO EXAMPLE1.user (id, name) VALUES (255, '<New-comer>');
INSERT INTO EXAMPLE1.group (id, name) VALUES (1, 'Haskell');
INSERT INTO EXAMPLE1.group (id, name) VALUES (2, 'C++');
INSERT INTO EXAMPLE1.group (id, name) VALUES (3, 'Java');
INSERT INTO EXAMPLE1.membership (user_id, group_id) VALUES (1, 1);
INSERT INTO EXAMPLE1.membership (user_id, group_id) VALUES (2, 1);
INSERT INTO EXAMPLE1.membership (user_id, group_id) VALUES (3, 1);
INSERT INTO EXAMPLE1.membership (user_id, group_id) VALUES (1, 3);
INSERT INTO EXAMPLE1.membership (user_id, group_id) VALUES (3, 3);

View File

@ -1,8 +0,0 @@
#! /bin/sh
set -x
psql -c "DROP TABLE EXAMPLE1.membership" testdb
psql -c "DROP TABLE EXAMPLE1.group" testdb
psql -c "DROP TABLE EXAMPLE1.user" testdb
psql -c "DROP SCHEMA IF EXISTS EXAMPLE1" testdb

View File

@ -0,0 +1,4 @@
DROP TABLE EXAMPLE1.membership;
DROP TABLE EXAMPLE1.group;
DROP TABLE EXAMPLE1.user;
DROP SCHEMA IF EXISTS EXAMPLE1;