DBMS Part B
DBMS Part B
1. Create the following tables by identifying primary and foreign keys. Specify the not null
property for mandatory keys.
SUPPLIERS (SUPPLIER_NO, SNAME, SADDRESS, SCITY)
COMPUTER_ITEMS (ITEM_NO, SUPPLIER_NO, ITEM_NAME, IQUANTITY)
Consider three suppliers. A supplier can supply more than one type of items.
Write the SQL queries for the following:
a) List ITEM and SUPPLIER details in alphabetical order of city name and in each city decreasing
order of IQUANTITY.
b) List the name and address, city of the suppliers who are supplying keyboard.
c) List the supplier name, items supplied by the suppliers ‘Cats’ and ‘Electrotech’.
d) Find the items having quantity less than five and insert the details of supplier and items of these
into another table NEWORDER.
2 (
7 );
Table created.
Page 35
CLASS: II BCA I BATCH PART-B ORACLE LAB
METHOD 1:
1 row created.
METHOD2:
1 row created.
8 rows selected.
2 (
3 I_NO VARCHAR2(5),
7 );
Table created.
Name
-----------------------------------------------------
I_NO
S_NO
I_NAME
I_QTY
Page 37
CLASS: II BCA I BATCH PART-B ORACLE LAB
INSERTING RECORDS:
METHOD1:
1 row created.
METHOD2:
8 rows selected.
Page 38
CLASS: II BCA I BATCH PART-B ORACLE LAB
QUERIES:
a) List ITEM and SUPPLIER details in alphabetical order of city name and in each city
decreasing order of IQUANTITY.
8 rows selected.
b) List the name and address, city of the suppliers who are supplying keyboard.
Page 39
CLASS: II BCA I BATCH PART-B ORACLE LAB
c) List the supplier name, items supplied by the suppliers ‘Cats’ and ‘Electrotech’.
S_NAME I_NAME
-------------------- ---------------
ELECTROTECH KEYBOARD
CATS KEYBOARD
ELECTROTECH CPU
CATS MOUSE
CATS KEYBOARD
ELECTROTECH MOUSE
6 rows selected.
d) Find the items having quantity less than five and insert the details of supplier and items of these
into another table NEWORDER.
Table created.
Page 40
CLASS: II BCA I BATCH PART-B ORACLE LAB
2. Create the following tables by identifying primary and foreign keys. Specify the not null
property for mandatory keys.
EMPLOYEE_MASTER (EMP_ID, EMP_NAME, EMAIL_ID , EMP_ADDRS, PHONE)
ATTENDANCE (EMP_ID, MONTH, WOM, MHRS, THRS, WHRS, TRHRS, FHRS, SHRS,
SUHRS). (Valid values for WOM<=5, MONTH can be 1-12). Apply appropriate constraints.
Consider 3 employees. And attendance records for at least two months.
2 (
8 );
Table created.
Page 41
CLASS: II BCA I BATCH PART-B ORACLE LAB
INSERTING RECORDS:
1 row created.
VIEWING RECORDS:
2 (
13 );
Table created.
INSERTING RECORD:
1 row created.
VIEWING RECORDS:
EMP_I MONTH WOM MHRS THRS WHRS TRHRS FHRS SHRS SUHRS
----- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------------------------------------
E1001 5 2 3 3 2 2 1 2 3
E1001 5 4 2 3 6 2 1 5 4
E1002 9 4 2 4 1 7 3 8 4
E1002 9 2 1 3 4 5 6 7 0
E1003 8 2 3 4 5 6 2 1 9
E1005 12 2 3 4 5 6 7 0 4
E1003 6 1 0 7 5 0 3 4 0
E1005 9 2 0 0 0 0 0 0 0
8 rows selected.
Page 43
CLASS: II BCA I BATCH PART-B ORACLE LAB
QURIES:
a) Display EMP_ID, EMP_NAME and EMAIL_ID of all employees who are working on every
Sunday of 2nd or 4th week in a month.
b) Display total hours worked by each employee in each month with EMP_ID.
SQL> SELECT
EMPLOYEE_MASTER.EMP_ID,MONTH,SUM(MHRS+THRS+WHRS+TRHRS+FHRS+SHRS+SU
HRS)"TOTAL HOURS" FROM EMPLOYEE_MASTER,ATTENDANCE WHERE
EMPLOYEE_MASTER.EMP_ID=ATTENDANCE.EMP_ID GROUP BY
EMPLOYEE_MASTER.EMP_ID,MONTH;
E1005 12 29
E1005 9 0
E1002 9 55
E1003 8 30
E1001 5 39
E1003 6 19
6 rows selected.
Page 44
CLASS: II BCA I BATCH PART-B ORACLE LAB
c) Display the names of the employees who never attended the duty so far (attendances not given
so far).
SQL> SELECT EMP_NAME FROM EMPLOYEE_MASTER WHERE EMP_ID NOT IN(SELECT
EMP_ID FROM ATTENDANCE);
EMP_NAME
----------
CHAITHRA
d) Display the employee name, month, week, total hours worked for employees who have total
number of hours more than 20 hours a week
SQL> SELECT
EMP_NAME,MONTH,WOM,SUM(MHRS+THRS+WHRS+TRHRS+FHRS+SHRS+SUHRS)"TOTA
L HOURS" FROM EMPLOYEE_MASTER,ATTENDANCE WHERE
EMPLOYEE_MASTER.EMP_ID=ATTENDANCE.EMP_ID GROUP BY
EMP_NAME,MONTH,WOM HAVING
SUM(MHRS+THRS+WHRS+TRHRS+FHRS+SHRS+SUHRS)>20;
NAGESH 5 4 23
PRANITHA 9 2 26
SUNIL 12 2 29
PAVITHRA 8 2 30
PRANITHA 9 4 29
Page 45
CLASS: II BCA I BATCH PART-B ORACLE LAB
3. Create the following tables by identifying primary and foreign keys, specify the not null property
for mandatory keys.
2 (
8 );
Table created.
Page 46
CLASS: II BCA I BATCH PART-B ORACLE LAB
DESCRIBING STRUCTURE:
INSERTING RECORDS:
1 row created.
VIEWING RECORDS:
Page 47
CLASS: II BCA I BATCH PART-B ORACLE LAB
2 (
6 );
Table created.
P_NO VARCHAR2(5)
RECORD INSERTION:
1 row created.
VIEWING RECORDS:
CU P_NO QTY_SOLD
-- ----- ---------------------------
C1 POOO3 2
C2 POOO2 4
Page 48
CLASS: II BCA I BATCH PART-B ORACLE LAB
C3 POOO2 10
C4 POOO1 3
C1 POOO4 2
C2 POOO3 2
C4 POOO4 1
7 rows selected.
CUST_NO TOTAL_AMOUNT
-- -- ---------------
C2 2620
b) Display the names of product for which either QtyAvailable is less than 30 or total QtySold is
less than 5 (Use UNION).
UNION
P_NAME
----------
CD DRIVE
KEYBOARD
MONITOR
Page 49
CLASS: II BCA I BATCH PART-B ORACLE LAB
P_NAME QTY_PURCHASE
--------- - ------------
MONITOR 3
KEYBOARD 1
TOTAL_PROFIT
------------
120.6
TOTAL PENDRIVES
---------------
14
Page 50
CLASS: II BCA I BATCH PART-B ORACLE LAB
4. Create table STUDENT_PROFILE that includes Rollno, name, class, ECCC (Extra/Cocurricular
he belongs to such as SPORTS, NSS, etc.) and another table MARKS_REPORT that includes
Rollno, Internal_Test, Marks1, Marks2, Marks3 and ECCC_marks.
Constraints
Internal_Test can be either 1 or 2.
Each mark can be 0 – 100. Absence in the test can be entered as -1.
Consider at least 3 classes.
Apply suitable datatype and constraints to each column.
Insert 5 students marks report in the both the tests.
Write the SQL queries for the following:
a) Find number of students failed class-wise.
b) Display the complete details of the students secured distinction (Percentage>=70) in I BCA
c) Display class and highest total marks in second internals in each class.
d) Display the student name with rollno and class of those who passed in I internals and
failed in II internals (use SET operator).
2 (
7 );
Table created.
INSERTING RECORDS:
VALUES(1001,'ANANYA','IBCA','NSS');
1 row created.
VIEWING RECORDS:
2 (
Page 52
CLASS: II BCA I BATCH PART-B ORACLE LAB
9 );
Table created.
ROLLNO NUMBER(6)
INTERNAL NUMBER(1)
MARK_1 NUMBER(3)
MARK_2 NUMBER(3)
MARK_3 NUMBER(3)
ECCC_MARKS NUMBER(3)
INSERTING RECORDS:
2 VALUES(1005,2,32,28,21,20);
1 row created.
Page 53
CLASS: II BCA I BATCH PART-B ORACLE LAB
VIEWING RECORDS:
1005 2 32 28 21 20
1005 1 65 45 65 98
1001 1 87 98 90 57
1001 2 -1 -1 -1 -1
1003 1 34 33 36 76
1003 2 90 98 99 98
1004 1 98 99 90 89
1004 2 99 90 87 70
1002 1 67 66 34 33
1002 2 32 22 21 20
10 rows selected.
QURIES:
Page 54
CLASS: II BCA I BATCH PART-B ORACLE LAB
CLASS FAIL_COUNT
---------- ----------
IBA 1
IBCA 1
IBCOM 2
b. Display the complete details of the students secured distinction (Percentage>=70) in I BCA
c. Display class and highest total marks in second internals in each class.
---------- -------------
IBCA 346
IBA 385
Page 55
CLASS: II BCA I BATCH PART-B ORACLE LAB
d. Display the student name with rollno and class of those who passed in I internals and
failed in II internals (use SET operator).
INTERSECT
Page 56
CLASS: II BCA I BATCH PART-B ORACLE LAB
5. Write a PL/SQL program to compute the selling price of books depending on the book code and
category. Use Open, Fetch and Close. The Book_detail table contains columns Book Code, Author,
Title, Category and Price. Insert 10 records.
The selling price = Price – Discount.
The discount is calculated as follows:
TABLE CREATION:
2 (
8 );
Table created.
Page 57
CLASS: II BCA I BATCH PART-B ORACLE LAB
BCODE CHAR(1)
INSERTING RECORDS:
1 row created.
VIEWING RECORDS:
10 rows selected.
Page 58
CLASS: II BCA I BATCH PART-B ORACLE LAB
PL/SQL CODE:
SQL> DECLARE
CURSOR B_CUR IS
BC BOOK_DETAIL.BCODE%TYPE;
AUTH BOOK_DETAIL.AUTHOR%TYPE;
TT BOOK_DETAIL.TITLE%TYPE;
CT BOOK_DETAIL.CAT%TYPE;
PR BOOK_DETAIL.PRICE%TYPE;
DPER NUMBER(6,2);
DISCOUNT NUMBER(6,2);
SPRICE NUMBER(6,2);
BEGIN
DBMS_OUTPUT.PUT_LINE('__________________________________________________');
DBMS_OUTPUT.PUT_LINE('_______________________________________________');
OPEN B_CUR;
LOOP
DPER:=10;
DPER:=12.5;
DPER:=18;
DPER:=19;
DPER:=25;
DPER:=24;
ELSE
DPER:=28;
END IF;
DISCOUNT:=(PR*DPER)/100;
SPRICE:=PR-DISCOUNT;
END LOOP;
DBMS_OUTPUT.PUT_LINE('________________________________________________________');
CLOSE B_CUR;
END;
Page 60
CLASS: II BCA I BATCH PART-B ORACLE LAB
OUTPUT:
___________________________________________________________________________________________________________________
_____________________________________________________________________________________________________________________
________________________________________________________________________________________________________
Page 61
CLASS: II BCA I BATCH PART-B ORACLE LAB
6. Write a PL/SQL program to display employee pay bill (using Cursor For loop). Use a procedure to
receive basic pay and to compute DA, HRA, Tax, PF, Gross Pay and Net pay
(Use OUT). Base table contains the columns empnum, empname, basic pay. Insert 3 records.
Allowances are computed as follows:
TABLE CREATION:
2 (
6 );
Table created.
Page 62
CLASS: II BCA I BATCH PART-B ORACLE LAB
INSERTING RECORDS:
1 row created.
VIEWING RECORDS:
PROCEDURE CODE:
BEGIN
IF BASIC<=20000 THEN
DA:=0.35*BASIC;
Page 63
CLASS: II BCA I BATCH PART-B ORACLE LAB
HRA:=0.08*BASIC;
DA:=0.38*BASIC;
HRA:=0.09*BASIC;
DA:=0.4*BASIC;
HRA:=0.1*BASIC;
ELSE
DA:=0.45*BASIC;
HRA:=0.1*BASIC;
END IF;
GROSS:=BASIC+DA+HRA;
PF:=LEAST(0.12*GROSS,20000);
IF GROSS<25000 THEN
PT:=100;
ELSE
PT:=200;
END IF;
NET:=GROSS-(PF+PT);
END;
Procedure created.
Page 64
CLASS: II BCA I BATCH PART-B ORACLE LAB
PL/SQL CODE:
SQL> DECLARE
DA NUMBER(10);
HRA NUMBER(10);
TAX NUMBER(10);
PF NUMBER(10);
PT NUMBER(10);
GROSS NUMBER(10);
NET NUMBER(10);
BEGIN
LOOP
PROC(EMP.BASIC,DA,HRA,TAX,PF,PT,GROSS,NET);
DBMS_OUTPUT.PUT_LINE('===================PAYSLIP====================');
DBMS_OUTPUT.PUT_LINE('EMPNO:'||EMP.ENO||'EMPNAME:'||EMP.ENAME);
DBMS_OUTPUT.PUT_LINE('BASICPAY:'||EMP.BASIC||'PF:'||PF);
DBMS_OUTPUT.PUT_LINE('DA:'||DA||'PT:'||PT);
DBMS_OUTPUT.PUT_LINE('HRA:'||HRA);
DBMS_OUTPUT.PUT_LINE('GROSS:'||GROSS||'NETPAY'||NET);
DBMS_OUTPUT.PUT_LINE('**********************************************');
END LOOP;
END;
/
Page 65
CLASS: II BCA I BATCH PART-B ORACLE LAB
OUTPUT:
===================PAYSLIP====================
EMPNO:E1001 EMPNAME:ANANYA
BASICPAY:5000 PF:858
DA:1750 PT:100
HRA:400
GROSS:7150 NETPAY6192
**********************************************
===================PAYSLIP====================
EMPNO:E1002 EMPNAME:ANVITHA
BASICPAY:30000 PF:5292
DA:11400 PT:200
HRA:2700
GROSS:44100 NETPAY38608
**********************************************
===================PAYSLIP====================
EMPNO:E1003EMP NAME:PAVITHRA
BASICPAY:25000 PF:4410
DA:9500 PT:200
HRA:2250
GROSS:36750 NETPAY32140
**********************************************
Page 66
CLASS: II BCA I BATCH PART-B ORACLE LAB
7. Given the following tables: ITEM_MASTER (itemno, name, stock, unit_price) [Apply the
Primary key and check constraint for stock and price as >0) [Insert 5 records]
ITEM_TRANS (itemno, quantity and trans_date)
2 (
7 );
Table created.
DESCRIBING STRUCTURE:
Page 67
CLASS: II BCA I BATCH PART-B ORACLE LAB
RECORD INSERTION:
1 row created.
VIEWING RECORDS:
2 (
Page 68
CLASS: II BCA I BATCH PART-B ORACLE LAB
5 TDATE DATE
6 );
Table created.
DESCRIBING STRUCTURE:
ITNO NUMBER(4)
TDATE DATE
END PCK_ITEM;
Package created.
INAME VARCHAR2(20);
BEGIN
Page 69
CLASS: II BCA I BATCH PART-B ORACLE LAB
RETURN 1;
EXCEPTION
RETURN 0;
END;
VAL NUMBER(1);
ASTOCK NUMBER(4);
BEGIN
VAL:=PCK_ITEM.ITEM_CHECK(ITMNO);
IF VAL=1 THEN
IF ASTOCK>=QTY THEN
ELSE
END IF;
END IF;
END;
END PCK_ITEM;
Page 70
CLASS: II BCA I BATCH PART-B ORACLE LAB
PL/SQL CODE:
SQL> DECLARE
ITEMNO NUMBER(4);
QTY NUMBER(4);
BEGIN
ITEMNO:=&ITEMNO;
QTY:=&QUANTITY;
PCK_ITEM.PROC_ITEM(ITEMNO,QTY);
END;
OUTPUT1:
old 5: ITEMNO:=&ITEMNO;
new 5: ITEMNO:=1000;
old 6: QTY:=&QUANTITY;
new 6: QTY:=100;
OUTPUT2:
old 5: ITEMNO:=&ITEMNO;
new 5: ITEMNO:=1002;
Page 71
CLASS: II BCA I BATCH PART-B ORACLE LAB
old 6: QTY:=&QUANTITY;
new 6: QTY:=300;
OUTPUT3:
old 5: ITEMNO:=&ITEMNO;
new 5: ITEMNO:=1001;
old 6: QTY:=&QUANTITY;
new 6: QTY:=100;
Page 72