Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

2021uci6516 Dbmsfile

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

DATABASE MANAGEMENT

SYSTEMS
PRACTICAL FILE
(CICPC05)

SUBMITTED TO : SUBMITTED BY:


Dr. Vishal Gupta Abhay Gupta
Roll No.: 2021UCI6516
Branch: CSE (IOT)

NETAJI SUBHAS UNIVERSITY OF TECHNOLOGY


Geeta Colony, Delhi
Pin: 110031
TABLE OF CONTENT

S.NO NAME OF EXPERIMENT PAGE DATE SIGNATURE


NO

TEACHER’S SIGNATURE
Ques1) Consider the following relational schema :

SAILORS (sid, sname, rating, date_of_birth)


BOATS (bid, bname, color)
RESERVES (sid, bid, date, time slot)

Write the following queries in SQL and relational algebra :

CREATE TABLE sailors (sid int PRIMARY


KEY, sname varchar(20), rating int,
date_of_birth date
);
CREATE TABLE boats (
bid int PRIMARY KEY,bname
varchar(20), color varchar(20)
);
CREATE TABLE reserves (sid int NOT NULL,
bid int NOT NULL,rdate date,
timeslot time,
FOREIGN KEY (sid) REFERENCES sailors(sid),FOREIGN KEY (bid)
REFERENCES boats(bid)
);

INSERT INTO sailors VALUES (1, 'Ryan', 5,'1975-04-23');


INSERT INTO sailors VALUES (2, 'Joanna',3, '1971-07-12');
INSERT INTO sailors VALUES (3, 'Alex',7, '1973-02-19'); INSERT INTO sailors VALUES (4, 'Trish',6,
'1971-04-13'); INSERT INTO sailors VALUES (5, 'Harry',9, '1971-09-15'); INSERT INTO sailors
VALUES (6, 'John', 8,'1976-08-15'); INSERT INTO sailors VALUES (7, 'Repter',9, '1968-07-12');
INSERT INTO sailors VALUES (8, 'Liam', 6,'1969-01-17');

INSERT INTO boats VALUES (11, 'boat1', 'red'); INSERT INTO boats VALUES (12,
'boat2', 'blue'); INSERT INTO boats VALUES (13, 'boat3', 'green');INSERT INTO
boats VALUES (14, 'boat4', 'yellow'); INSERT INTO boats VALUES (15, 'boat5',
'pink'); INSERT INTO boats VALUES (16, 'boat6', 'orange');INSERT INTO boats
VALUES (17, 'boat7', 'green');INSERT INTO boats VALUES (18, 'boat8', 'black');
INSERT INTO boats VALUES (19, 'boat9', 'red'); INSERT INTO boats VALUES (20,
'boat10', 'blue');

INSERT INTO reserves VALUES (1,13,'2021-01-


12',"19:30:10");
INSERT INTO reserves VALUES (2,12,'2021-02-
16',"5:20:40");
INSERT INTO reserves VALUES (3,16,'2021-06-
20',"11:10:24");
INSERT INTO reserves VALUES (4,17,'2021-07-
5',"19:40:45");
INSERT INTO reserves VALUES (4,19,'2021-01-
2',"12:20:34");
INSERT INTO reserves VALUES (6,13,'2021-02-
28',"15:00:00");
INSERT INTO reserves VALUES (7,20,'2021-03-
14',"29:50:12");
INSERT INTO reserves VALUES (2,15,'2021-05-
21',"9:26:11");

a) Find sailors who’ve reserved at least one boat

b) Find names of sailors who’ve reserved a red or a greenboat in the month of March.

c) Find names of sailors who’ve reserved a red and agreen boat.


d) Find sid of sailors who have not reserved a boat afterJan 2018.

e) Find sailors whose rating is greater than that of all thesailors named “John”

f) Find sailors who’ve reserved all boats.

g) Find name and age of the oldest sailor(s).


h) Find the age of the youngest sailor for each rating withat least 2 such sailors

Relational Algebra of Q1
Ques 2) Consider the following relational schema:

CUSTOMER (cust_num, cust_lname , cust_fname, cust_balance);


PRODUCT (prod_num, prod_name, price)
INVOICE (inv_num, prod_num, cust_num, inv_date ,unit_sold, inv_amount);
Write SQL queries and relational algebraic expression for the following

create table customer ( cust_num int


primary key,cust_fname varchar(20),
cust_lname varchar(20), cust_balance int
);
create table product ( prod_num int
primary key,prod_name varchar(30), price
int
);
create table invoice( invoice_num
int, prod_num int , cust_num int
not null,invoice_date date,
unit_sold int,
invoice_amount int,
primary key (invoice_num ,prod_num , cust_num ,
invoice_date),
foreign key (cust_num) references customer(cust_num)on delete cascade,
foreign key (prod_num) references product(prod_num)on delete cascade
);

insert into customer values(11 , 'Rohan' , 'Singh' , 5000);insert into customer values(12 ,
'Ronak' , 'Godara' , 8000);insert into customer values(13 , 'Atharav' , 'Mahajan' ,40000);
insert into customer values(14 , 'Ishan' , 'Mehta' , 10000);insert into customer values(15 , 'Virat'
, 'Kohli' , 9000); insert into customer values(16 , 'Tanishq' , 'Mehta' ,12000);
insert into customer values(17 , 'Justin' , 'Timberlake' ,20000);
insert into customer values(18 , 'Taylor' , 'Swift' , 50000);insert into customer values(19 , 'Rock'
, 'Bottom' , 5000);insert into customer values(20 , 'Tyler' ,'woods' , 0);

