diff --git a/create_tables.sql b/create_tables.sql new file mode 100644 index 0000000..78d6933 --- /dev/null +++ b/create_tables.sql @@ -0,0 +1,37 @@ +create table people( + id serial primary key, + name varchar(255) not null, + dob date, + email varchar(255) +); + +create table directors( + id serial primary key, + country varchar(255), + personId int references people(id) not null, +); + + +create table films( + id serial primary key, + title varchar(255) not null unique, + release_year int not null, + score int not null, + directorId int references directors(id) not null, + starId int references people(id) not null, + writerId int references people(id) not null, + genreId int references genre(id) not null +); + + +create table genres( + id serial primary key, + name varchar(255) not null unique, +); + +create table cast( + id serial primary key, + personId int references people(id) not null, + filmId int references films(id) not null +); + diff --git a/design.md b/design.md new file mode 100644 index 0000000..01d0abb --- /dev/null +++ b/design.md @@ -0,0 +1,34 @@ +## Person + +- int id pk +- string name +- datetime dob +- string email + + +## Director + +- int id pk +- string country +- int personId fk + +## Movie + +- int id pk +- string title +- int releaseYear +- int score +- int directorId fk +- int star fk +- int writer fk +- int genre fk + +## Genre + +- int id pk +- string name + +## Cast + +- int personId fk pk +- int movieId fk pk diff --git a/populate_tables.sql b/populate_tables.sql new file mode 100644 index 0000000..dab329f --- /dev/null +++ b/populate_tables.sql @@ -0,0 +1,56 @@ +insert into genres(name) values ('Science Fiction'), ('Drama'), ('Romance'), ('Historical'), ('Thriller'); +insert into people(name, dob, email) values + ('Stanley Kubrick', null, null), + ('George Lucas', null, 'george@email.com'), + ('Robert Mulligan', null, null), + ('James Cameron', null, 'james@cameron.com'), + ('David Lean', null, null), + ('Anthony Mann', null, null), + ('Theodoros Angelopoulos', null, 'theo@angelopoulos.com'), + ('Paul Verhoeven', null, null), + ('Krzysztof Kieslowski', null, null), + ('Jean-Paul Rappeneau', null, null), +-- 11 + ('Keir Dullea', '30/05/1936', null), + ('Mark Hamill', '25/09/1951', null), + ('Gregory Peck', '05/04/1916', null), + ('Leonardo DiCaprio', '11/11/1974', null), + ('Julie Christie', '14/04/1940', null), + ('Charlton Heston', '04/10/1923', null), + ('Manos Katrakis', '14/08/1908', null), + ('Rutger Hauer', '23/01/1944', null), + ('Juliette Binoche', '09/03/1964', null), + ('Gerard Depardieu', '27/12/1948', null), +-- 21 + ('Arthur C Clarke', null, 'arthur@clarke.com'), + ('Harper Lee', null, 'harper@lee.com'), + ('Boris Pasternak', null, 'boris@boris.com'), + ('Frederick Frank', null, 'fred@frank.com'), + ('Erik Hazelhoff Roelfzema', null, 'erik@roelfzema.com'), + ('Krzysztof Kieslowski', null, 'email@email.com'), + ('Edmond Rostand', null, 'edmond@rostand.com'); + +insert into directors(country, personId) values + ('USA', 1), + ('USA', 2), + ('USA', 3), + ('Canada', 4), + ('UK', 5), + ('USA', 6), + ('Greece', 7), + ('Netherlands', 8), + ('Poland', 9), + ('France', 10); + +insert into films(title, release_year, score, directorId, starId, writerId, genreId) values + ('2001: A Space Odyssey', 1968, 10, 1, 11, 21, 1), + ('Star Wars: A New Hope', 1977, 7, 2, 12, 2, 1), + ('To Kill A Mockingbird', 1962, 10, 3, 13, 22, 2), + ('Titanic', 1997, 5, 4, 14, 4, 3), + ('Dr Zhivago', 1965, 8, 5, 15, 23, 4), + ('El Cid', 1961, 6, 6, 16, 24, 4), + ('Voyage to Cythera', 1984, 8, 7, 17, 7, 2), + ('Soldier of Orange', 1977, 8, 8, 18, 25, 5), + ('Three Colours: Blue', 1993, 8, 9, 19, 26, 2), + ('Cyrano de Bergerac', 1990, 9, 10, 20, 27, 3), + diff --git a/q1.sql b/q1.sql new file mode 100644 index 0000000..bf33d4a --- /dev/null +++ b/q1.sql @@ -0,0 +1,6 @@ +-- Show the title and director name for all films +select title, director_name +from films + inner join directorPerson + on films.directorId = directorPerson.id; + diff --git a/q2.sql b/q2.sql new file mode 100644 index 0000000..377b5c5 --- /dev/null +++ b/q2.sql @@ -0,0 +1,6 @@ +-- Show the title, director and star name for all films +select title, director_name, people.name star_name +from directorFilms + inner join people + on directorFilms.starId = people.id; + diff --git a/q3.sql b/q3.sql new file mode 100644 index 0000000..eed489a --- /dev/null +++ b/q3.sql @@ -0,0 +1,5 @@ +-- Show the title of films where the director is from the USA +select title +from directorFilms +where director_country = "USA"; + diff --git a/q4.sql b/q4.sql new file mode 100644 index 0000000..722b480 --- /dev/null +++ b/q4.sql @@ -0,0 +1,5 @@ +-- Show only those films where the writer and the director are the same person +select title +from films +where directorId = writerId; + diff --git a/q5.sql b/q5.sql new file mode 100644 index 0000000..c19b5bc --- /dev/null +++ b/q5.sql @@ -0,0 +1,5 @@ +--Show directors and film titles for films with a score of 8 or higher +select title, director_name +from directorFilms +where score >= 8; + diff --git a/q6.sql b/q6.sql new file mode 100644 index 0000000..5955361 --- /dev/null +++ b/q6.sql @@ -0,0 +1,30 @@ +--Make at least 5 more queries to demonstrate your understanding of joins, and other relationships between tables. + +create view directorPerson as + select name director_name, country director_country + from directors inner join people + on people.id = directors.personId; + +create view directorFilms as + select *, + from films + inner join directorPerson on films.directorId = directorPerson.id; + +create view directorStarFilms as + select directorFilms.*, dob star_dob, name star_name from + from directorFilms + inner join people on directorFilms.starId = people.id; + +-- Show all films Leonardo DiCaprio has starred in +select title +from films +inner join people + on films.starId = people.id +where people.name = "Leonardo DiCaprio"; + +-- Show drama films +select title +from films +inner join genres + on films.genreId = genres.id +where genres.name = "Drama";