+ All Categories
Home > Documents > dbms file

dbms file

Date post: 27-Dec-2015
Category:
Upload: adam-moore
View: 52 times
Download: 0 times
Share this document with a friend
Description:
lab file
31
IILM COLLEGE OF ENGG & TECHNOLOGY DATABASE MANAGEMENT SYSTEM LAB FILE (CS 402P) CSE 4 TH SEM SUBMITTED TO: SUBMITTED BY: Mr. ANKUR AGGARWAL
Transcript
Page 1: dbms file

IILM COLLEGE OF ENGG & TECHNOLOGY

DATABASE MANAGEMENT SYSTEM LAB

FILE

(CS 402P)

CSE 4TH SEM

SUBMITTED TO: SUBMITTED BY:

Mr. ANKUR AGGARWAL

Page 2: dbms file

1

INDEX

S.no Program name Date Signature

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, cust_id:

integer, item_id: integer, qty_sold: integer)

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)

3 3. Database Schema for a Employee-pay scenario

employee(emp_id : integer, emp_name: string)

department(dept_id: integer, dept_name:string)

paydetails(emp_id : integer, dept_id: integer, basic:

integer, deductions: integer, additions: integer, DOJ:

date)payroll(emp_id : integer, pay_date: date)

4 Database Schema for a Video Library scenario

Customer(cust_no: integer,cust_name: string)

Membership(Mem_no: integer, cust_no: integer)

Cassette(cass_no:integer, cass_name:string, Language:

String) Iss_rec(iss_no: integer, iss_date: date,

mem_no: integer, cass_no: integer)

5 Database Schema for a student-Lab scenario

Student(stud_no: integer, stud_name: string, class:

string),Class(class: string, descrip: string)

Lab(mach_no: integer, Lab_no: integer, description:

String),Allotment(Stud_no: Integer, mach_no: integer,

dayof week: string)

6 Create a procedure which updates the salaries

of an employees as follows.

7 Create a procedure which updates the salaries of an employees as follows.

Page 3: dbms file

2

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, cust_id: integer, item_id: integer,

qty_sold: integer)

For the above schema, perform the following—

a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables

c) List all the bills for the current date with the customer names and item numbers

d) List the total Bill details with the quantity sold, price of the item and the

final amount e) List the details of the customer who have bought a product which has a

price>200 f) Give a count of how many products have been bought by each customer

g) Give a list of products bought by a customer having cust_id as 5 h) List the item details which are sold as of today i) Create a view which lists out the bill_no, bill_date, cust_id, item_id, price,

qty_sold, amount.

Page 4: dbms file

3

Aim:

a) Create the tables with the appropriate integrity constraints and Insert around

10 records in each of the tables

SQL> create table customer1 (cust_id number(5) primary key, cust_name

varchar2(15));

Output: Table created.

SQL> desc customer1;

Output:

Name Null? Type

----------------------------------------- -------- ----------------

CUST_ID NOT NULL NUMBER(5)

CUST_NAME VARCHAR2(15)

b) SQL> insert into customer1 values(&cust_id,'&cust_name');

SQL> select * from customer1;

Output:

CUST_ID CUST_NAME

---------- ---------------

100 ramu

101 kamal

102 raju

103 raju sundaram

104 lawrence

SQL> create table item(item_id number(4) primary key,

item_name varchar2(15),price number(6,2));

SQL> dsec item

Output:

Name Null? Type

……………………………………………………………………………………………………

Cust_id NOT NULL NUMBER(4)

Item_name VARCHAR2(15)

PRICE NUMBER(6,2)

SQL>insert into item values(&item_id,’&item_name’,&price);

Page 5: dbms file

4

SQL> select * from item;

Output:

ITEM_ID ITEM_NAME PRICE

……………………………………………………………………………………..

2334 geera 6.25

4532 corn soup 34.65

2124 lays chips 20

4531 setwet 99.99

2319 duracell 45.5

SQL>create table sale(bill_no number(5) primary key,bill_date date, cust_id

number(5) references customer(cust_id), item_id number(4) references

item(item_id),qty_sold number(4));

