diff --git a/sql/products/insert-products.sql b/sql/films/extension-1.sql similarity index 100% rename from sql/products/insert-products.sql rename to sql/films/extension-1.sql diff --git a/sql/films/extension-2.sql b/sql/films/extension-2.sql new file mode 100644 index 0000000..0ea7e5d --- /dev/null +++ b/sql/films/extension-2.sql @@ -0,0 +1,50 @@ +DROP TABLE IF EXISTS directors CASCADE; + +CREATE TABLE directors +( + id serial PRIMARY KEY, + name VARCHAR(255) +); + +DROP TABLE IF EXISTS films; + +CREATE TABLE films ( + id serial PRIMARY KEY, + title VARCHAR(255) NOT NULL, + genre VARCHAR(255), + release_year INT, + score INT, + director_id INT, + FOREIGN KEY (director_id) REFERENCES directors(id) + ON DELETE SET NULL, + UNIQUE(title) +); + +INSERT INTO directors (name) VALUES +('Dir1'), +('Dir2'), +('Dir3'), +('Dir4'), +('Dir5'), +('Dir6'); + + +INSERT INTO films (title, genre, release_year, score, director_id) VALUES +('The Shawshank Redemption', 'Drama', 1994, 9, 1), +('The Godfather', 'Crime', 1972, 9, 3), +('The Dark Knight', 'Action', 2008, 9, 5), +('Alien', 'SciFi', 1979, 9, 2), +('Total Recall', 'SciFi', 1990, 8, 4), +('The Matrix', 'SciFi', 1999, 8, 6), +('The Matrix Resurrections', 'SciFi', 2021, 5, 3), +('The Matrix Reloaded', 'SciFi', 2003, 6, 1), +('The Hunt for Red October', 'Thriller', 1990, 7, 5), +('Misery', 'Thriller', 1990, 7, 2), +('The Power Of The Dog', 'Western', 2021, 6, 6), +('Hell or High Water', 'Western', 2016, 8, 4), +('The Good the Bad and the Ugly', 'Western', 1966, 9, 1), +('Unforgiven', 'Western', 1992, 7, 3); + +SELECT films.title, directors.name +FROM films +JOIN directors ON directors.id = films.director_id; \ No newline at end of file diff --git a/sql/films/extension-3.sql b/sql/films/extension-3.sql new file mode 100644 index 0000000..20e9430 --- /dev/null +++ b/sql/films/extension-3.sql @@ -0,0 +1,11 @@ +SELECT directors.name, COUNT(films.id) +FROM directors +LEFT JOIN films ON directors.id = films.director_id +GROUP BY directors.name; + + + + + + + diff --git a/sql/films/setup-films.sql b/sql/films/setup-films.sql new file mode 100644 index 0000000..12ceb48 --- /dev/null +++ b/sql/films/setup-films.sql @@ -0,0 +1,24 @@ +CREATE TABLE films( + id serial PRIMARY KEY, + title VARCHAR(255) NOT NULL, + genre VARCHAR(255), + release_year INT, + score INT, + UNIQUE(title) +); + +INSERT INTO films (title, genre, release_year, score) VALUES +('The Shawshank Redemption', 'Drama', 1994, 9), +('The Godfather', 'Crime', 1972, 9), +('The Dark Knight', 'Action', 2008, 9), +('Alien', 'SciFi', 1979, 9), +('Total Recall', 'SciFi', 1990, 8), +('The Matrix', 'SciFi', 1999, 8), +('The Matrix Resurrections', 'SciFi', 2021, 5), +('The Matrix Reloaded', 'SciFi', 2003, 6), +('The Hunt for Red October', 'Thriller', 1990, 7), +('Misery', 'Thriller', 1990, 7), +('The Power Of The Dog', 'Western', 2021, 6), +('Hell or High Water', 'Western', 2016, 8), +('The Good the Bad and the Ugly', 'Western', 1966, 9), +('Unforgiven', 'Western', 1992, 7); \ No newline at end of file diff --git a/sql/products/create-products.sql b/sql/products/create-products.sql deleted file mode 100644 index e7a5100..0000000 --- a/sql/products/create-products.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE products( - id serial primary key, - name varchar(255) not null, - price int not null, - discount boolean, - unique(name) -) -