-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-insurance.sql
307 lines (277 loc) · 9.83 KB
/
2-insurance.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
-- Consider the database schemas given below.
-- Write ER diagram and schema diagram. The primary keys are underlined and the data types are
-- specified.
-- Create tables for the following schema listed below by properly specifying the primary keys and
-- foreign keys.
-- Enter at least five tuples for each relation.
-- Insurance database
-- PERSON (driver id#: string, name: string, address: string)
-- CAR (regno: string, model: string, year: int)
-- ACCIDENT (report_ number: int, acc_date: date, location: string)
-- OWNS (driver id#: string, regno: string)
-- PARTICIPATED(driver id#:string, regno:string, report_ number: int,damage_amount: int)
-- 1. Find the total number of people who owned cars that were involved in accidents in 2021.
-- 2. Find the number of accidents in which the cars belonging to “Smith” were involved.
-- 3. Add a new accident to the database; assume any values for required attributes.
-- 4. Delete the Mazda belonging to “Smith”.
-- 5. Update the damage amount for the car with license number “KA09MA1234” in the accident with report.
-- 6. A view that shows models and year of cars that are involved in accident.
-- 7. A trigger that prevents a driver from participating in more than 3 accidents in a given year.
CREATE DATABASE INSURANCE;
USE INSURANCE;
CREATE TABLE PERSON(
driver_id varchar(15) primary key,
name varchar(35) not null,
address varchar(35) not null
);
CREATE TABLE CAR(
regno varchar(15) primary key,
model varchar(30) not null,
year int not null
);
CREATE TABLE ACCIDENT(
report_number int primary key,
acc_date date not null,
location varchar(40) not null
);
CREATE TABLE OWNS(
driver_id varchar(15) not null,
regno varchar(15) not null,
foreign key (driver_id) references PERSON(driver_id) on delete cascade,
foreign key (regno) references CAR(regno) on delete cascade
);
CREATE TABLE PARTICIPATED(
driver_id varchar(15) not null,
regno varchar(15) not null,
report_number int not null,
damage_amount int not null,
foreign key (driver_id) references PERSON(driver_id) on delete cascade,
foreign key (regno) references CAR(regno) on delete cascade,
foreign key (report_number) references ACCIDENT(report_number) on delete cascade
);
INSERT INTO PERSON VALUES
("01AB11", "Smith", "Mysore"),
("01AB12", "Suresh", "Bengaluru"),
("01AB13", "Ramesh", "Chikkamagaluru"),
("01AB14", "Viraj", "Tumakuru"),
("01AB15", "Soumya", "Mandya");
INSERT INTO CAR VALUES
('KA09MC5656', 'MAZDA', 2021),
("KA09MA1111", 'HARRIER',2019),
('KA09MA1234', 'NEXON' , 2020),
('KA09MB1212', 'MERCEDES', 2020),
('KA09MD3434', 'BMW', 2018);
INSERT INTO ACCIDENT VALUES
(1021, '2021-01-01', 'Mysuru'),
(1022, '2021-02-01', 'Bengaluru'),
(1023, '2021-03-01', 'Mumbai'),
(1024, '2022-01-01', 'Chennai'),
(1025, '2023-01-01','Bengaluru');
INSERT INTO OWNS VALUES
("01AB11", "KA09MC5656"),
("01AB11", "KA09MA1111"),
("01AB13", "KA09MA1234"),
("01AB14", "KA09MB1212"),
("01AB15", "KA09MD3434");
INSERT INTO PARTICIPATED VALUES
("01AB11", "KA09MC5656", 1021, 30000),
("01AB11", "KA09MA1111", 1022, 40000),
("01AB13", "KA09MA1234", 1023, 50000),
("01AB14", "KA09MB1212", 1024, 60000),
("01AB15", "KA09MD3434", 1025, 700000);
SELECT *FROM PERSON;
/*
+-----------+--------+----------------+
| driver_id | name | address |
+-----------+--------+----------------+
| 01AB11 | Smith | Mysore |
| 01AB12 | Suresh | Bengaluru |
| 01AB13 | Ramesh | Chikkamagaluru |
| 01AB14 | Viraj | Tumakuru |
| 01AB15 | Soumya | Mandya |
+-----------+--------+----------------+
5 rows in set (0.01 sec)
*/
SELECT *FROM CAR;
/*
+------------+----------+------+
| regno | model | year |
+------------+----------+------+
| KA09MA1111 | HARRIER | 2019 |
| KA09MA1234 | NEXON | 2020 |
| KA09MB1212 | MERCEDES | 2020 |
| KA09MC5656 | MAZDA | 2021 |
| KA09MD3434 | BMW | 2018 |
+------------+----------+------+
5 rows in set (0.00 sec)
*/
SELECT *FROM ACCIDENT;
/*
+---------------+------------+-----------+
| report_number | acc_date | location |
+---------------+------------+-----------+
| 1021 | 2021-01-01 | Mysuru |
| 1022 | 2021-02-01 | Bengaluru |
| 1023 | 2021-03-01 | Mumbai |
| 1024 | 2022-01-01 | Chennai |
| 1025 | 2023-01-01 | Bengaluru |
+---------------+------------+-----------+
5 rows in set (0.00 sec)
*/
SELECT *FROM OWNS;
/*
+-----------+------------+
| driver_id | regno |
+-----------+------------+
| 01AB11 | KA09MC5656 |
| 01AB11 | KA09MA1111 |
| 01AB13 | KA09MA1234 |
| 01AB14 | KA09MB1212 |
| 01AB15 | KA09MD3434 |
+-----------+------------+
5 rows in set (0.00 sec)
*/
SELECT *FROM PARTICIPATED;
/*
+-----------+------------+---------------+---------------+
| driver_id | regno | report_number | damage_amount |
+-----------+------------+---------------+---------------+
| 01AB11 | KA09MC5656 | 1021 | 30000 |
| 01AB11 | KA09MA1111 | 1022 | 40000 |
| 01AB13 | KA09MA1234 | 1023 | 50000 |
| 01AB14 | KA09MB1212 | 1024 | 60000 |
| 01AB15 | KA09MD3434 | 1025 | 700000 |
+-----------+------------+---------------+---------------+
5 rows in set (0.00 sec)
*/
-- Find the total number of people who owned cars that were involved in accidents in 2021
SELECT COUNT(driver_id)
FROM PARTICIPATED p, ACCIDENT a
WHERE p.report_number=a.report_number AND a.acc_date LIKE "2021%";
/*
+------------------+
| COUNT(driver_id) |
+------------------+
| 3 |
+------------------+
1 row in set (0.03 sec)
*/
-- Find the number of accidents in which the cars belonging to “Smith” were involved.
SELECT COUNT(DISTINCT a.report_number)
FROM PARTICIPATED ptd, PERSON p, ACCIDENT a
WHERE ptd.driver_id=p.driver_id and ptd.report_number=a.report_number AND p.name LIKE '%smith%';
/*
+---------------------------------+
| COUNT(DISTINCT a.report_number) |
+---------------------------------+
| 2 |
+---------------------------------+
1 row in set (0.01 sec)
*/
-- Add a new accident to the database; assume any values for required attributes.
INSERT INTO ACCIDENT VALUES
(1026, '2023-04-04', 'Chennai');
-- Query OK, 1 row affected (0.04 sec)
INSERT INTO PARTICIPATED VALUES
('01AB14','KA09MB1212',1026,65000);
-- Query OK, 1 row affected (0.04 sec)
SELECT *FROM ACCIDENT;
/*
+---------------+------------+-----------+
| report_number | acc_date | location |
+---------------+------------+-----------+
| 1021 | 2021-01-01 | Mysuru |
| 1022 | 2021-02-01 | Bengaluru |
| 1023 | 2021-03-01 | Mumbai |
| 1024 | 2022-01-01 | Chennai |
| 1025 | 2023-01-01 | Bengaluru |
| 1026 | 2023-04-04 | Chennai |
+---------------+------------+-----------+
6 rows in set (0.00 sec)
*/
SELECT *FROM PARTICIPATED;
/*
+-----------+------------+---------------+---------------+
| driver_id | regno | report_number | damage_amount |
+-----------+------------+---------------+---------------+
| 01AB11 | KA09MC5656 | 1021 | 30000 |
| 01AB11 | KA09MA1111 | 1022 | 40000 |
| 01AB13 | KA09MA1234 | 1023 | 50000 |
| 01AB14 | KA09MB1212 | 1024 | 60000 |
| 01AB15 | KA09MD3434 | 1025 | 700000 |
| 01AB14 | KA09MB1212 | 1026 | 65000 |
+-----------+------------+---------------+---------------+
6 rows in set (0.00 sec)
*/
-- Delete the Mazda belonging to “Smith”.
DELETE FROM CAR
WHERE model='mazda' AND regno IN
(SELECT CAR.regno FROM PERSON p, OWNS o WHERE p.driver_id=o.driver_id AND CAR.regno=o.regno AND p.name LIKE "%SMITH%");
-- Query OK, 1 row affected (0.03 sec)
SELECT *FROM CAR;
/*
+------------+----------+------+
| regno | model | year |
+------------+----------+------+
| KA09MA1111 | HARRIER | 2019 |
| KA09MA1234 | NEXON | 2020 |
| KA09MB1212 | MERCEDES | 2020 |
| KA09MD3434 | BMW | 2018 |
+------------+----------+------+
4 rows in set (0.00 sec)
*/
-- Update the damage amount for the car with license number “KA09MA1234” in the accident with report.
UPDATE PARTICIPATED SET damage_amount=55000 WHERE regno='KA09MA1234';
/*
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
*/
SELECT * FROM PARTICIPATED;
/*
+-----------+------------+---------------+---------------+
| driver_id | regno | report_number | damage_amount |
+-----------+------------+---------------+---------------+
| 01AB11 | KA09MA1111 | 1022 | 40000 |
| 01AB13 | KA09MA1234 | 1023 | 55000 |
| 01AB14 | KA09MB1212 | 1024 | 60000 |
| 01AB15 | KA09MD3434 | 1025 | 700000 |
| 01AB14 | KA09MB1212 | 1026 | 65000 |
+-----------+------------+---------------+---------------+
5 rows in set (0.00 sec)
*/
-- A view that shows models and year of cars that are involved in accident.
CREATE VIEW CarModelYear AS
SELECT DISTINCT(model),year
FROM CAR c,PARTICIPATED ptd
WHERE c.regno=ptd.regno;
-- Query OK, 0 rows affected (0.04 sec)
SELECT * FROM CarModelYear;
/*
+----------+------+
| model | year |
+----------+------+
| HARRIER | 2019 |
| NEXON | 2020 |
| MERCEDES | 2020 |
| BMW | 2018 |
+----------+------+
4 rows in set (0.00 sec)
*/
-- A trigger that prevents a driver from participating in more than 3 accidents in a given year.
DELIMITER $$
CREATE TRIGGER PreventParticipationn
BEFORE INSERT ON PARTICIPATED
FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM PARTICIPATED WHERE driver_id=new.driver_id)>=2 THEN
SIGNAL SQLSTATE '45000' SET message_text='Driver has already participated in 2 accidents';
END IF;
END; $$
-- Query OK, 0 rows affected (0.04 sec)
DELIMITER ;
INSERT INTO ACCIDENT VALUES
(1027, '2023-04-04', 'Chennai');
-- Query OK, 1 row affected (0.04 sec)
INSERT INTO PARTICIPATED VALUES
('01AB14','KA09MB1212',1027,65000);
-- ERROR 1644 (45000): Driver has already participated in 2 accidents