Output: Table Created.

SQL>dsec sale

Output:

Name Null? Type

………………………………………………………………………………………..

BILL_NO NOT NULL NUMBER(4)

BILL_DATE DATE

CUST_ID NUMBER(5)

ITEM_ID NUMBER(4)

QTY_SOLD NUMBER(4)

SQL>insert into Sale values(&bill_no, ’&bill_date’,

&cust_id, &item_id, &qty_sold);

SQL>select * from sale;

Output:

BILL_NO BILL_DATE CUST_ID ITEM_ID QTY_SOLD

………………………………………………………………………………………………………...

1450 04-JAN-06 100 2124 2

1451 04-JAN-06 101 2319 1

1452 04-JAN-06 103 4531 2

1453 04-JAN-06 102 2334 3

1454 04-JAN-06 104 4532 3

Page 6: dbms file

5

c) List all the bills for the current date with the customer names and item

numbers

SQL> select c.custname, i.itemid, s.billno from customer c, item I, sale s

where c.custid=s.custid and

s.billdate=to_char(sysdate);

CUSTNAME ITEMID BILLNO

------------- --------- ---------

John 5001 332

d) List the total Bill details with the quantity sold, price of the item and the final

amount.

SQL> select i.price, s.qty,(i.price*s.qty) total from item I, sale s where

i.itemid=s.itemid;

PRICE QTY TOTAL

------- ----- --------

120 2 240

20 3 60

5 2 10

10 1 10

350 4 1400

e) List the details of the customer who have bought a product which has a

price>200

SQL> select c.custid, c.custname from customer c, sale s, item i where

i.price>200 and

c.custid=s.custid and i.itemid=s.itemid;

CUSTID CUSTNAME

--------- --------------

4 duffy

Page 7: dbms file

6

f) Give a count of how many products have been bought by each customer

SQL> select custid, count(itemid) from sale group by custid;

CUSTID COUNT(ITEMID)

---------- ---------------------

1 2

3 1

4 1

5 1

g) Give a list of products bought by a customer having cust_id as 5

SQL> select i.itemname from item i, sale s where s.custid=5 and i.itemid-

s.itemid;

ITEMNAME

--------------

Pens

h) List the item details which are sold as of today

SQL> select i.itemid, i.itemname from item I, sale s where

i.itemid=s.itemid

and s.billdate=to_char(sysdate);

ITEMID ITEMNAME

--------- -------------

1234 pencil

i) Create a view which lists out the bill_no, bill_date, cust_id, item_id, price, qty_sold, amount

SQL>create view cust as (select s.billno, s.billdate, c.custid, i. iitemid,

i.price, s.qty from customer c,sale s item I where c.custid=s.custid

and i.iemid=s.itemid);

view created.

SQL>select * from cust;

BILLNO BILLDATE CUSTID ITEMID PRICE QTY

……………………………………………………………………………………………

3432 12-JAN-06 3 3244 120 2

4424 20-FEB-06 1 3456 20 3

332 13-MAR-06 1 1234 5 2

2343 10-MAR-06 5 5001 10 1

1331 11-MAR-06 4 76776 350 4

Page 8: dbms file

7

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)

For the above schema, perform the following—

a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables

c) List all the student names with their membership numbers d) List all the issues for the current date with student and Book names

e) List the details of students who borrowed book whose author is CJDATE f) Give a count of how many books have been bought by each student g) Give a list of books taken by student with stud_no as 5

h) List the book details which are issued as of today

Page 9: dbms file

8

AIM:

Create the tables with the appropriate integrity constraints

Insert around 10 records in each of the tables

SQL>create table student(stud_no number(5) primary key,stud_name

varchar2(15));

SQL>desc student;

Name Null? Type

………………………………………………………………………………………..

STUD_NO NOT NULL NUMBER(5)

STUD_NAME VARCAHR2(15)

Valid Test Data:

SQL>insert into student values(&stud_no,’&stud_name’);

SQL>select * from student;

STUD_NO STUD_NAME

....................................................................

508 HARISH

513 BALAJI

518 RAKESH

524 PAVAN

