20
SQL DESIGNERS RUS INC May 30, 2016 Authored by: Michael Peterson The Thousand Song Database IT 201 - Final Project

Cover Page & Table of Contents

Embed Size (px)

Citation preview

Page 1: Cover Page & Table of Contents

SQL Designers rus inc

May 30, 2016Authored by: Michael Peterson

The Thousand Song Database

IT 201 - Final Project

Page 2: Cover Page & Table of Contents

1

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

THE THOUSAND SONG DATABASEIT 201 - Final Project

Table of ContentsI. Identify problems with the thousand song databaseII. 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 entityc. Indicate the relationship between the entities, including minimum

cardinality. Assumptions pertaining will be located hered. Implement the relationship between entities by adding linking columns

(foreign keys) and linking tables – as appropriateIII. SQL statements to create the new tables in the new database

a. Select the best data type for each columnb. Define primary keys and foreign keysc. Define at least one required column other than a primary keyd. Specify at least one default value

IV. SQL statements to add one example row to each tableV. 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.

Page 3: Cover Page & Table of Contents

2

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

I. Identified problems with music table: No primary key, repeating data (artist can more than one album, an album can have more than one song, Genre can have more than one artist), table is not normalized (you can create 3-tables to reduce data redundancy and eliminate repeating data).

II. Entities & Attributes & Primary Keys & Assumptions a. Entities: Song, Artist, Albumb. Attributes Song : name, time, plays, *songIDc. Attributes Artist: name, *artistIDd. Attributes Album: title, year, genre,*albumIDe. 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 Albumsong_name album_titlesong_length album_yearsong_plays genre*songID *albumID Artist

album_ID (FK) artist_nameartist_ID(FK) *artistID

Page 4: Cover Page & Table of Contents

3

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

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));

Page 5: Cover Page & Table of Contents

4

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

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, genreFROM music;

INSERT INTO song(song_name, song_length, song_plays, artistName, albumName)SELECT song, time, plays, artist, albumFROM music;

Page 6: Cover Page & Table of Contents

5

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

Update Song table with FK #1

UPDATE songSET song.artist_ID = (SELECT DISTINCT artist.artistID FROM artist, musicWHERE artist.artist_name = song.artistName AND song.artistName = music.artist);

Update Song table with FK #2

UPDATE songSET song.album_ID = (SELECT DISTINCT album.albumIDFROM album, musicWHERE album.album_title = song.albumNameAND song.albumName = music.album LIMIT 1);

Drop added columns used to generate FK’s

ALTER TABLE songDROP COLUMN artistName;

ALTER TABLE songDROP COLUMN albumName;

Page 7: Cover Page & Table of Contents

6

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

Update Song Table for ID Discrepancies

UPDATE song SET album_ID = 182WHEREsongID = 995

UPDATE song SET album_ID = 186WHEREsongID = 999

UPDATE song SET album_ID = 185WHEREsongID = 998

UPDATE song SET album_ID = 184WHEREsongID = 997

UPDATE song SET album_ID = 183WHEREsongID = 996

UPDATE song SET album_ID = 187WHEREsongID = 1000;

Page 8: Cover Page & Table of Contents

7

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

VI. Statements for a. through j.queries a.

SELECT song_name, song_playsFROM song WHERE song_plays > 5;

Page 9: Cover Page & Table of Contents

8

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

b.

SELECT song_name, genre, song_lengthFROM song s, album a WHERE (a.genre LIKE "R&B"OR a.genre LIKE "Jazz")AND s.song_length < 2;

Page 10: Cover Page & Table of Contents

9

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

c.

*****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, genreFROM albumWHERE genre LIKE "%Rock%"AND album_year BETWEEN 2010 AND 2013;

Page 11: Cover Page & Table of Contents

10

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

d.

*****Query data prior to delete to confirm correct delete statement*****SELECT s.song_name, a.artist_nameFROM 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";

Page 12: Cover Page & Table of Contents

11

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

e.

SELECT s.song_name, ar.artist_name, a.album_title, s.song_length, a.genreFROM song s, album a, artist arWHERE ar.artistID = s.artist_IDAND s.album_ID = a.albumID;

Page 13: Cover Page & Table of Contents

12

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

f.

SELECT AVG(song_length) AS `Avg Length`, a.genreFROM album a, song sWHERE a.albumID = s.album_IDGROUP BY a.genre;

Page 14: Cover Page & Table of Contents

13

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

g.

SELECT genre, COUNT(*) AS `Total Songs`FROM albumWHERE genre IS NOT NULLGROUP BY genreHAVING COUNT(*) > 20;

Page 15: Cover Page & Table of Contents

14

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

h.

SELECT s.song_name, ar.artist_name, a.album_title,a.genreFROM song s, artist ar, album a WHERE ar.artistID = s.artist_IDAND s.album_ID = a.albumIDGROUP BY a.album_title;

Page 16: Cover Page & Table of Contents

15

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

i.

UPDATE albumSET genre = "Alternative" WHERE album_title = "9";

SELECT * FROM `album` WHERE album_title = "9";

Page 17: Cover Page & Table of Contents

16

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

j.

SELECT song_name, song_lengthFROM songWHERE song_length > (SELECT AVG(song_length)FROM song)ORDER BY song_length ASC;

Page 18: Cover Page & Table of Contents

17

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

VII. Create a view called Top20

CREATE VIEW Top20 ASSELECT s.song_name, ar.artist_name, a.album_titleFROM song s, artist ar, album aWHERE s.album_ID = a.albumID AND s.artist_ID = ar.artistID

Page 19: Cover Page & Table of Contents

18

The

Thou

sand

Son

g Da

taba

se |

5/3

0/20

16

ORDER BY song_plays DESCLIMIT 20;

SELECT * FROM Top20; //For second part of part VII//