PostgreSQL

Create table for countries:

* CREATE TABLE countries (
* country\_code char(2) PRIMARY KEY,
* country\_name text UNIQUE
* );

Add data for countries:

* INSERT INTO countries (country\_code, country\_name)
* VALUES ('us','United States'), ('mx','Mexico'), ('au','Australia'), ('gb','United Kingdom'),('de','Germany'),('ee','Estonia');

Create table for cities:

* 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)
* );

Add data for cities:

* INSERT INTO cities
* VALUES ('Portland','97206','us');

Create table for venues:

* CREATE TABLE venues (
* venue\_id SERIAL PRIMARY KEY,
* name varchar(255),
* street\_address text,
* type char(7) CHECK (type in ('public','private')) DEFAULT 'public',
* active boolean DEFAULT TRUE,
* postal\_code varchar(9),
* country\_code char(2),
* FOREIGN KEY (country\_code, postal\_code)
* REFERENCES cities (country\_code, postal\_code) MATCH FULL
* );

Add data for venues

* INSERT INTO venues (name, street\_address, postal\_code, country\_code)
* VALUES ('Crystal Ballroom','97206','us');
* INSERT INTO venues (name, postal\_code, country\_code)
* VALUES ('Voodoo Doughnut','97206','us')
* INSERT INTO venues (name, street\_address, postal\_code, country\_code)
* VALUES ('My Place', 'Penn Avenue', 'NW11 0LR', 'gb');

Create table for events:

* CREATE TABLE events (
* events\_id SERIAL PRIMARY KEY,
* title text,
* starts timestamp,
* ends timestamp,
* venue\_id integer,
* FOREIGN KEY (venue\_id)
* REFERENCES venues (venue\_id)
* );

Add data for events

* INSERT INTO events (title, starts, ends, venue\_id)
* VALUES ('Fight Club','2018-02-15 17:30:00','2018-02-15 19:30:00', 2),
* ('April Fools Day', '2018-04-01 00:00:00','2018-04-01 23:59:00', NULL),
* ('Christmas Day', '2018-02-15 19:30:00', '2018-12-25 23:59:00', NULL),
* ('Moby', '2018-02-06 21:00', '2018-02-06 23:00', (SELECT venue\_id FROM venues
* WHERE name = 'Crystal Ballroom')),
* ('Wedding', '2018-02-26 21:00','2018-02-26 23:00', 2),
* ('Dinner with Mom', '2018-02-26 18:00:00','2018-02-26 20:30:00', 3),
* ('Valentine''s Day','2018-02-14 00:00:00', '2018-02-14 23:59:00', NULL);

#### Create countries table with columns 'country code', and 'country name'

* CREATE TABLE countries (
* country\_code char(2) PRIMARY KEY,
* country\_name text UNIQUE
* );

#### Insert data into countries table

* INSERT INTO countries (country\_code, country\_name)
* VALUES ('us','United States'),('mx','Mexico'),('au','Australia'),
* ('gb','United Kingdom'),('de','Germany'),('ll','Loompaland');

#### Delete 'll' from countries table

* DELETE FROM countries
* WHERE country\_code = 'll';

#### Create cities table with columns 'name', 'postal code', and 'country code'

* 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)
* );

Display tables

* SELECT \*
* FROM countries;
* SELECT \*
* FROM cities;

Insert data into cities

* INSERT INTO cities
* VALUES ('Portland','87200','us');

Update cities

* UPDATE cities
* SET postal\_code = '97206'
* WHERE name = 'Portland';

Join 'countries' and 'cities' tables. Will display table of cities with country\_name

* SELECT cities.\*, country\_name
* FROM cities INNER JOIN countries
* ON cities.country\_code = countries.country\_code;

Create 'venues' table with columns 'venue id', 'name', 'street address', 'type', 'postal code', and 'country code'

* 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 data into table 'venues'

* INSERT INTO venues (name, postal\_code, country\_code)
* VALUES ('Crystal Ballroom','97206','us');

Display venue id, venue name, and city name

* 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;

insert data for 'Voodoo Doughnut' into 'Venues' and return 'Venue ID'

* INSERT INTO venues (name, postal\_code, country\_code)
* VALUES ('Voodoo Doughnut','97206','us') RETURNING
* venue\_id;

Create 'events' table

* CREATE TABLE events (
* events\_id SERIAL PRIMARY KEY,
* title text,
* starts timestamp,
* ends timestamp,
* active boolean DEFAULT TRUE,
* venue\_id integer,
* FOREIGN KEY (venue\_id)
* REFERENCES venues (venue\_id)
* );

Insert data into table 'events'

* INSERT INTO events (title, starts, ends, venue\_id, event\_id)
* VALUES ('FIGHT CLUB', '2018-02-15 17:30:00', '2018-02-15 19:30:00', '2', '1');
* INSERT INTO events (title, starts, ends, venue\_id, event\_id)
* VALUES ('April Fools Day','2018-04-01 00:00:00','2018-04-01 23:59:00', '2', '2');
* INSERT INTO events (title, starts, ends, venue\_id, event\_id)
* VALUES ('Christmas Day', '2018-02-15 19:30:00', '2018-12-25 23:59:00', '2', '3');

Return 'event title' and 'venue name'

* SELECT e.title, v.name
* FROM events e LEFT JOIN venues v
* ON e.venue\_id = v.venue\_id;