534 JOYCE

SQL>create table membership(mem_no number(5) primary key,stud_no

number(5) references student(stud_no));

SQL>dsec membership;

Name Null? Type

…………………………………………………………………………………………………….

MEM_NO NOT NULL NUMBER(5)

STUD_NO NUMBER(5)

SQL>insert into membership values(&mem_no,&stud_no);

Enter value for mem_no:5440

Enter value for stud_no:510

old 1:insert into membership values(&mem_no,&stud_no)

new 1:insert into membership values(5440,510)

insert into membership values(5440,510)

*

Page 10: dbms file

9

Errors Observed:

ERROR at line 1:

ORA-02291:integrity constraint(HARISH.SYS_C002724)violated-primary key not

found

SQL>select * from membership;

MEM_NO STUD_NO

………………………………………………………………………..

5440 513

5441 508 5442 518 5443 534

5444 524

SQL>create table book(book_no number(5) primary key,book_name

varchar2(20),author varchar2(2));

SQL>desc book;

Name Null? Type

………………………………………………………………………………………..

BOOK_NO NOT NULL NUMBER(5)

BOOK_NAME VARCHAR2(20)

AUTHOR VARCHAR2(20)

SQL>insert into book values(&book_no,’&book_name’,’&author’);

SQL>select * from book;

BOOK_NO BOOK_NAME AUTHOR

………………………………………………………………………………………………..

9123 DBMS Rama Krishna

2342 JAVA Robett wilkins

4523 Fearless tales Alfred

8723 my ambition Harish

7821 Harry Potter JK Rowling

SQL>create table lss_rec(iss_no number primary key,iss_date date,mem_no

number(5) references membership(mem_no),book_no number(5) references

book(book_no));

Page 11: dbms file

10

SQL>desc iss_rec;

Name Null? Type

………………………………………………………………………………………………………

ISS_NO NOT NULL NUMBER

ISS_DATE DATE

MEM_NO NUMBER(5)

BOOK_NO NUMBER(5)

SQL>select * from iss_rec;

ISS_NO ISS_DATE MEM_NO BOOK_NO

…………………………………………………………………………………………………

43 05-JAN-06 5443 4523

81 28-DEC-05 5441 8723

22 08-DEC-05 5440 7821

53 07-JAN-06 5442 9123

35 06-JAN-06 5444 2342

c) List all the student names with their membership numbers

SQL> select s.studname, m.memno from student s, membership m where

m.studno=s.studno;

STUDNAME MEMNO

------------- --------

abhijeet 1001

arun 1002

arvind 1003

ashish 1004

ashwin 1005

d) List all the issues for the current date with student and Book names

SQL> select i.issno, s.studname, b.bookname from iss_rec I, membership m,

student s, book b

2 where i.memno=m.memno and m.studno=s.studno and

i.issdate=to_char(sysdate);

ISSNO STUDNAME BOOKNAME

------- ----------------------------------

13 arvind P&S

Page 12: dbms file

11

e) List the details of students who borrowed book whose author is CJDATE

SQL> select * from student where studno in(select studno from membership

where memno in

2 (select memno from iss_rec where bookno in(select bookno from book where

author=’CJDATE’)));

STUDNO STUDNAME

---------- -------------

505 ashwin

f) Give a count of how many books have been bought by each student

SQL> select s.studno, count(i.bookno) from student s.membership m, book b, 2

iss_rec I where s.studno=m.studno and b.bookno=i.bookno group by s.studno;

STUDNO COUNT(I.BOOKNO)

---------- -----------------------

501 5

502 5

503 5

504 5

505 5

g) Give a list of books taken by student with stud_no as 5

SQL> select bookname from book where bookno in (select bookno from iss_rec

where

2 memno in(select memno from membership where

3 studno in(select studno from student where studno=5)));

BOOKNAME

-------------

NT

h) List the book details which are issued as of today

SQL> delete from book where bookno in(select bookno from iss_rec where

issdate=to_char(sysdate));

delete from book where bookno in (select bookno from iss_rec where

issdate=to_char(sysdate))

Page 13: dbms file