insert into product values(101, 'Register', 30);insert into product values(102,


'Pen', 10);
insert into product values(103, 'Laptop', 50000);insert into product
values(104, 'Mouse', 1300); insert into product values(105, 'Tablet', 25000);
insert into product values(106, 'Bag', 770);
insert into product values(107, 'Colours', 150); insert into product values(108,
'Mobile', 18000);

insert into invoice values(1,'101' ,13 ,'2021-11-21' ,2 ,30);


insert into invoice values(2,'104' ,14 ,'2021-12-10' ,1
,1300);
insert into invoice values(3,'102' ,11 ,'2015-10-12' ,1
,40000);
insert into invoice values(4,'105' ,16 ,'2011-01-22' ,3
,1400);
insert into invoice values(5,'106' ,13 ,'2021-05-19' ,2
,1500);
insert into invoice values(6,'102' ,12 ,'2010-12-09' ,12
,100);
insert into invoice values(7,'105' ,15 ,'2018-09-03' ,10
,5000);
insert into invoice values(8,'108' ,16 ,'2019-10-22' ,2
,1500);
insert into invoice values(9,'107' ,18 ,'2020-01-18' ,12
,150);
insert into invoice values(10,'104' ,14 ,'2021-11-23' ,5
,6000);
insert into invoice values(11,'103' ,12 ,'2017-10-13' ,2
,75000);
insert into invoice values(12,'108' ,16 ,'2020-11-31' ,1
,8000);
insert into invoice values(12,'108' ,17 ,'2020-11-23' ,1
,8000);
a) Find the names of the customer who have purchasedno item. Set default value of
Cust_balance as 0 for
such customers.

b) Write the trigger to update the CUST_BALANCE in theCUSTOMER table when a new
invoice record
is entered for the customer.

c) Find the customers who have purchased more thanthree units of a product in a day.
d) Write a query to illustrate Left Outer, Right Outer andFull Outer Join.
left:

right:
Full:

e) Count the number of products sold on each date.


f) As soon as customer balance becomes greater thanRs. 100,000, copy the
customer_num in new table
called ”GOLD_CUSTOMER”
g) Add a new attribute CUST_DOB in customer table

Relational Algebra for Q2:


Ques3: Consider the following relational schema :

DEPARTMENT(Department_ID, Name, Location_ID)


JOB (Job_ID , Function )
EMPLOYEE (Employee_ID, name, DOB, Job_ID , Manager_ID, Hire_Date, Salary,
department_id)

Answer the following queries using SQL and relational algebra:

CREATE TABLE DEPARTMENT (


Department_ID int PRIMARY KEY,Name varchar(20)
NOT NULL, Location_ID INT
);

CREATE TABLE JOB ( Job_ID int PRIMARY


KEY,functions varchar(50)
);

CREATE TABLE EMPLOYEE (


Employee_ID int, Name
varchar(20),dob date,
Job_ID int, Manager_ID int,
Hire_Date date,
Salary int, department_ID INT,
FOREIGN KEY (Job_ID) REFERENCES JOB(Job_ID), FOREIGN KEY
(Department_ID) REFERENCES
department(Department_ID)
);

INSERT INTO DEPARTMENT VALUES (1,'coding',20); INSERT INTO DEPARTMENT VALUES


(2,'security',60); INSERT INTO DEPARTMENT VALUES (3,'finance',90); INSERT INTO
DEPARTMENT VALUES (4,'analytics',90);INSERT INTO DEPARTMENT VALUES
(5,'fitness',10); INSERT INTO DEPARTMENT VALUES (6,'designing',30);INSERT INTO
DEPARTMENT VALUES (7,'hardware',40);INSERT INTO DEPARTMENT VALUES
(8,'technical',50);

INSERT INTO JOB VALUES(101,'in coding'); INSERT INTO JOB VALUES(102,'in


security'); INSERT INTO JOB VALUES(103,'in techniacal'); INSERT INTO JOB
VALUES(104,'in fitness team');INSERT INTO JOB VALUES(105,'in finance');
INSERT INTO JOB VALUES(106,'in analytics'); INSERT INTO JOB VALUES(107,'in
hardware'); INSERT INTO JOB VALUES(108,'in designing');

INSERT INTO EMPLOYEE VALUES(1001,'rohan','1999-02-24',102,201,'2012-01-11',100000,2);


INSERT INTO EMPLOYEE VALUES(1002,'tanishq','1997-01-13',102,201,'2016-02-
15',50000,2);
INSERT INTO EMPLOYEE VALUES(1003,'Rakul','1999-09-16',104,201,'2015-03-01',60000,4);
INSERT INTO EMPLOYEE VALUES(1004,'bajaj','1997-07-18',105,201,'2018-11-11',50000,1);
INSERT INTO EMPLOYEE VALUES(1005,'Chirag','1996-11
-17',105,201,'2014-11-25',75000,1);
INSERT INTO EMPLOYEE VALUES(1006,'Sohan','1998-12
-11',108,201,'2021-01-01',80000,7);
INSERT INTO EMPLOYEE VALUES(1007,'aryan','1997-02-28',102,201,'2019-10-18',10000,2);
INSERT INTO EMPLOYEE VALUES(1008,'bhavya','1998-03-23',108,201,'2012-12-
15',80000,7);

a) Write a query to count number of employees whojoined in March 2015

b) Display the Nth highest salary drawing employee


details.
c) Find the budget (total salary) of each department.

d) Find the department with the maximum budget.

e) Create a view to show number of employees workingin Delhi and update it


automatically when the
database is modified.

f) Write a trigger to ensure that no employee of age lessthan 25 can be inserted in the
database.

Relational Algebra for Ques 3:

You might also like