-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbms_tables.sql
230 lines (161 loc) · 5.42 KB
/
dbms_tables.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
create table custInfo
(
mail_id varchar(30) not null ,
password varchar(30) not null ,
name varchar(40) not null,
phone_no int(13) ,
default_addr varchar(40) ,
login_count int ,
primary key(mail_id)
);
create table distanceInfo
(
location1 varchar(40),
location2 varchar(40),
distance float(5,2) not null,
primary key(location1,location2)
);
create table taxiBooking
(
booking_id int unsigned not null auto_increment,
pickup_time timestamp not null,
pickup_addr varchar(40) not null,
dest_addr varchar(40) not null,
car_name varchar(30) not null,
car_id_assigned varchar(30) ,
time_of_booking timestamp default current_timestamp not null,
price decimal(8,2) ,
check(timestampdiff(hour,current_timestamp,pickup_time) > 1),
foreign key(pickup_addr,dest_addr) references distanceInfo(location1,location2),
primary key(booking_id)
);
create table carInfo
(
car_id varchar(40),
car_type varchar(40) default 'taxi' ,
car_name varchar(40),
brand varchar(40),
car_condition varchar(40) default 'working',
cost int default 1000000,
charge_perkm decimal(5,2) default 15.00 ,
rcharge_perday decimal(7,2) default 1000.00,
availability varchar(40) default 'available' ,
primary key(car_id)
);
create table driverInfo
(
driver_id varchar(40),
drive_name varchar(40),
phone_no int not null ,
driving_since int default 2,
salary int default 5000 ,
primary key(driver_id)
);
create table bookingInfo
(
user_id varchar(30) not null,
booking_id int unsigned not null,
foreign key(user_id) references custInfo(mail_id),
foreign key(booking_id) references taxiBooking(booking_id),
primary key(user_id,booking_id)
);
create table booked_for
(
booking_id int unsigned ,
car_id varchar(40),
foreign key(booking_id) references taxiBooking(booking_id),
foreign key(car_id) references carInfo(car_id),
primary key(booking_id , car_id)
);
create table driven_by
(
car_id varchar(40),
driver_id varchar(40),
foreign key(car_id) references carInfo(car_id),
foreign key(driver_id) references driverInfo(driver_id),
primary key(car_id,driver_id)
);
create table car_rental
(
rental_id varchar(40),
car_name varchar(40),
duration int not null ,
rental_date timestamp not null,
time_of_booking timestamp default current_timestamp ,
price int ,
primary key(rental_id)
);
create table rentalInfo
(
user_id varchar(40),
rental_id varchar(40),
foreign key(user_id) references custInfo(mail_id),
foreign key(rental_id) references car_rental(rental_id) ,
primary key(user_id , rental_id)
);
delimiter //
create function getAvailability(pickup_time timestamp , car_id varchar(40))
returns varchar(40)
begin
declare answer1 varchar(40) ;
declare busy_count integer ;
select count(*) into busy_count from taxiBooking , booked_for
where taxiBooking.booking_id = booked_for.booking_id and booked_for.car_id = carInfo.car_id and
abs(timestampdiff(hour,taxiBooking.pickup_time,pickup_time)) <2 ;
if busy_count = 0 then set answer1 = 'available';
else set answer1 = 'not available';
end if;
return answer1 ;
end //
delimiter ;
delimiter //
create function assignTaxi(bookingId int)
returns float(8,2)
begin
declare price float(5,2);
declare dist integer ;
declare pickTime timestamp ;
declare pickup ,dest , carId,carName varchar(40) ;
select taxiBooking.pickup_addr into pickup
from taxiBooking where taxiBooking.booking_id = bookingId ;
select taxiBooking.dest_addr into dest
from taxiBooking where taxiBooking.booking_id = bookingId ;
select taxiBooking.car_name into carName
from taxiBooking where taxiBooking.booking_id = bookingId ;
select taxiBooking.pickup_time into pickTime
from taxiBooking where taxiBooking.booking_id = bookingId ;
select carInfo.car_id into carId from carInfo
where strcmp(car_type,"taxi")=0 and carInfo.car_name = carName and
strcmp(getAvailability(pickTime , carInfo.car_id ) , "available")=0 limit 1 ;
select carInfo.charge_perkm into price from carInfo
where carInfo.car_id = carId;
insert into booked_for (booking_id , car_id ) values (bookingId , carId) ;
select distanceInfo.distance into dist from distanceInfo where distanceInfo.location1 = dest and distanceInfo.location2 = pickup ;
return distance*price ;
end //
delimiter ;
delimiter //
create trigger finalAssignTaxi
after insert on taxiBooking
for each row
begin
update taxiBooking set taxiBooking.price = assignTaxi(taxiBooking.booking_id);
end //
delimiter ;
delimiter //
create function getCost(pickup_addr varchar(40) , dest_addr varchar(40) , car_name varchar(40))
returns float(7,2)
begin
declare price float(5,2);
declare dist int;
insert into price select carInfo.charge_perkm from carInfo where carInfo.car_name = car_name limit 1 ;
insert into dist select distanceInfo.distance where distanceInfo.location1=pickup_addr and distanceInfo.location2=dest_addr ;
return price*dist ;
end //
delimiter ;
insert into carInfo ( car_id ,car_name , car_type , brand , car_condition , cost , charge_perkm , rcharge_perday ,availability ) values
('WB11S1001','Maruti Swift','taxi','Maruti Suzuki','working' , 600000 , 20 , 800 , 'available') ,
('WB06V2525','Maruti Omni','taxi','Maruti Suzuki','working', 450000 , 15 , 700 , 'available') ,
('WB14Q6554','Mahindra Scorpio','taxi','Mahindra','working',1000000, 25 , 1500 , 'available') ,
('WB10Y5111','Hyundai Verna','taxi','Hyundai','working',1100000,30,1600,'available') ,
('WB04Z1447','Honda City','taxi','Honda','working', 1300000, 35 , 1700 ,'available');