12

3. Database Schema for a Employee-pay scenario

employee(emp_id : integer, emp_name: string)

department(dept_id: integer, dept_name:string)

paydetails(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—

a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables

c) List the employee details department wise d) List all the employee names who joined after particular date

e) List the details of employees whose basic salary is between 10,000 and 20,000

f) Give a count of how many employees are working in each department

g) Give a names of the employees whose netsalary>10,000 h) List the details for an employee_id=5

Page 14: dbms file

13

AIM: Create the tables with the appropriate integrity constraints

Insert around 10 records in each of the tables

Create table employee(emp_id number(5) primary key,emp_name

varchar2(25));

SQL>desc employee;

Name Null? Type

………………………………………………………………………………………..

EMP_ID NOT NULL NUMBER(5)

EMP_NAME VARCHAR2(25)

Valid Test Data:

SQL>insert into employee values(&emp_id,’&emp_name’);

SQL>select * from employee;

EMP_ID EMP_NAME

………………………………………………………….

10 Robert

21 Coulthard

30 Fernando Alonso

39 Kartikeyan

87 Kimmi

SQL>create table department(dept_id number(5) primary key,dept_name

varchar2(20));

SQL>desc department;

Name Null? Type

………………………………………………………………………………………..

DEPT_ID NOT NULL NUMBER(5)

DEPT_NAME VARCHAR2(20)

SQL>insert into department values(&dept_id,’&dept_name’);

SQL>select * from department;

DEPT_ID DEPT_NAME

……………………………………………………………………………..

100 sales 101 accounts 102 administration

103 production 104 supervisor

Page 15: dbms file

14

SQL>create table paydetails(emp_id number(5) references

employee(emp_id),dept_id number(5) reerences department(dept_id),basic

number(7,2),deductions number(5,2),additions number(5,2),doj date);

SQL>desc paydetails;

Name Null? Type

………………………………………………………………………………………..

EMP_ID NUMBER(5)

DEPT_ID NUMBER(5)

BASIC NUMBER(7,2)

DEDUCTIONS NUMBER(5,2)

ADDITIONS NUMBER(5,2)

DOJ DATE

Different Data Sets:

SQL>insert into paydeatils values(&emp_id,&dept_id,

&basic,&deductions,&additions,&doj);

SQL>select * from paydeatils;

EMP_ID DEPT_ID BASIC DEDUCTIONS ADDITIONS DOJ

…………………………………………………………………………………………………………………..

10 101 25023.12 43.09 71.23 08-JAN-93

21 100 10500.29 23.98 40.9 01-JAN-06

30 102 6500.5 30.54 15 06-JUL-97

39 103 9700.45 32.78 65.09 08-AUG-03

87 104 15000 97.66 154.8 24-SEP-04

SQL>create table payroll(emp_id number(5)references

employee(emp_id),pay_date date);

SQL>desc payroll;

Name Null? Type

………………………………………………………………………………………..

EMP_ID NUMBER(5)

PAY_DATE DATE

SQL>insert into payroll values(&emp_id,’&date’);

SQL>select * from payroll;

Page 16: dbms file

15

EMP_ID PAY_DATE

………………………………………………………….

10 31-JAN-06

21 03-FEB-06

30 15-JAN-06

39 27-JAN-06

87 04-FEB-06

c) List the employee details department wise

SQL>select empid,deptid from paydet;

EMPID DEPTID

…………………………

401 500 402 200

403 600 404 400 405 1200

d) List all the employee names who joined after particular date

SQL>select e,empname from employee e,paydet p where e.empid=p.empid and

p.doj>=’05-mar-06’;

EMPNAME

…………………

AVINASH

NITIN

PHALGUN

e) List the details of employees whose basic salary is between 10,000 and

20,000

sqL> Select empid,empname from employee where salary between 10000 and

20000;

EMPID EMPNAME

…………………………….

402 AKHILA 403 aaaaaaaa

EMPID EMPNAME

…………………………….

AKHILA

Page 17: dbms file

16

f) Give a count of how many employees are working in each department

SQL>select count(empid),deptid from paydet group by deptid;

