chinook database using MSQL 2016 Server Development I know.pdf
Cover Page & Table of Contents
1. SQL DESIGNERS RUS INC
May 30, 2016
Authored by: Michael Peterson
The Thousand Song Database
IT 201 - Final Project
2. 1
TheThousandSongDatabase|5/30/2016
The Thousand Song Database
IT 201 - Final Project
Table of Contents
I. Identify problems with the thousand song database
II. Draw an ER diagram that has the below criteria
a. Display each entity that is represented in the in the music table, and attributes
that belong to each entity
b. Assign a primary key to each entity
c. Indicate the relationship between the entities, including minimum cardinality.
Assumptions pertaining will be located here
d. Implement the relationship between entities by adding linking columns (foreign
keys) and linking tables – as appropriate
III. SQL statements to create the new tables in the new database
a. Select the best data type for each column
b. Define primary keys and foreign keys
c. Define at least one required column other than a primary key
d. Specify at least one default value
IV. SQL statements to add one example row to each table
V. SQL statements to populate the new tables based on the original music table.
VI. SQL statements performed on the new tables (a-j)
VII. A View called Top20 that displays the song title, artist, and album for the 20 most
popular songs.
3. 2
TheThousandSongDatabase|5/30/2016
I. Identified problems with music table: No primary key, repeating data (artist
can more than one album, an album can have more thanone song, Genre can have
more than one artist), table is not normalized (you cancreate 3-tables to reduce data
redundancy and eliminate repeating data).
II. Entities & Attributes & Primary Keys & Assumptions
a. Entities: Song, Artist, Album
b. Attributes Song : name, time, plays, *songID
c. Attributes Artist: name, *artistID
d. Attributes Album: title, year, genre,*albumID
e. Assumptions: I am assuming that an Artist has at least one song, and that an
Album has at least one song. I am assuming there are many songs to an Artist,
and there are many songs to an Album. I am assuming there is a possibility of a
song having no album.
Song Album
song_name album_title
song_length album_year
song_plays genre
*songID *albumID Artist
album_ID (FK) artist_name
artist_ID(FK) *artistID
4. 3
TheThousandSongDatabase|5/30/2016
III. Create new tables
CREATE TABLE artist (
artistID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
artist_name VARCHAR(35) NOT NULL
);
CREATE TABLE album (
albumID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
album_title VARCHAR(60) NOT NULL,
album_year VARCHAR(4),
genre VARCHAR(18)
);
CREATE TABLE song (
songID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
song_name VARCHAR(98) NOT NULL,
song_length VARCHAR(5),
song_plays VARCHAR(5) DEFAULT "0",
artistName varchar(100),
albumName varchar(98),
album_ID INT,
artist_ID INT,
FOREIGN KEY (artist_ID) REFERENCES artist (artistID),
FOREIGN KEY (album_ID) REFERENCES album (albumID)
);
5. 4
TheThousandSongDatabase|5/30/2016
IV. Add sample row
INSERT INTO artist VALUES
(NULL, 'My Band');
INSERT INTO album VALUES
(NULL, 'Rasta 101(Disc 2)', '1994', 'Reggae');
INSERT INTO song VALUES
(NULL, 'Roast the Pig', '4:13', '6', 1, 1);
V. Populate New Tables
INSERT INTO artist
(artist_name)
SELECT DISTINCT artist
FROM music;
INSERT INTO album
(album_title, album_year, genre)
SELECT DISTINCT album, year, genre
FROM music;
INSERT INTO song
(song_name, song_length, song_plays, artistName, albumName)
SELECT song, time, plays, artist, album
FROM music;
6. 5
TheThousandSongDatabase|5/30/2016
Update Song table with FK #1
UPDATE song
SET song.artist_ID = (SELECT DISTINCT artist.artistID
FROM artist, music
WHERE artist.artist_name = song.artistName
AND song.artistName = music.artist);
Update Song table with FK #2
UPDATE song
SET song.album_ID = (SELECT DISTINCT album.albumID
FROM album, music
WHERE album.album_title = song.albumName
AND song.albumName = music.album LIMIT 1);
Drop added columns used to generate FK’s
ALTER TABLE song
DROP COLUMN artistName;
ALTER TABLE song
DROP COLUMN albumName;
7. 6
TheThousandSongDatabase|5/30/2016
Update Song Table for ID Discrepancies
UPDATE song
SET album_ID = 182
WHERE
songID = 995
UPDATE song
SET album_ID = 186
WHERE
songID = 999
UPDATE song
SET album_ID = 185
WHERE
songID = 998
UPDATE song
SET album_ID = 184
WHERE
songID = 997
UPDATE song
SET album_ID = 183
WHERE
songID = 996
UPDATE song
SET album_ID = 187
WHERE
songID = 1000;
10. 9
TheThousandSongDatabase|5/30/2016
*****Changed column album_year from VARCHAR to INT*****
ALTER TABLE `album`
CHANGE `album_year`
`album_year` INT(4) NULL DEFAULT NULL;
SELECT album_title, album_year, genre
FROM album
WHERE genre LIKE "%Rock%"
AND album_year
BETWEEN 2010 AND 2013;
d.
11. 10
TheThousandSongDatabase|5/30/2016
*****Query data prior to delete to confirm correct delete statement*****
SELECT s.song_name, a.artist_name
FROM song s, artist a
WHERE s.song_name = "Sleep don't weep"
AND a.artist_name = "Damien Rice";
DELETE
FROM song
WHERE song_name = "Sleep don't weep";
e.
18. 17
TheThousandSongDatabase|5/30/2016
CREATE VIEW Top20 AS
SELECT s.song_name, ar.artist_name, a.album_title
FROM song s, artist ar, album a
WHERE s.album_ID = a.albumID
AND s.artist_ID = ar.artistID
ORDER BY song_plays DESC
LIMIT 20;
SELECT *
FROM Top20;
//For second part of part VII//