-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScarranMusicCreate.sql
160 lines (145 loc) · 4.31 KB
/
ScarranMusicCreate.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Create the primary entities
CREATE TABLE IF NOT EXISTS Band
(
bandID INT AUTO_INCREMENT,
bandName VARCHAR(100) NOT NULL,
CONSTRAINT Band_pk
PRIMARY KEY (bandID)
);
CREATE TABLE IF NOT EXISTS Artist
(
artistID INT AUTO_INCREMENT,
fName VARCHAR(100) NOT NULL,
lName VARCHAR(100) NULL,
CONSTRAINT Artist_pk
PRIMARY KEY (artistID)
);
CREATE TABLE IF NOT EXISTS Album
(
albumID INT AUTO_INCREMENT,
albumTitle VARCHAR(100) NOT NULL,
liveRecording BOOL DEFAULT FALSE NULL,
CONSTRAINT Album_pk
PRIMARY KEY (albumID)
);
CREATE TABLE IF NOT EXISTS Label
(
labelID INT AUTO_INCREMENT,
labelName VARCHAR(100) NOT NULL,
CONSTRAINT Label_pk
PRIMARY KEY (labelID)
);
CREATE TABLE IF NOT EXISTS Song
(
songID INT AUTO_INCREMENT,
songTitle VARCHAR(100) NOT NULL,
duration TIME NULL,
lyrics VARCHAR(5000) NULL,
CONSTRAINT Song_pk
PRIMARY KEY (songID)
);
# Create “Playlist” before “Concert” due to One-to-Many relationship
CREATE TABLE IF NOT EXISTS Playlist
(
playlistID INT AUTO_INCREMENT,
playlistTitle VARCHAR(100) NOT NULL,
CONSTRAINT Playlist_pk
PRIMARY KEY (playlistID)
);
CREATE TABLE IF NOT EXISTS Concert
(
concertID INT AUTO_INCREMENT,
playlistID INT NULL,
location VARCHAR(100) NULL,
date DATE NULL,
CONSTRAINT Concert_pk
PRIMARY KEY (concertID),
CONSTRAINT Concert_Playlist_playlistID_fk
FOREIGN KEY (playlistID) REFERENCES Playlist (playlistID)
ON UPDATE CASCADE ON DELETE SET NULL
);
# Create the relational tables
CREATE TABLE IF NOT EXISTS BandArtist
(
bandArtistID INT AUTO_INCREMENT,
bandID INT NOT NULL,
artistID INT NOT NULL,
CONSTRAINT BandArtist_pk
PRIMARY KEY (bandArtistID),
CONSTRAINT BandArtist_Artist_artistID_fk
FOREIGN KEY (artistID) REFERENCES Artist (artistID)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT BandArtist_Band_bandID_fk
FOREIGN KEY (bandID) REFERENCES Band (bandID)
ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS AlbumSong
(
albumSongID INT AUTO_INCREMENT,
albumID INT NOT NULL,
songID INT NOT NULL,
CONSTRAINT AlbumSong_pk
PRIMARY KEY (albumSongID),
CONSTRAINT AlbumSong_Album_albumID_fk
FOREIGN KEY (albumID) REFERENCES Album (albumID)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT AlbumSong_Song_songID_fk
FOREIGN KEY (songID) REFERENCES Song (songID)
ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS PlaylistSong
(
playlistSongID INT AUTO_INCREMENT,
playlistID INT NOT NULL,
songID INT NOT NULL,
CONSTRAINT PlaylistSong_pk
PRIMARY KEY (playlistSongID),
CONSTRAINT PlaylistSong_Playlist_playlistID_fk
FOREIGN KEY (playlistID) REFERENCES Playlist (playlistID)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT PlaylistSong_Song_songID_fk
FOREIGN KEY (songID) REFERENCES Song (songID)
ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS BandAlbum
(
bandAlbumID INT AUTO_INCREMENT,
bandID INT NOT NULL,
albumID INT NOT NULL,
CONSTRAINT BandAlbum_pk
PRIMARY KEY (bandAlbumID),
CONSTRAINT BandAlbum_Album_albumID_fk
FOREIGN KEY (albumID) REFERENCES Album (albumID)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT BandAlbum_Band_bandID_fk
FOREIGN KEY (bandID) REFERENCES Band (bandID)
ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS BandSong
(
bandSongID INT AUTO_INCREMENT,
bandID INT NOT NULL,
songID INT NOT NULL,
CONSTRAINT BandSong_pk
PRIMARY KEY (bandSongID),
CONSTRAINT BandSong_Band_bandID_fk
FOREIGN KEY (bandID) REFERENCES Band (bandID)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT BandSong_Song_songID_fk
FOREIGN KEY (songID) REFERENCES Song (songID)
ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS AlbumLabel
(
albumLabelID INT AUTO_INCREMENT,
albumID INT NOT NULL,
labelID INT NOT NULL,
CONSTRAINT AlbumLabel_pk
PRIMARY KEY (albumLabelID),
CONSTRAINT AlbumLabel_Album_albumID_fk
FOREIGN KEY (albumID) REFERENCES Album (albumID)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT AlbumLabel_Label_labelID_fk
FOREIGN KEY (labelID) REFERENCES Label (labelID)
ON UPDATE CASCADE ON DELETE CASCADE
);