COUNT (EMPID) DEPTID

………………………………………………………

1 200

1 400

1 500

1 600

1 1200

g) Give a names of the employees whose netsalary>10,000

SQL> select empname from employee where empid in(select empid from paydet

where basic-deduction>10000);

EMPNAME

………………

AVINASH

AKHILA

HARISH

NITIN

PHALGUN

h) List the details for an employee_id=5

SQL> select * from employee where empid=5;

EMPID EMPNAME

------------------------------------------

5 Coulthard

Page 18: dbms file

17

4. Database Schema for a Video Library scenario

Customer(cust_no: integer,cust_name: string)

Membership(Mem_no: integer, cust_no: integer)

Cassette(cass_no:integer, cass_name:string, Language: String)

Iss_rec(iss_no: integer, iss_date: date, mem_no: integer, cass_no:

integer)

For the above schema, perform the following—

a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables

c) List all the customer names with their membership numbers

d) List all the issues for the current date with the customer names and cassette names

Page 19: dbms file

18

AIM: Create the tables with the appropriate integrity constraints

Insert around 10 records in each of the tables

SQL>create table customer(cust_no number(5) primary key,cust_name

varchar2(20));

SQL>desc customer;

Name Null? Type

……………………………………………………………………………………………………………..

CUST_NO NOT NULL NUMBER(5)

CUST_NAME VARCHAR2(20)

Valid Test Data:

SQL>insert into customer values(&cust_no,’&cust_name’);

SQL>select * from customer;

CUST_NO CUST_NAME

……………………………………………………………….

50 scott 51 pandey 52 varshney

53 naidu 54 bhimbra

SQL>create table membership(mem_no number(5) primary key,cust_no

number(5) references customer(cust_no));

SQL>dsec membership;

Name Null? Type

………………………………………………………………………………………………………...

MEM_NO NOT NULL NUMBER(5)

CUST_NO NUMBER(5)

SQL>insert into memship values(&mem_no,&cust_no);

SQL>select * from memship;

Page 20: dbms file

19

MEM_NO CUST_NO

…………………………………………………

920 50

981 51

897 52

820 53

928 54

SQL>create table cassette(cass_no number(5) primary key,

Cass_name varchar2(15),language varchar2(15));

SQL>desc cassette;

Name Null? Type

………………………………………………………………………………………..

CASS_NO NOT NULL NUMBER(5)

CASS_NAME VARCHAR2(15)

LANGUAGE VARCHAR2(15)

SQL>insert into cassette values(&cass_no,’&cass_name’,’&language’);

SQL>select * from cassette;

CASS_NO CASS_NAME LANGUAGE

………………………………………………………………………………………

1 tagore telugu

2 the lion king English

3 anniyan tamil

4 indra telugu

5 lord of rings English

SQL>create table issu_rec(iss_no number(5) primary key,iss_date date,mem_no

number(5)references memship(mem_no),cass_no number(5) references

cassette(cass_no));

SQL>desc issu_rec;

Name Null? Type

………………………………………………………………………………………………………...

ISS_NO NOT NULL NUMBER(5)

ISS_DATE DATE

MEM_NO NUMBER(5)

CASS_NO NUMBER(5)

Page 21: dbms file

20

SQL>select * from issu_rec;

ISS_NO ISS_DATE MEM_NO CASS_NO

……………………………………………………………………………………

22 07-JAN-06 920 1

23 10-JAN-00 981 2

26 10-JAN-06 897 5

3 01-JAN-06 820 4

34 31-DEC-05 928 3

c) List all the customer names with their membership numbers

SQL>select c.custname,m.memno from customer1 c,membership1 m where

c.custno=m.custno;

CUSTNAME MEMNO

……………….. ………………..

NIKHIL 51

VIVEK 52

SHRAVAN 58

VAMSI 57

SHIVA 56

d) List all the issues for the current date with the customer names and cassette names

SQL>select i.issno,c.custname,cc.cassettename from customer1

c,membership1 m,cassette cc,issrec1 I where i.issdate=to_char(sysdate)

