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

Ar Dbs Final

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

DATABASE MANAGEMENT SYSTEMS

Practical File
CBCPC05

Submitted By: Submitted To:


Arpita Arora Dr. Manoj Kumar
Roll No: 2022UCB6002

Department of Computer Science and Engineering


Netaji Subash University of Technology, East Campus
Geeta Colony, New Delhi – 110031
INDEX

S.NO TOPIC DATE SIGN

SQL Queries and Relational Algebra for


database
1 11-09-23
SAILORS (sid, sname, rating, date_of_birth)
BOATS (bid, bname, color)
RESERVES (sid, bid, date, time slot)

SQL Queries and Relational Algebra for


database
2 CUSTOMER (cust_num, cust_lname , cust_fname, 16-10-23
cust_balance);
PRODUCT (prod_num, prod_name, price);
INVOICE (inv_num, prod_num, cust_num,
inv_date ,unit_sold, inv_amount);

SQL Queries and Relational Algebra for


database
3 30-10-23
DEPARTMENT(Department_ID, Name,
Location_ID)
JOB (Job_ID , Function )
EMPLOYEE (Employee_ID, name, DOB, Job_ID ,
Manager_ID, Hire_Date, Salary, department_id)
Q1) 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 NOTNULL,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 boatsVALUES (14, 'boat4', 'yellow');
INSERT INTO boats VALUES (15, 'boat5', 'pink');
INSERT INTOboats 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 INTOboats 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-012',"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 ofsailors who’ve reserved a red or a greenboat in the month


of March.

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


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

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

f) Find sailors who’ve reserved allboats.

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

h) Find the age of the youngestsailorfor each rating withat least 2 such
sailors
RELATIONAL ALGEBRA FOR 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_numint not
null,invoice_date date, unit_sold int, invoice_amountint, 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);insertinto customer
values(12 , 'Ronak' , 'Godara' , 8000);
insert into customer values(13 , 'Atharav' , 'Mahajan' ,40000); insertinto
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);
insertintocustomer 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 than three units of a
product in a day

d) Write a query to illustrate Left Outer, Right Outer and Full 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


Q3) 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_IDint 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 INTODEPARTMENT VALUES (6,'designing',30);
INSERT INTO DEPARTMENT VALUES (7,'hardware',40);
INSERT INTODEPARTMENT VALUES (8,'technical',50);
INSERT INTO JOB VALUES(101,'in coding');
INSERT INTO JOB VALUES(102,'in security');
INSERT INTO JOB VALUES(103,'in technical');
INSERT INTOJOB VALUES(104,'in fitnessteam');
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 INTOEMPLOYEEVALUES(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 who joined 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 less than 25 can be


inserted in the database.
Relational Algebra for Q3:

You might also like