NIRBHAYDBMS
NIRBHAYDBMS
Answer:-
(i):
create table NIRPERSON ( driver_id VARCHAR(20) Primary Key, name VARCHAR(100), address VARCHAR(100) );
create table NIRCAR (regno VARCHAR(20) Primary Key, model VARCHAR(100), year INT ); create table
Foreign Key (driver_id) REFERENCES NIRPERSON (driver_id), Foreign Key (regno) REFERENCES NIRCAR (regno));
create table NIRPARTICIPATED ( driver_id VARCHAR(20), regno VARCHAR(20), report_number INT, damage_amount INT,
Primary Key (driver_id, regno, report_number), Foreign Key (driver_id) REFERENCES NIRPERSON(driver_id), Foreign Key
(regno) REFERENCES NIRCAR(regno),
Output of Q.1.(i)
VALUES ('A52', Deepu, 'Bihar'); insert into NIRPERSON (driver_id, name, address)
VALUES ('A10', 'Adarsh', 'Delhi'); insert into NIRPERSON (driver_id, name, address)
address) VALUES ('A04', 'Pranav', 'Lucknow'); insert into NIRPERSON (driver_id, name,
-- Insert data into NIRCAR insert into NIRCAR (regno, model, year) VALUES
('R001', 'Vitara Brezza', 2015); insert into NIRCAR (regno, model, year) VALUES
('R002', 'Kia Seltos', 2016); insert into NIRCAR (regno, model, year) VALUES
('R003', 'Tata Harrier', 2017); insert into NIRCAR (regno, model, year) VALUES
('R004', 'Renault Duster', 2018); insert into NIRCAR (regno, model, year) VALUES
insert into NIRACCIDENT (report_number, accident_date, location) VALUES (1, TO_DATE('2006-03-12', 'YYYY-MM-DD'),
'Bihar');
insert into NIRACCIDENT (report_number, accident_date, location) VALUES (2, TO_DATE('2006-10-19', 'YYYY-MM-DD'),
'Delhi');
insert into NIRACCIDENT (report_number, accident_date, location) VALUES (3, TO_DATE('2007-09-21', 'YYYY-MM-DD'),
'HNIRana');
insert into NIRACCIDENT (report_number, accident_date, location) VALUES (4, TO_DATE('2008-08-26', 'YYYY-MM-DD'),
'Lucknow');
insert into NIRACCIDENT (report_number, accident_date, location) VALUES (5, TO_DATE('2009-09-20', 'YYYY-MM-DD'),
'Kolkata');
-- Insert data into NIRPARTICIPATED insert into NIRPARTICIPATED (driver_id, regno, report_number,
damage_amount) VALUES ('A12', 'R001', 1, 10000); insert into NIRPARTICIPATED (driver_id, regno,
report_number, damage_amount) VALUES ('A10', 'R002', 2, 15000); insert into NIRPARTICIPATED (driver_id,
regno, report_number, damage_amount) VALUES ('A03', 'R003', 3, 20000); insert into NIRPARTICIPATED
(driver_id, regno, report_number, damage_amount) VALUES ('A04', 'R004', 4, 25000); insert into
DRIVER_ID REGNO
A52 R001
A10 R002
A03 R003
A04 R004
A40 R005
Q.1. (iii) (a). Demonstration of the updated damaged amount for the car with specific regno in
accident with report number 12 to 25000. update NIRPARTICIPATED SET damage_amount = 25000 WHERE
regno = 'specific_regno' AND report_number = 12;
(b). Adding a new accident to the database. insert into NIRACCIDENT (report_number, accident_date,
location) VALUES (6, TO_DATE('2022-01-01', 'YYYY-MM-DD'),
'New Location'); insert into NIRPARTICIPATED (driver_id, regno, report_number, damage_amount) VALUES ('A12',
'R001', 6, 5000);
Q.1.(iv). Total No. of people who owned cars that were involved in accidents involved in 2006.
select count(DISTINCT O.driver_id) AS Total_People
FROM NIROWNS O
Q.1.(V) Number of accidents in which cars belonging to a specific model were involved: select
count(DISTINCT NIRPARTICIPATED.report_number) AS Number_Of_Accidents
FROM NIRPARTICIPATED
Q.2.(i).Creating tables by properly specifying the primary key and foreign key. create
table NIRCUSTOMER ( CustNo INT Primary Key, Cname VARCHAR(50),City VARCHAR(100)); create table
NIRORDER ( OrderNo INT Primary Key, Odate DATE, CustNo INT , Ord_Amt INT, Foreign Key (CustNo)
REFERENCES NIRCUSTOMER(CustNo) ); create table NIRORDER_ITEM ( OrderNo INT, ItemNo INT, qty INT,
create table NIRSHIPMENT ( OrderNo INT, WarehouseNo INT, ShipDate DATE, Primary Key(OrderNo,
WarehouseNo),
'YYYY-MM-DD'), 1, 500); insert into NIRORDER (OrderNo, Odate, CustNo, Ord_Amt) VALUES (102, TO_DATE('2023-02-01',
'YYYY-MM-DD'), 2, 700); insert into NIRORDER (OrderNo, Odate, CustNo, Ord_Amt) VALUES (103, TO_DATE('2023-03-01',
'YYYY-MM-DD'), 3, 300); insert into NIRORDER (OrderNo, Odate, CustNo, Ord_Amt) VALUES (104, TO_DATE('2023-04-01',
'YYYY-MM-DD'), 4, 600); insert into NIRORDER (OrderNo, Odate, CustNo, Ord_Amt) VALUES (105, TO_DATE('2023-05-01',
'YYYY-MM-DD'), 5, 800);
-- Insert data into CUSTOMER insert into NIRCUSTOMER (CustNo, Cname, City) VALUES
(1, 'John Doe', 'New York'); insert into NIRCUSTOMER (CustNo, Cname, City) VALUES
(2, 'Jane Smith', 'Los Angeles'); insert into NIRCUSTOMER (CustNo, Cname, City)
VALUES (3, 'Robert Brown', 'Chicago'); insert into NIRCUSTOMER (CustNo, Cname, City)
VALUES (4, 'Emily Davis', 'Houston'); insert into NIRCUSTOMER (CustNo, Cname, City)
-- Insert data into SHIPMENT insert into NIRSHIPMENT (OrderNo, WarehouseNo, ShipDate) VALUES (101, 1,
TO_DATE('2023-01-01', 'YYYY-MM-DD')); insert into NIRSHIPMENT (OrderNo, WarehouseNo, ShipDate) VALUES (102, 2,
TO_DATE('2023-01-01', 'YYYY-MM-DD')); insert into NIRSHIPMENT (OrderNo, WarehouseNo, ShipDate) VALUES (103, 3,
TO_DATE('2023-01-01', 'YYYY-MM-DD')); insert into NIRSHIPMENT (OrderNo, WarehouseNo, ShipDate) VALUES (104, 4,
TO_DATE('2023-01-01', 'YYYY-MM-DD')); insert into NIRSHIPMENT (OrderNo, WarehouseNo, ShipDate) VALUES (105, 5,
TO_DATE('2023-01-01', 'YYYY-MM-DD'));
AVG(OrdAmt) AS Average_Order_Amount
FROM NIRCUSTOMER
GROUP BY Cname;
CUSTOMER_NAME NUMBER_OF_ORDERS AVERAGE_ORDER_AMOUNT
DISTINCT OrderNo
FROM NIRSHIPMENT
101
Q.2.(v).Demonstration of Deleting Item# 10 from the ITEM table and make that field null in the ORDER-ITEM table
update NIRORDER_ITEM SET ItemNo = NULL WHERE ItemNo = 10; delete from NIRITEM WHERE ItemNo = 10;
-- i) Alter the data type of e_no from number to varchar (already created as VARCHAR)
create table NIRDept ( dept_no INT, dept_name VARCHAR(50), e_no VARCHAR(20), dept_loc
VARCHAR(100), dept_hod VARCHAR(50), Primary Key (dept_no), Foreign Key (e_no) REFERENCES
NIREmp(e_no) );
-- i) Create the reference between Emp and Dept table with e_no attribute (done with FOREIGN KEY)
-- ii) Assign dept_no as primary key (already done in table creation)
-- iii) Update the dept_hod for one department update NIRDept SET
Q.5. Queries
(i) Finding the employee name and dept_hod whose dept_hod is John select NIREmp.e_name,
(ii) Finding the average salNIR of the employee of CSE department select
AVG(NIREmp.e_salNIR) AS Average_SalNIR
AVERAGE_SALNIR
Q.6.Queries: -
(i) Displaying the records employee whose name starts with ‘a’. select * from NIREmp
create table NIREMP ( e_no VARCHAR(20) Primary Key, e_name VARCHAR(50), e_phone VARCHAR2(30), e_addr
VARCHAR(100), e_salNIR NUMBER );
insert into NIREMP (e_no, e_name, e_phone, e_addr, e_salNIR) VALUES ('E001', 'Alice', '1234567890', '123 Maple St',
50000);
insert into NIREMP (e_no, e_name, e_phone, e_addr, e_salNIR) VALUES ('E002', 'Bob', '0987654321', '456 Oak St', 55000);
-- Starting a transaction and create a savepoint
BEGIN
-- Creating a savepoint
SAVEPOINT my_savepoint;
insert into NIREMP (e_no, e_name, e_phone, e_addr, e_salNIR) VALUES ('E003', 'Charlie', '5556667777', '789 Pine St',
60000); update NIREMP SET e_salNIR = 58000 WHERE e_no =
'E001';
ROLLBACK TO my_savepoint;
COMMIT; END;
27-Jul-24