and c.custno=m.custno and i.cassno=cc.cassno and i.memno=m.memno;

OutPut:

no rows selected.

Page 22: dbms file

21

5.Database Schema for a student-Lab scenario

Student(stud_no: integer, stud_name: string, class: string)

Class(class: string, descrip: 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 b) Insert around 10 records in each of the tables

c) List all the machine allotments with the student names, lab and machine numbers

d) List the total number of lab allotments day wise

e) Give a count of how many machines have been allocated to the ‘CSIT’ class

f) Give a machine allotment etails of the stud_no 5 with his personal and class details

g) Count for how many machines have been allocatedin Lab_no 1 for the day of the week as “Monday”

h) How many students class wise have allocated machines in the labs

Page 23: dbms file

22

AIM: Create the tables with the appropriate integrity constraints

Insert around 10 records in each of the tables

SQL>create table stu(stud_no number(5) primary key,stud_nam

varchar2(20),class varchar2(20));

SQL> desc stu;

Name null? Type

STUD_NO NOT NULL NUMBER(5)

STUD_NAM VARCHAR2(20)

CLASS VARCHAR2(20)

Valid Data Sets:

SQL> insert into stu values(&stud_no,’&stud_nam’,’&class’);

SQL> select * from stu;

STUD_NO STUD_NAM CLASS

39 LEON CSE

34 VIKAS CSIT

18 MATHEW ECE

8 HANSEN MECH

24 ALEXIS EEE

SQL> Create table class (class varchar2(20), descript varchar2(10));

SQL> Describe class;

Name null type

CLASS VARCHAR2(10)

DESCRIPT VARCHAR2(20)

SQL> create table lab(match_no number(5), lab_no number(5), description

varchar2(20));

SQL> desc lab;

Name null type

MACH_NO NOT NULL NUMBER(5)

LAB_NO NUMBER(5)

DESCRIPTION VARCHAR2(20)

Page 24: dbms file

23

SQL> insert into lab values(&mach_no,&lab_no,’&description’);

SQL> select * from lab;

MATCH_NO LAB_NO DESCRIPTION

--------------- --------- --------------------

23 7 physics

78 2 chemistry

87 1 edc

12 10 cds

8 3 java lab

SQL> create table allotment(stud_no number(5) references stu(stud_no),

match_no number(5) references lab(mach_no),

Doweek varchar2(20));

SQL> desc allotment;

Name Null? Type

-------------- ------- ------------------------

STUD_NO NUMBER(5)

MACH_NO NUMBER(5)

DOWEEK VARCHAR2(20)

SQL>select * from allotment;

STUD_NO MACH_NO DOWEEK

------------- -- ------------ ------------

39 23 sat

34 87 mon

18 78 tue

8 12 wed

24 12 thu

Page 25: dbms file

24

c) List all the machine allotments with the student names, lab and machine numbers

SQL>select s.studname,l.machno from student1 s,lab l,allotment a where

a.machno=l.machno and a.studno=s.studno;

STUDNAME MACHNO

………………………………………..

ABHIJEET 1

KALYAN 22

ASHWIN 3

ARKA 4

ARVIND 5

d) List the total number of lab allotments day wise

SQL>select l.machno,l.descrip,a.day from lab l,allotment a where

a.machno=l.machno;

MACHNO DESCRIP DAY

……………………………………………………………………

1 UNIX MONDAY

22 UNIX TUESDAY

3 XP WEDNESDAY

4 WINDOWS THRUSDAY

5 ME FRIDAY

e) Give a count of how many machines have been allocated to the ‘CSIT’ class

SQL>select count(machno)from allotment where studno in(select studno

from student1 where class=’CSIT’);

COUNT (MACHNO)

……………………..

1

6

Page 26: dbms file

25

f) Give a machine allotment etails of the stud_no 5 with his personal and class details

SQL>select a.studno,a.machno,s.studname,s.class from allotment a,student1

s where a.studno=s.studno and a.studno=503;

STUDNO MACHNO STUDNAME CLASS

………………………………………………………………………………………………………

503 5 ARVIND CSE

