Skip to content

Instantly share code, notes, and snippets.

@hrstt
Created March 26, 2013 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrstt/5246173 to your computer and use it in GitHub Desktop.
Save hrstt/5246173 to your computer and use it in GitHub Desktop.
7つのデータベース 7つの世界 2章 PostgreSQL 1日目 ref: http://qiita.com/items/18847b18646a1965c398
CREATE TABLE countries (
country_code char(2) PRIMARY KEY,
country_name text UNIQUE
);
INSERT INTO countries (country_code, country_name)
VALUES ('us', 'Unitate States'), ('mx', 'Mexico'), ('au', 'Australia'), ('gb', 'United Kingdom'), ('de', 'Germany'),('ll', 'Loompaland');
INSERT INTO venues (name, postal_code, country_code) VALUES ('Voodoo Donuts', '97205', 'us') RETURNING venue_id;
venue_id
----------
2
(1 row)
INSERT 0 1
CREATE TABLE events (
event_id SERIAL PRIMARY KEY,
title varchar(255),
starts timestamp,
ends timestamp,
venue_id integer REFERENCES venues
);
INSERT INTO events (title, starts, ends, venue_id)
VALUES ('LARP Club', '2012-02-15 17:30:00', '2012-02-15 19:30:00', 2),
('April Fools Day', '2012-04-01 00:00:00', '2012-04-01 23:59:00', NULL),
('Christmas Day', '2012-12-25 00:00:00', '2012-12-25 23:59:00', NULL);
SELECT e.title, v.name
FROM events e JOIN venues v
ON e.venue_id = v.venue_id;
title | name
-----------+---------------
LARP Club | Voodoo Donuts
(1 row)
SELECT e.title, v.name
FROM events e LEFT JOIN venues v
ON e.venue_id = v.venue_id;
title | name
-----------------+---------------
LARP Club | Voodoo Donuts
April Fools Day |
Christmas Day |
(3 rows)
CREATE INDEX events_title ON events USING hash (title);
CREATE INDEX events_start ON events USING btree(starts);
DELETE FROM countries WHERE country_code='ll';
List of relations
Schema | Name | Type | Owner | Table
--------+----------------------------+-------+----------+-----------
public | cities_pkey | index | postgres | cities
public | countries_country_name_key | index | postgres | countries
public | countries_pkey | index | postgres | countries
public | events_pkey | index | postgres | events
public | events_start | index | postgres | events
public | events_title | index | postgres | events
public | venues_pkey | index | postgres | venues
(7 rows)
SELECT * FROM pg_class WHERE relname = 'events';
SELECT c.country_name, e.title
FROM events e
INNER JOIN venues v ON e.venue_id = v.venue_id
INNER JOIN countries c ON v.country_code = c.country_code;
ALTER TABLE venues ADD COLUMN active boolean DEFAULT TRUE;
SELECT * FROM venues;
CREATE TABLE cities (
name text NOT NULL,
postal_code varchar(9) CHECK (postal_code <> ''),
country_code char(2) REFERENCES countries,
PRIMARY KEY (country_code, postal_code)
);
INSERT INTO cities VALUES ('Portland', '87200', 'us');
UPDATE cities SET postal_code='97205' WHERE name = 'Portland';
SELECT cities.*, country_name
FROM cities INNER JOIN countries
ON cities.country_code = countries.country_code;
name | postal_code | country_code | country_name
----------+-------------+--------------+----------------
Portland | 97205 | us | Unitate States
(1 row)
CREATE TABLE venues (
venue_id SERIAL PRIMARY KEY,
name varchar(255),
street_address text,
type char(7) CHECK (type in ('public', 'private')) DEFAULT 'public',
postal_code varchar(9),
country_code char(2),
FOREIGN KEY(country_code, postal_code)
REFERENCES cities (country_code, postal_code) MATCH FULL
);
INSERT INTO venues (name, postal_code, country_code)
VALUES ('Crystal Ballroom', '97205', 'us');
SELECT v.venue_id, v.name, c.name
FROM venues v INNER JOIN cities c
ON v.postal_code = c.postal_code AND v.country_code = c.country_code;
venue_id | name | name
----------+------------------+----------
1 | Crystal Ballroom | Portland
(1 row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment