forked from cristiscu/erd-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chinook-create-database.sql
153 lines (131 loc) · 4.17 KB
/
chinook-create-database.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
CREATE OR REPLACE DATABASE "Chinook";
CREATE TABLE "Album"
(
"AlbumId" INT NOT NULL,
"Title" VARCHAR(160) NOT NULL,
"ArtistId" INT NOT NULL,
CONSTRAINT "PK_Album" PRIMARY KEY ("AlbumId")
);
CREATE TABLE "Artist"
(
"ArtistId" INT NOT NULL,
"Name" VARCHAR(120),
CONSTRAINT "PK_Artist" PRIMARY KEY ("ArtistId")
);
CREATE TABLE "Customer"
(
"CustomerId" INT NOT NULL,
"FirstName" VARCHAR(40) NOT NULL,
"LastName" VARCHAR(20) NOT NULL,
"Company" VARCHAR(80),
"Address" VARCHAR(70),
"City" VARCHAR(40),
"State" VARCHAR(40),
"Country" VARCHAR(40),
"PostalCode" VARCHAR(10),
"Phone" VARCHAR(24),
"Fax" VARCHAR(24),
"Email" VARCHAR(60) NOT NULL,
"SupportRepId" INT,
CONSTRAINT "PK_Customer" PRIMARY KEY ("CustomerId")
);
CREATE TABLE "Employee"
(
"EmployeeId" INT NOT NULL,
"LastName" VARCHAR(20) NOT NULL,
"FirstName" VARCHAR(20) NOT NULL,
"Title" VARCHAR(30),
"ReportsTo" INT,
"BirthDate" TIMESTAMP,
"HireDate" TIMESTAMP,
"Address" VARCHAR(70),
"City" VARCHAR(40),
"State" VARCHAR(40),
"Country" VARCHAR(40),
"PostalCode" VARCHAR(10),
"Phone" VARCHAR(24),
"Fax" VARCHAR(24),
"Email" VARCHAR(60),
CONSTRAINT "PK_Employee" PRIMARY KEY ("EmployeeId")
);
CREATE TABLE "Genre"
(
"GenreId" INT NOT NULL,
"Name" VARCHAR(120),
CONSTRAINT "PK_Genre" PRIMARY KEY ("GenreId")
);
CREATE TABLE "Invoice"
(
"InvoiceId" INT NOT NULL,
"CustomerId" INT NOT NULL,
"InvoiceDate" TIMESTAMP NOT NULL,
"BillingAddress" VARCHAR(70),
"BillingCity" VARCHAR(40),
"BillingState" VARCHAR(40),
"BillingCountry" VARCHAR(40),
"BillingPostalCode" VARCHAR(10),
"Total" NUMERIC(10,2) NOT NULL,
CONSTRAINT "PK_Invoice" PRIMARY KEY ("InvoiceId")
);
CREATE TABLE "InvoiceLine"
(
"InvoiceLineId" INT NOT NULL,
"InvoiceId" INT NOT NULL,
"TrackId" INT NOT NULL,
"UnitPrice" NUMERIC(10,2) NOT NULL,
"Quantity" INT NOT NULL,
CONSTRAINT "PK_InvoiceLine" PRIMARY KEY ("InvoiceLineId")
);
CREATE TABLE "MediaType"
(
"MediaTypeId" INT NOT NULL,
"Name" VARCHAR(120),
CONSTRAINT "PK_MediaType" PRIMARY KEY ("MediaTypeId")
);
CREATE TABLE "Playlist"
(
"PlaylistId" INT NOT NULL,
"Name" VARCHAR(120),
CONSTRAINT "PK_Playlist" PRIMARY KEY ("PlaylistId")
);
CREATE TABLE "PlaylistTrack"
(
"PlaylistId" INT NOT NULL,
"TrackId" INT NOT NULL,
CONSTRAINT "PK_PlaylistTrack" PRIMARY KEY ("PlaylistId", "TrackId")
);
CREATE TABLE "Track"
(
"TrackId" INT NOT NULL,
"Name" VARCHAR(200) NOT NULL,
"AlbumId" INT,
"MediaTypeId" INT NOT NULL,
"GenreId" INT,
"Composer" VARCHAR(220),
"Milliseconds" INT NOT NULL,
"Bytes" INT,
"UnitPrice" NUMERIC(10,2) NOT NULL,
CONSTRAINT "PK_Track" PRIMARY KEY ("TrackId")
);
ALTER TABLE "Album" ADD CONSTRAINT "FK_AlbumArtistId"
FOREIGN KEY ("ArtistId") REFERENCES "Artist" ("ArtistId");
ALTER TABLE "Customer" ADD CONSTRAINT "FK_CustomerSupportRepId"
FOREIGN KEY ("SupportRepId") REFERENCES "Employee" ("EmployeeId");
ALTER TABLE "Employee" ADD CONSTRAINT "FK_EmployeeReportsTo"
FOREIGN KEY ("ReportsTo") REFERENCES "Employee" ("EmployeeId");
ALTER TABLE "Invoice" ADD CONSTRAINT "FK_InvoiceCustomerId"
FOREIGN KEY ("CustomerId") REFERENCES "Customer" ("CustomerId");
ALTER TABLE "InvoiceLine" ADD CONSTRAINT "FK_InvoiceLineInvoiceId"
FOREIGN KEY ("InvoiceId") REFERENCES "Invoice" ("InvoiceId");
ALTER TABLE "InvoiceLine" ADD CONSTRAINT "FK_InvoiceLineTrackId"
FOREIGN KEY ("TrackId") REFERENCES "Track" ("TrackId");
ALTER TABLE "PlaylistTrack" ADD CONSTRAINT "FK_PlaylistTrackPlaylistId"
FOREIGN KEY ("PlaylistId") REFERENCES "Playlist" ("PlaylistId");
ALTER TABLE "PlaylistTrack" ADD CONSTRAINT "FK_PlaylistTrackTrackId"
FOREIGN KEY ("TrackId") REFERENCES "Track" ("TrackId");
ALTER TABLE "Track" ADD CONSTRAINT "FK_TrackAlbumId"
FOREIGN KEY ("AlbumId") REFERENCES "Album" ("AlbumId");
ALTER TABLE "Track" ADD CONSTRAINT "FK_TrackGenreId"
FOREIGN KEY ("GenreId") REFERENCES "Genre" ("GenreId");
ALTER TABLE "Track" ADD CONSTRAINT "FK_TrackMediaTypeId"
FOREIGN KEY ("MediaTypeId") REFERENCES "MediaType" ("MediaTypeId");