0% found this document useful (0 votes)
2 views5 pages

Sailors

The document contains SQL commands for inserting data into tables related to sailors, boats, and reservations, as well as various queries to retrieve specific information about sailors and their reservations. It also includes triggers to manage reservations and prevent certain actions based on conditions, such as deleting old reservations and preventing the insertion of reservations for sailors with low ratings. Additionally, there are commands related to accidents, car ownership, and customer orders, including views and deletion of records based on specific criteria.

Uploaded by

kefivek340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Sailors

The document contains SQL commands for inserting data into tables related to sailors, boats, and reservations, as well as various queries to retrieve specific information about sailors and their reservations. It also includes triggers to manage reservations and prevent certain actions based on conditions, such as deleting old reservations and preventing the insertion of reservations for sailors with low ratings. Additionally, there are commands related to accidents, car ownership, and customer orders, including views and deletion of records based on specific criteria.

Uploaded by

kefivek340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

insert into sailors values

("S101","Albert",50,10),
("S102","Robert",49,9),
("S103","Gilbert",48,8),
("S104","Kurt",47,8),
("S105","Andrew",45,9),
("S106","Daniel",25,6),
("S107","gaga",20,5),
("S108","gugu",20,6),
("S109","gigi",19,4),
("S110","Luigi",23,2);
insert into boats values
("B101","Speeder","White"),
("B102","Titanic","Black"),
("B103","Indy","Blue"),
("B104","Firestorm","Green"),
("B105","StormRanger","Brown");

insert into reservers VALUES


("S101","B101","2019-12-12"),
("S101","B102","2019-12-13"),
("S101","B103","2019-12-14"),
("S101","B104","2019-12-15"),
("S101","B105","2019-12-16"),
("S103","B102","2028-12-12"),
("S104","B102","2027-12-12"),
("S105","B102","2026-12-12"),
("S106","B102","2025-12-12"),
("S102","B102","2029-12-12"),
("S107","B102","2024-12-12"),
("S108","B102","2023-12-12"),
("S109","B102","2023-11-12"),
("S103","B103","2020-05-05"),
("S103","B104","2024-11-01"),
("S103","B105","2025-01-01");

--1 Find the colours of boats reserved by Albert


select colour from boats as b, sailors as s,reservers as r where s.sid
= r.sid and r.bid = b.bid and s.sname = "Albert";
--2Find all sailor id’s of sailors who have a rating of at least 8 or
reserved boat 103
select distinct s.sid , sname from sailors as s , reservers as r where
s.sid = r.sid and ( s.rating >=8 or r.bid = "B103");

--3 Find thenames of sailors who have not reserved a boat whose name
contains the string “storm”.
--Order the names in ascending order.
select distinct sname from sailors as s ,reservers as r, boats as b
where r.sid = s.sid and r.bid = b.bid and b.bname not like "%storm%"
order by s.sname ;
--6 For each boat which was reserved by at least 5 sailors with age >=
40, find the boat id and
--the average age of such sailors.
SELECT reservers.bid, AVG(Sailors.age) as avg_age
FROM reservers
JOIN Sailors ON reservers.sid = Sailors.sid
WHERE Sailors.age >= 40
GROUP BY reservers.bid
HAVING COUNT(reservers.sid) >= 5;

-- delte old reservatio9ns

delimiter //
create trigger delete_old
after insert on reservers
for each row BEGIN
if new.sdate < CURRENT_DATE() THEN
signal sqlstate "45000" set message_text = "Old reservation
deleted";
DELETE from rservers where sid = new.sid and sdate = new.sdate;
end if;
end //
delimiter ;

delimiter //
create trigger del_old2
after update on reservers
for each row BEGIN
if new.sdate < CURRENT_DATE() THEN
signal sqlstate "45000" set message_text = "Old reservation
deleted";
DELETE from rservers where sid = new.sid and bid = new.bid and
sdate = new.sdate;
end if;
end //
delimiter ;

trigger to prevent deletion of active reservation


delimiter //
create trigger bad_rating
before insert on reservers
for each row
BEGIN
if (select rating from sailors as s where s.sid = new.sid) <3 THEN
signal sqlstate "45000" set message_text = "bad rating bro..
sorry";
end if ;
end //
delimiter ;

AACCCCC0

1 = no of accidents in 2021
select count(distinct p.d_id) from participated as p , accident AS a
where p.report_no = a.report_no and a.acc_date like "2021%";

2= no of accidents in which car belonging to smith were invloved


select count(*) from person
join participated on person.d_id = participated.d_id
join accident on participated.report_no = accident.report_no
join owns on person.d_id = owns.d_id
where d_name like "Driver_5"
order by person.d_id;

4 delete smith’s mazda


DELETE from car where model = "ALto800" and reg_no in(select reg_no
from owns where d_id in (select d_id from person where d_name =
"Driver_5"))
;

If damage >50000 don’t buy


delimiter //
create trigger no_buy
before insert on owns
for each ROW
begin
if ((select sum(damage_amt) from participated as p where p.d_id =
new.d_id)>=50000 )then
signal sqlstate "45000" set message_text="Sorry you can't buy
'cause is damage too high. Go play GTA bich";
end if;
end;
//

select ORDERS.order_id ,SHIPMENT.ship_date from ORDERS


join SHIPMENT on SHIPMENT.order_id = ORDERS.order_id
where w_id = "202";

select WAREHOUSE.w_id , WAREHOUSE.city from WAREHOUSE


join SHIPMENT on SHIPMENT.w_id = WAREHOUSE.w_id
join ORDERS on ORDERS.order_id = SHIPMENT.order_id
join CUSTOMER on CUSTOMER.cust_id = ORDERS.cust_id
where cname = "Kumar";

select CUSTOMER.cname as Customer_name , count(ORDERS.order_id) as


no_of_orders, avg(ORDERS.order_amt) as avg_amt from CUSTOMER
join ORDERS on CUSTOMER.cust_id = ORDERS.cust_id group by cname;

delete from ORDERS where cust_id in (select cust_id in CUSTOMER where


cname = "Kumar");

create view kumar as select w.wid, w.city from warehouse as w, shipment


as s, orders as o , customer as c where w.wid = s.wid and s.oid = o.oid
and o.cust = c.cust and cname = "Kumar";
select * from ITEM where unit_price = (select max(unit_price) from item
);

You might also like