g) Count for how many machines have been allocatedin Lab_no 1 for the day of the week as “Monday”

h) How many students class wise have allocated machines in the labs

SQL>select count(studno) “allocated students in the labs”,class from

student1 where studno in(select studno from allotment) group by class;

allocated students in the lab CLASS

……………………………………………………………………………

2 CSE

1 ECE

1 EEE

1 IT

Page 27: dbms file

26

6) Create a Cursor which update the salaries of an

Employee as follows. 1. if sal<1000then update the salary to 1500. 2. if sal>=1000 and <2000 then update the salary to 2500.

3. if sal>=2000 and <=3000 then update the salary to 4000. And also count the no.of records have been updated.*/

Code:

Declare

Cursor my_cur is select empno,sal from emp;

Xno emp.empno%type; Xsal emp.sal%type;

C number; Begin

Open my_cur; C:=0;

Loop Fetch my_cur into xno,xsal;

If(xsal<1000) then Update emp set sal=3000 where empno=xno;

C:=c+1; Else if(xsal>=2000) and xsa<3000) then

Update emp set sal=4000 where empno=xno; C:=c+1;

End if;

End if; Exit when my_cur%NOTFOUND ;

End loop; Close my_cur;

Dbma_output.put_line(c||’records have been successfully updated’); End;

Sql>@a.sql; records have been successfully updated

pl/sql procedure successfully completed.

Valid Test Data Before executing the cursor, the records in emp table as follows

Sql>select * from emp;

Page 28: dbms file

27

OUTPUT:

EMPNO ENAME JOB MGR HIREDATE SAL COMMD EPTNO -----------------------------------------------------------------

7369 SMITH CLERK 7902 17-DEC-80 2000 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

-------- ---------- --------- ---------- --------- ---------- ---------------------- 7566 JONES MANAGER 7839 02-APR-81 2975 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

… ….

14 rows selected.

Page 29: dbms file

28

7) Create a procedure which updates the salaries of

an employees as follows. 1.if sal<1000 then update the salry to 1500.

2.if sal>=1000 and <=2400 then update the salary to 2500.*/

Create or replace procedure myproc as

Cursor my_cur is select empno,sal from emp; Xno emp.empno%type;

Xsal emp.sal%type; C number;

Begin Open my_cur;

C:=0; Loop

Fetch my_cur into xno,xsal; If(xsal<1000) then

Update emp set sal=1500 where empno=xno; C:=c+1;

Else

Is(xsal>=1000 and xsal<=2400) then Update emp set sal=2500 where empno=xno;

C:=c+1; End if;

End if; Exit when my_cur%NOTFOUND;

End loop; Close my_cur;

Dbms_output.put_line(c||’records have been successfully updated’); End;

/

Valid Test Data:

Procedure created.

Sql>exec myproc;

Page 30: dbms file

29

OUTPUT:

Records have been successfully completed.

/* create function which add two given numbers. (Simple programs)

*/ Create or replace function add_fun(a number,b number) return

Number as C number;

Begin C:=a+b;

Return c; End;

/ Function created.

/*add_fun specification*/ Declare

Result number; Begin

Result:=add_fun(10,20); Dbms_output.put_line(‘the sum of 10 and 20 is’||result);

End; Sql>/

The sum of 10 and 20 is 30 Pl/sql procedure successfully completed.

/*create a function which count total no.of employees having salary

less than 6000.*/ /*function body*/

Create or replace function count_emp(esal number)return number as

Cursor vin_cur as Select empno,sal from emp; Xno emp.empno%type;

Xsal emp.sal%type; C number;

Begin Open vin_cur;

C:=0; loop

fetch vin_cur into xno,xsal; if(xsal<esal) then

c:=c+1; end if;

exit when vin_cur%notfound; end loop;

close vin_cur;

return c;

Page 31: dbms file

30

end;

/ Function created.

/*function specification*/ Declare

Ne number; Xsal number;

Begin Ne:=count_emp(xsal);

Dbms_output.put_line(xsal); Dbma_output.put_line(‘there are ‘||ne||;employees’);

End; /

OUTPUT

There are 8 employees.


Recommended