-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathartist.sql
60 lines (50 loc) · 1.64 KB
/
artist.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
# artist.sql
# artist/painting tables for master-detail join examples
DROP TABLE IF EXISTS artist;
#@ _CREATE_TABLE_ARTIST_
CREATE TABLE artist
(
a_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # artist ID
name VARCHAR(30) NOT NULL, # artist name
PRIMARY KEY (a_id),
UNIQUE (name)
);
#@ _CREATE_TABLE_ARTIST_
INSERT INTO artist (name) VALUES
('Da Vinci'),
('Monet'),
('Van Gogh'),
('Renoir')
;
SELECT * FROM artist;
DROP TABLE IF EXISTS painting;
#@ _CREATE_TABLE_PAINTING_
CREATE TABLE painting
(
a_id INT UNSIGNED NOT NULL, # artist ID
p_id INT UNSIGNED NOT NULL AUTO_INCREMENT, # painting ID
title VARCHAR(100) NOT NULL, # title of painting
state VARCHAR(2) NOT NULL, # state where purchased
price INT UNSIGNED, # purchase price (dollars)
INDEX (a_id),
PRIMARY KEY (p_id)
);
#@ _CREATE_TABLE_PAINTING_
# Use INSERT INTO ... SELECT form to get proper artist ID corresponding
# to artist name
INSERT INTO painting (a_id,title,state,price)
SELECT a_id, 'The Last Supper', 'IN', 34
FROM artist WHERE name = 'Da Vinci';
INSERT INTO painting (a_id,title,state,price)
SELECT a_id, 'Mona Lisa', 'MI', 87
FROM artist WHERE name = 'Da Vinci';
INSERT INTO painting (a_id,title,state,price)
SELECT a_id, 'Starry Night', 'KY', 48
FROM artist WHERE name = 'Van Gogh';
INSERT INTO painting (a_id,title,state,price)
SELECT a_id, 'The Potato Eaters', 'KY', 67
FROM artist WHERE name = 'Van Gogh';
INSERT INTO painting (a_id,title,state,price)
SELECT a_id, 'Les Deux Soeurs', 'NE', 64
FROM artist WHERE name = 'Renoir';
SELECT * FROM painting;