-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab2.sql
More file actions
199 lines (133 loc) · 6.41 KB
/
lab2.sql
File metadata and controls
199 lines (133 loc) · 6.41 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
------------------------------------
-- drop in reverse order to handle dependencies
------------------------------------
drop table if exists contact_info;
drop table if exists assembly;
drop table if exists line_worker;
drop table if exists computer;
drop table if exists warranty_policy;
------------------------------------
-- warranty_policy
------------------------------------
create table warranty_policy (
policy_id VARCHAR (15) not null,
policy_name VARCHAR (50),
coverage_days int,
primary key (policy_id))
;
insert into warranty_policy values ('1', 'first policy', 120);
select * from warranty_policy;
------------------------------------
-- computer
------------------------------------
create table computer (
serial_number VARCHAR (15) not null,
cpu_speed INT,
memory_capacity INT,
price MONEY,
dvd_speed INT,
policy_id VARCHAR (15) not null,
primary key (serial_number),
foreign key (policy_id) references warranty_policy(policy_id)
);
insert into computer values ('comp123', 50, 3, 53.99, 130, '1');
select * from computer;
------------------------------------
-- line_worker
------------------------------------
create table line_worker (
employee_id VARCHAR (150) not null,
first_name VARCHAR (150),
last_name VARCHAR (150),
birthdate DATE,
primary key (employee_id))
;
insert into line_worker values ('bob123', 'billy', 'bob', '04-15-1978');
select * from line_worker;
------------------------------------
-- assembly
------------------------------------
create table assembly (
employee_id VARCHAR (15) not null,
serial_number VARCHAR (15) not null,
build_hours INT,
foreign key (employee_id) references line_worker(employee_id),
foreign key (serial_number) references computer(serial_number)
);
insert into assembly values ('bob123', 'comp123', 23);
select * from assembly;
------------------------------------
-- contact_info
------------------------------------
create table contact_info (
employee_id VARCHAR (15) not null,
phone_number VARCHAR (15) not null,
primary key (phone_number),
foreign key (employee_id) references line_worker(employee_id)
);
insert into contact_info values ('bob123', '123-4567');
select * from contact_info;
------------------------------------
-- Challenge 1 - inserting data
------------------------------------
-- warranty
insert into warranty_policy (policy_id , policy_name , coverage_days ) values ('2', 'second policy', 120);
insert into warranty_policy (policy_id , policy_name , coverage_days ) values ('3', 'third policy', 30);
insert into warranty_policy (policy_id , policy_name , coverage_days ) values ('4', 'fourth policy', 240);
insert into warranty_policy (policy_id , policy_name , coverage_days ) values ('5', 'fifth policy', 90);
select * from warranty_policy wp ;
-- computer
insert into computer (serial_number , cpu_speed , memory_capacity , price , dvd_speed , policy_id ) values ('comp124', 50, 3, 253.99, 130, '1');
insert into computer (serial_number , cpu_speed , memory_capacity , price , dvd_speed , policy_id ) values ('comp125', 150, 6, 353.99, 260, '3');
insert into computer (serial_number , cpu_speed , memory_capacity , price , dvd_speed , policy_id ) values ('comp126', 150, 6, 4353.99, 130, '4');
insert into computer (serial_number , cpu_speed , memory_capacity , price , dvd_speed , policy_id ) values ('comp127', 250, 3, 153.99, 260, '2');
select * from computer c ;
-- worker
insert into line_worker (employee_id , first_name , last_name , birthdate ) values ('jadoe', 'jane', 'doe', '04-15-1978');
insert into line_worker (employee_id , first_name , last_name , birthdate ) values ('jdab', 'june', 'dab', '04-15-1978');
insert into line_worker (employee_id , first_name , last_name , birthdate ) values ('jdoe', 'john', 'doe', '04-15-1978');
insert into line_worker (employee_id , first_name , last_name , birthdate ) values ('bboop', 'betty', 'boop', '04-15-1978');
select * from line_worker lw ;
-- assembly
insert into assembly (employee_id, serial_number, build_hours) values ('jadoe', 'comp124', 23);
insert into assembly (employee_id, serial_number, build_hours) values ('jdab', 'comp125', 23);
insert into assembly (employee_id, serial_number, build_hours) values ('jdoe', 'comp126', 23);
insert into assembly (employee_id, serial_number, build_hours) values ('bboop', 'comp127', 23);
select * from assembly;
-- contact
insert into contact_info (employee_id , phone_number ) values ('jadoe', '223-4567');
insert into contact_info (employee_id , phone_number ) values ('jdab', '323-4567');
insert into contact_info (employee_id , phone_number ) values ('jdoe', '423-4567');
insert into contact_info (employee_id , phone_number ) values ('jdoe', '523-7890');
select * from contact_info ci ;
--- Part 2-a - I changed the table name and redid the apostrophese to be quotes
insert into warranty_policy values ('p02', null, 2);
select * from warranty_policy wp;
-- Part 2-b -this will not work because policy id is required
-- insert into warranty_policy values (null, '5 year warranty',5);
-- Part 3 - The directions don't say to use the code included, so I wrote my own for practice - which means these don't line up. i changed to the id we just entered, and added another row with one i had previously which is used by another table, which is what I think this exercise is supposed to do?
delete from warranty_policy where policy_id='p02';
-- delete from warranty_policy where policy_id='1';
------------------------------------
-- Challenge 2
------------------------------------
-- Part 2
select a2.serial_number , lw.first_name , lw.last_name
from line_worker lw
join assembly a2
on a2.employee_id = lw.employee_id
order by
a2.serial_number asc,
lw.last_name asc;
-- Part 3
-- it doesnt say sum, but i think we're supposed to use it??? adding data to practice sum()
insert into assembly (employee_id, serial_number, build_hours) values ('jadoe', 'comp123', 7);
insert into assembly (employee_id, serial_number, build_hours) values ('jdab', 'comp123', 4);
insert into assembly (employee_id, serial_number, build_hours) values ('jdab', 'comp126', 3);
insert into assembly (employee_id, serial_number, build_hours) values ('jdab', 'comp124', 1);
insert into assembly (employee_id, serial_number, build_hours) values ('jdab', 'comp127', 10);
insert into assembly (employee_id, serial_number, build_hours) values ('jdab', 'comp125', 2);
select a.serial_number , sum(a.build_hours) as total_hours
from assembly a
group by a.serial_number
order by total_hours desc