Professional Documents
Culture Documents
Lab Mannul
Lab Mannul
1. Database Schema for a customer-sale scenario Customer (Cust id: integer, Cust name: string)
Item (item_id: integer, item name: string, price: integer) Sale (bill no: integer, bill data: date,
cystoid: integer, item_id: integer, qty_sold: integer) For the above schema, perform the following
— Create the tables with the appropriate integrity constraints Insert around 10 records in each of
the tables.
Query:-
create table customer(cust_id int primary key, cust_name varchar(255) not null);
Output:-
create table item(item_id int primary key,item_name varchar(255) not null,item_price int);
Output:-
create table sale(bill_no int primary key,bii_date date,cust_id int not null,
item_id int not null,qty_sold int not null,foreign key (cust_id) references
customer(cust_id),foreign key (item_id ) references item(item_id));
Output:-
Query:-
insert into customer (cust_id,cust_name) values (1,'vashu'),(2,'abhay'),(3,'yash'),
(4,'jonh'),(5,'surbhi'),(6,'sam'),(7,'ashutosh'),(8,'mohit'),(9,'priyanka'),
(10,'muktanand');
Output:-
insert into item (item_id,item_name,item_price ) values(1,'laptop',53000),
(2,'mouse',799),(3,'keyboard',999),(4,'ssd',2999),(5,'key+mouse',1599),
(6,'monitor',17999),(7,'motherboard',6999),(8,'RAM',3999),(9,'printer',15999),
(10,'graphicscard',8999);
Output:-
(a)List all the bills for the current date with the customer names and item
numbers.
Query:-
select s.bill_no,c.cust_name,s.item_id from sale s join customer c
on s.cust_id=c.cust_id where bii_date='5 may 2023'
Output:-
(b)List the total Bill details with the quantity sold, price of the item and the
final amount.
Query:-
select s.bill_no,c.cust_name,i.item_name ,s.qty_sold,i.item_price,s.qty_sold*i.item_price as
final_amount from sale s join customer c on s.cust_id =c.cust_id join item i on
s.item_id=i.item_id;
Output:-
(c) List the details of the customer who have bought a product which has a
price>5000.
Query:-
select c.cust_id,c.cust_name from customer c join sale s on s.cust_id =c.cust_id join item i
on i.item_id = s.item_id where item_price > 5000;
Output:-
(d)Give a count of how many products have been bought by each customer.
Query:-
select cust_id,count(*) as num_product_bought from sale Group by cust_id;
Output:-
(g)Create a view which lists out the bill no, bill date, cust_id, item_id, price,
qty_sold, amount.
Query:-
create view bill_detail as selects.bill_no,s.bii_date,s.cust_id,s.item_id,i.item_price, s.qty_sold
,i.item_price * s.qty_sold as amount from sale s join item i on s.item_id=i.item_id ;
Output:-
(h)Create a view which lists the daily sales date wise for the last one week.
Query:-
create view daliy_sales1 as select i.item_name , c.cust_name from item i join sale s on
s.item_id=i.item_id join customer c on s.cust_id=c.cust_id WHERE s.bii_date >=
DATEADD(day,-7, GETDATE());
select * from daliy_sales1;
output:-
Experiment:-2
2. Database Schema for a Student Library scenario Student (Stud no: integer, Stud name: string)
Membership (Mem_no: integer, Stud no: integer) Book(book no: integer, book_name:string,
author: string) Iss_rec (iss_no: integer, iss_date: date, Mem_no: integer, book_no: integer) .
(a) Create the tables with the appropriate integrity constraints.
Query:-
create table student(stu_no int primary key,stu_name varchar(255) not null);
create table membership(Mem_no int primary key,stu_no int not null,
foreign key (stu_no) references student(stu_no));
create table book(book_no int primary key,book_name varchar(255) not null,
author varchar(255) not null);
create table iss_rec(iss_no int primary key,iss_date date not null,Mem_no int not null,book_no
int not null,foreign key (mem_no) references membership(mem_no),foreign key (book_no)
references book(book_no));
Output:-
(d) List all the issues for the current date with student and Book names.
Query:-
select s.stu_name,b. book_name,i.iss_no from iss_rec i join book b on i.book_no=b.book_no join
membership m on m.Mem_no=i.Mem_no join student s on s.stu_no=m.stu_no where
iss_date='25 may 2023';
Output:-
(e)List the details of students who borrowed book whose author is CJ DATE.
Query:-
select s.stu_no,s.stu_name ,b.book_name from iss_rec i join Membership m on
m.Mem_no=i.Mem_no join student s on s.stu_no= m.stu_no join book b on b.book_no=
i.book_no where b.author='c.j date';
Output:-
(F)Give a count of how many books have been bought by each student.
Query:-
select s.stu_no, count(i.book_no) as total_book from student s,membership m, book b, iss_rec
i where s.stu_no=m.stu_no and b.book_no=i.book_no group by s.stu_no;
Output:-
(j) Create a view which lists the daily issues-date wise for the last one week.
Query:-
create view daliy_issued as select i.iss_no ,i.iss_date from iss_rec i
WHERE i.book_no>= DATEADD(day,-7, GETDATE());
select * from daliy_issued;
Output:-
Experiment:-3
3. Database Schema for a Employee-pay scenario employee (emp_id: integer, emp_name: string)
department (dept_id: integer, emp_name: string) pay details (emp_id: integer, dept_id: integer,
basic: integer, deductions: integer, additions: integer, DOJ: date) payroll (emp_id: integer,
pay_date: date) For the above schema, perform the following:
insert into payroll (emp_id,pay_date) values (1,'7 july 2023'),(2,' 8 july 2023'),
(3,'9 july 2023'),(4,'10 july 2023'), (5,'11 july 2023');
Output:-
(c) List the employee details department wise.
Query:-
select e.emp_id, e.emp_name, d.dept_id from employee e join department d
on e.emp_name= d.emp_name;
Output:-
(d) List all the employee names who joined after particular date.
Query:-
select e.emp_name from employee e join pay_details p on e.emp_id=p.emp_id
where DOJ>='9 june 2023';
Output:-
(e)List the details of employees whose basic salary is between 10,000 and
20,000.
Query:-
select e.emp_id, e.emp_name , p.basics from employee e join pay_details p on
e.emp_id=p.emp_id where p.basics between 10000 and 20000;
Output:-
Output:-
(i)Create a view which lists out the emp_name, department, basic, deductions,
net salary.
Query:-
create view lists as select e.emp_name ,p.basics,p.deductions,p.additions from pay_details p
join employee e on e.emp_id=p.emp_id;
select *from lists;
Output:-
(j) Create a view which lists the emp_name and his netsalary.
Query:-
create view lists1 as select e.emp_name,p.basics from pay_details p join
employee e on e.emp_id=p.emp_id;
select *from lists1;
output:-
Experiment:-4
4.Database Schema for a student-Lab scenario Student (stud_no: integer,
stud_name: string, class: string) Class (class: string, descript: string) Lab (mach_no:
integer, Lab_no: integer, description: String) Allotment (Stud_no: Integer,
mach_no: integer, dayof week: string) For the above schema, perform the
following—
(a) Create the tables with the appropriate integrity constraints
Query:-
create table student1(stud_no int primary key, stud_name varchar(255), class varchar(255));
create table Class (class varchar(255), descript varchar(255));
create table Lab (mach_no int primary key, Lab_no int, descriptions varchar(255));
create table Allotment (stud_no Int, mach_no integer, dayof_week varchar(20) ,
foreign key (stud_no) references student1(stud_no),
foreign key (mach_no) references Lab(mach_no));
Output:-
(c) List all the machine allotments with the student names, lab and machine
number.
Query:-
select s.stud_name,l.Lab_no,l.mach_no from student1 s ,Lab l,
Allotment a where a.stud_no=s.stud_no and l.mach_no =a.mach_no;
Output:-
(f) Give a machine allotment detail of the stud_no 5 with his personal and
class details.
Query:-
select s.stud_no,s.stud_name,s.class,a.mach_no from student1 s,Allotment a
where a.stud_no =s.stud_no and s.stud_no=5;
Output:-
(g) Count for how many machines have been allocated in Lab_no 1 for the day
of the week as ―Monday‖.
Query:-
select count(mach_no) as count from Allotment a where a.dayof_week='mon';
Output:-
(h) How many students class wise have allocated machines in the labs?
Query:-
select count(a.stud_no) as 'allocated students in the labs' ,s.class from
Allotment a , student1 s where a.stud_no=s.stud_no group by s.class;
Output:-
(i) Create a view which lists out the stud_no, stud_name, mach_no, lab_no,
dayofweek.
Query:-
create view details as select s.stud_no,s.stud_name,a.mach_no,a.dayof_week,l.Lab_no
from student1 s,Allotment a,Lab l where s.stud_no=a.stud_no and a.mach_no=l.mach_no;
Output:-
(j) Create a view which lists the machine allotment details for ―Thursday.
Query:-
create view details1 as select a.mach_no,a.stud_no,l.Lab_no from Allotment a , Lab l where
l.mach_no=a.mach_no and dayof_week='thur' ;
output:-