diff --git a/lab-2/music-store-app/sql/dummy_data.sql b/lab-2/music-store-app/sql/dummy_data.sql index 5db6038f8f89a35887340919cbac1bbb45743352..2bc338f8ffc9860a3690d71710e06c38e713c849 100644 --- a/lab-2/music-store-app/sql/dummy_data.sql +++ b/lab-2/music-store-app/sql/dummy_data.sql @@ -24,20 +24,27 @@ VALUES ('John', 'Lennon', 3), ('Madonna', NULL, NULL); -INSERT INTO Album (name, year, artist_id, price) +INSERT INTO Album (upc, title, year, artist_id, price, genre_id) VALUES -('Soul Rebel', 1970, 1, 25.99 ), -('Catch A Fire', 1973, 1, 25.99 ), -('Natty Dread', 1974, 1, 20.99 ), -('Babylon By Bus', 1978, 1, 24.99 ), -('Night Nurse', 1982, 5, 17.99 ), -('Mr Issacs', 1982, 5, 9.99 ), -('Black and Dekker', 1980, 6, 19.99 ), -('Sunday Coming', 1970, 4, 15.99 ); +('123425364732', 'Soul Rebel', 1970, 1, 25.99, 1 ), +('017263547261', 'Catch A Fire', 1973, 1, 25.99, 1 ), +('018263526272', 'Natty Dread', 1974, 1, 20.99, 1 ), +('018273527273', 'Babylon By Bus', 1978, 1, 24.99, 1 ), +('491827364626', 'Night Nurse', 1982, 5, 17.99, 1 ), +('018276272828', 'Mr Issacs', 1982, 5, 9.99, 1 ), +('018273662728', 'Black and Dekker', 1980, 6, 19.99, 1 ), +('726517237627', 'Sunday Coming', 1970, 4, 15.99, 1 ), +('018274372722', 'Imagine', 1971, 7, 11.99, 6 ), +('018273727287', 'Like a Virgin', 1984, 8, 9.99, 6 ); -INSERT INTO Track (title, album_id, track_number) +INSERT INTO Track (name, album_upc, track_number) VALUES -('Sunday Coming', 8, 1), -('These Eyes', 8, 2), -('Hurting Me', 8, 3); +('Sunday Coming', '726517237627', 1), +('Imagine', '018274372722', 1), +('Material Girl', '018273727287', 1), +('Angel', '018273727287', 2), +('Like a Virgin', '018273727287', 3), +('Crippled Inside', '018274372722', 2), +('These Eyes', '726517237627', 2), +('Hurting Me', '726517237627', 3); diff --git a/lab-2/music-store-app/sql/practice_queries.sql b/lab-2/music-store-app/sql/practice_queries.sql index f2872a19d3abe2771b92fec648f3ac2ba3ea0680..1c05ab5a5b82d3321ed29798dc520079ee02b74e 100644 --- a/lab-2/music-store-app/sql/practice_queries.sql +++ b/lab-2/music-store-app/sql/practice_queries.sql @@ -13,4 +13,35 @@ SELECT name, year FROM Album WHERE price > 15; UPDATE Album SET price=21.99 WHERE name='Soul Rebel'; /* Delete artists with first_name Madonna (Yay!) */ -DELETE FROM Artist WHERE first_name='Madonna'; \ No newline at end of file +DELETE FROM Artist WHERE first_name='Madonna'; + +/* Example simple table join. +Gets title, price and year of albums +where Artist last name is 'Marley' +and orders by year ascending */ +SELECT title, price, year FROM Album +INNER JOIN Artist + ON Artist.id=Album.artist_id +WHERE Artist.last_name='Marley' +ORDER BY year ASC; + +/* Example multi table join +Gets track names, track numbers +and titles of all pop albums */ +SELECT t.name, t.track_number, a.title +FROM Track t +INNER JOIN Album a + ON a.upc=t.album_upc +INNER JOIN Genre g + ON g.id=a.genre_id +WHERE g.name='Pop'; + +/* Example multi table join +Gets album title, genre and name +of first track on every album */ +SELECT a.title, g.name, t.name FROM Album a +INNER JOIN Genre g + ON a.genre_id=g.id +INNER JOIN Track t + ON a.upc=t.album_upc +WHERE t.track_number=1; diff --git a/lab-2/music-store-app/sql/schema.sql b/lab-2/music-store-app/sql/schema.sql index 3b2bb7725c5f648441cf92fb2dd6b8dcd7507838..8e1df6cfc50d37c27b438d50379bd5ee083ed2ec 100644 --- a/lab-2/music-store-app/sql/schema.sql +++ b/lab-2/music-store-app/sql/schema.sql @@ -9,7 +9,9 @@ CREATE TABLE Genre ( PRIMARY KEY (id) ); -/* Define table for storing act (i.e. a group or solo artist) */ +/* Define table for storing act (i.e. a group or solo artist) +ON DELETE CASCADE not needed in this +case as a Band does not HAVE to have an associated genre */ CREATE TABLE Band ( id INT AUTO_INCREMENT, name VARCHAR(50) NOT NULL, @@ -32,27 +34,38 @@ CREATE TABLE Artist ( /* Define table for storing albums */ CREATE TABLE Album ( - id INT AUTO_INCREMENT, - name VARCHAR(50) NOT NULL, + upc CHAR(12), + title VARCHAR(50) NOT NULL, year YEAR(4), artist_id INT, compilation BOOLEAN NOT NULL DEFAULT 0, price DECIMAL(5, 2) unsigned NOT NULL, - PRIMARY KEY (id), + genre_id INT, + PRIMARY KEY (upc), FOREIGN KEY (artist_id) - REFERENCES Artist (id) + REFERENCES Artist (id), + FOREIGN KEY (genre_id) + REFERENCES Genre (id) ); -/* Define table for storing single tracks */ +/* Define table for storing single tracks +ON DELETE CASCADE will ensure no track +is left that isn't linked to an album. +ON UPDATE CASCADE will inherit any changes +to the Album UPC (Universal Product Code). +A composite PRIMARY KEY is used here to +ensure the track_number is unique +on any given album */ CREATE TABLE Track ( - id INT AUTO_INCREMENT, - title VARCHAR(50) NOT NULL, - album_id INT NOT NULL, - track_number INT UNIQUE, - PRIMARY KEY (id), - FOREIGN KEY (album_id) - REFERENCES Album (id) + name VARCHAR(50) NOT NULL, + album_upc CHAR(12) NOT NULL, + track_number INT, + PRIMARY KEY (album_upc, track_number), + FOREIGN KEY (album_upc) + REFERENCES Album (upc) + ON UPDATE CASCADE + ON DELETE CASCADE ); /* Define table for storing customers */ @@ -77,17 +90,20 @@ CREATE TABLE Transaction ( PRIMARY KEY (id), FOREIGN KEY (customer_id) REFERENCES Customer(id) + ON DELETE CASCADE ); /* Define table for storing line items */ CREATE TABLE LineItem ( id INT AUTO_INCREMENT, trans_id INT NOT NULL, - album_id INT NOT NULL, + album_upc CHAR(12) NOT NULL, quantity INT NOT NULL DEFAULT 1, PRIMARY KEY (id), FOREIGN KEY (trans_id) - REFERENCES Transaction (id), - FOREIGN KEY (album_id) - REFERENCES Album (id) + REFERENCES Transaction (id) + ON DELETE CASCADE, + FOREIGN KEY (album_upc) + REFERENCES Album (upc) + ON DELETE CASCADE );