SlideShare a Scribd company logo
1 of 18
SQL DESIGNERS RUS INC
May 30, 2016
Authored by: Michael Peterson
The Thousand Song Database
IT 201 - Final Project
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.
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
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)
);
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;
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;
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;
7
TheThousandSongDatabase|5/30/2016
VI. Statements for a. through j.queries
a.
SELECT song_name, song_plays
FROM song
WHERE song_plays > 5;
b.
8
TheThousandSongDatabase|5/30/2016
SELECT song_name, genre, song_length
FROM song s, album a
WHERE (a.genre LIKE"R&B"
OR a.genre LIKE"Jazz")
AND s.song_length < 2;
c.
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.
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.
11
TheThousandSongDatabase|5/30/2016
SELECT s.song_name, ar.artist_name, a.album_title,
s.song_length, a.genre
FROM song s, album a, artist ar
WHERE ar.artistID = s.artist_ID
AND s.album_ID = a.albumID;
f.
12
TheThousandSongDatabase|5/30/2016
SELECT AVG(song_length) AS `Avg Length`, a.genre
FROM album a, song s
WHERE a.albumID = s.album_ID
GROUP BY a.genre;
g.
13
TheThousandSongDatabase|5/30/2016
SELECT genre, COUNT(*) AS `Total Songs`
FROM album
WHERE genre IS NOT NULL
GROUP BY genre
HAVING COUNT(*) > 20;
h.
14
TheThousandSongDatabase|5/30/2016
SELECT s.song_name, ar.artist_name, a.album_title,
a.genre
FROM song s, artist ar, album a
WHERE ar.artistID = s.artist_ID
AND s.album_ID = a.albumID
GROUP BY a.album_title;
i.
15
TheThousandSongDatabase|5/30/2016
UPDATE album
SET genre = "Alternative"
WHERE album_title = "9";
SELECT *
FROM `album`
WHERE album_title = "9";
j.
16
TheThousandSongDatabase|5/30/2016
SELECT song_name, song_length
FROM song
WHERE song_length > (SELECT AVG(song_length)FROMsong)
ORDER BY song_length ASC;
VII. Create a view called Top20
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//

More Related Content

Similar to Cover Page & Table of Contents

Problem 2 struct to hold information about a song struct So.pdf
 Problem 2 struct to hold information about a song struct So.pdf Problem 2 struct to hold information about a song struct So.pdf
Problem 2 struct to hold information about a song struct So.pdfahujaelectronics175
 
a database to represent information about CDs made by popular music gr.pdf
a database to represent information about CDs made by popular music gr.pdfa database to represent information about CDs made by popular music gr.pdf
a database to represent information about CDs made by popular music gr.pdfpawanmca65
 
Program 02 Based on the previous problem you should impleme.pdf
Program 02 Based on the previous problem you should impleme.pdfProgram 02 Based on the previous problem you should impleme.pdf
Program 02 Based on the previous problem you should impleme.pdfaddtechglobalmarketi
 
Program 01 For this program you will complete the program in.pdf
Program 01 For this program you will complete the program in.pdfProgram 01 For this program you will complete the program in.pdf
Program 01 For this program you will complete the program in.pdfaddtechglobalmarketi
 
8.15 Program Playlist (C++) You will be building a linked list. Mak.pdf
8.15 Program Playlist (C++) You will be building a linked list. Mak.pdf8.15 Program Playlist (C++) You will be building a linked list. Mak.pdf
8.15 Program Playlist (C++) You will be building a linked list. Mak.pdfarenamobiles123
 
chinook database using MSQL 2016 Server Development I know.pdf
chinook database using MSQL 2016 Server Development I know.pdfchinook database using MSQL 2016 Server Development I know.pdf
chinook database using MSQL 2016 Server Development I know.pdfabdulrahman7810
 

Similar to Cover Page & Table of Contents (6)

Problem 2 struct to hold information about a song struct So.pdf
 Problem 2 struct to hold information about a song struct So.pdf Problem 2 struct to hold information about a song struct So.pdf
Problem 2 struct to hold information about a song struct So.pdf
 
a database to represent information about CDs made by popular music gr.pdf
a database to represent information about CDs made by popular music gr.pdfa database to represent information about CDs made by popular music gr.pdf
a database to represent information about CDs made by popular music gr.pdf
 
Program 02 Based on the previous problem you should impleme.pdf
Program 02 Based on the previous problem you should impleme.pdfProgram 02 Based on the previous problem you should impleme.pdf
Program 02 Based on the previous problem you should impleme.pdf
 
Program 01 For this program you will complete the program in.pdf
Program 01 For this program you will complete the program in.pdfProgram 01 For this program you will complete the program in.pdf
Program 01 For this program you will complete the program in.pdf
 
8.15 Program Playlist (C++) You will be building a linked list. Mak.pdf
8.15 Program Playlist (C++) You will be building a linked list. Mak.pdf8.15 Program Playlist (C++) You will be building a linked list. Mak.pdf
8.15 Program Playlist (C++) You will be building a linked list. Mak.pdf
 
chinook database using MSQL 2016 Server Development I know.pdf
chinook database using MSQL 2016 Server Development I know.pdfchinook database using MSQL 2016 Server Development I know.pdf
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;
  • 8. 7 TheThousandSongDatabase|5/30/2016 VI. Statements for a. through j.queries a. SELECT song_name, song_plays FROM song WHERE song_plays > 5; b.
  • 9. 8 TheThousandSongDatabase|5/30/2016 SELECT song_name, genre, song_length FROM song s, album a WHERE (a.genre LIKE"R&B" OR a.genre LIKE"Jazz") AND s.song_length < 2; c.
  • 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.
  • 12. 11 TheThousandSongDatabase|5/30/2016 SELECT s.song_name, ar.artist_name, a.album_title, s.song_length, a.genre FROM song s, album a, artist ar WHERE ar.artistID = s.artist_ID AND s.album_ID = a.albumID; f.
  • 13. 12 TheThousandSongDatabase|5/30/2016 SELECT AVG(song_length) AS `Avg Length`, a.genre FROM album a, song s WHERE a.albumID = s.album_ID GROUP BY a.genre; g.
  • 14. 13 TheThousandSongDatabase|5/30/2016 SELECT genre, COUNT(*) AS `Total Songs` FROM album WHERE genre IS NOT NULL GROUP BY genre HAVING COUNT(*) > 20; h.
  • 15. 14 TheThousandSongDatabase|5/30/2016 SELECT s.song_name, ar.artist_name, a.album_title, a.genre FROM song s, artist ar, album a WHERE ar.artistID = s.artist_ID AND s.album_ID = a.albumID GROUP BY a.album_title; i.
  • 16. 15 TheThousandSongDatabase|5/30/2016 UPDATE album SET genre = "Alternative" WHERE album_title = "9"; SELECT * FROM `album` WHERE album_title = "9"; j.
  • 17. 16 TheThousandSongDatabase|5/30/2016 SELECT song_name, song_length FROM song WHERE song_length > (SELECT AVG(song_length)FROMsong) ORDER BY song_length ASC; VII. Create a view called Top20
  • 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//