BSC 3 Practicals

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

-11. CREATING TABLE WITH CONSTRAINTS Problem Def.

: Creating the table CLIENT_MASTER for the following constraints i. ii. iii. iv. A CHECK constraint on the client no., so that client no. value must start with C Client no. value must not be null or repeated A CHECK constraint on name so that name is entered in upper case A CHECK constraint on city so that only the cities BOMBAY, NEW DELHI, MADRAS, CALCUTTA allowed v. Also include the following columns with appropriate data types and constraints Address State Balance Creation Procedure: 1. Enter into SQL by giving username and password 2. At SQL prompt type the CREATE TABLE command as follows
CREATE TABLE CLIENT_MASTER ( CLNO VARCHAR2(6) PRIMARY KEY CHECK (CLNO LIKE C%), CLNAME VARCHAR2(30) NOT NULL CHECK (CLNAME=UPPER(CLNAME)), CITY VARCHAR2(10) CHECK (CITY IN (BOMBAY,NEW DELHI,MADRAS,CALCUTTA)), ADDRESS VARCHAR2(50) NOT NULL, STATE VARCHAR2(15) NOT NULL, BALANCE NUMBER(10,2) );

3. Enter the data into the table using INSERT command as follows:
INSERT INTO CLIENT_MASTER VALUES(&CLNO,&CLNAME,&CITY,&ADDRESS,&STATE,&BALANCE);

4. While inserting, enter values that match with constraints 5. Repeat the INSERT command to insert more rows 6. To view the inserted rows, enter a query as follows: SELECT * FROM CLIENT_MASTER; 7. To view only client no, client name, city and balance column values, enter query as follows: SELECT CLNO, CLNAME, CITY, BALANCE FROM CLIENT_MASTER; 8. To view the rows in sorted order based on client no., enter the query as follows: SELECT * FROM CLIENT_MASTER ORDER BY CLNO; 9. To list the details of the client who has highest balance, enter the query as follows:
SELECT * FROM CLIENT_MASTER WHERE BALANCE=(SELECT MAX(BALANCE) FROM CLIENT_MASTER);

-22. CREATING A VIEW Problem Def: Creating a view to get employee name and his manager name from the table EMP. The table contains the columns as given below: EMPNO EMPNAME JOINDATE MGR SALARY CHAR(6) VARCHAR2(15) DATE CHAR(6) NUMBER(8,2)

Foreign key is MGR and Parent key is EMP(EMPNO) Creation Procedure: 1. Enter into SQL by giving username and password 2. At SQL prompt, create EMP table as follows: CREATE TABLE EMP ( EMPNO CHAR(6) PRIMARY KEY, EMPNAME VARCHAR2(15) NOT NULL, JOINDATE DATE, MGR CHAR(6) REFERENCES EMP(EMPNO), SALARY NUMBER(8,2) ); 3. Enter the data into the table using INSERT command as follows:
INSERT INTO EMP VALUES(&EMPNO,&ENAME,&JDT,&MGR,&SALARY);

4. While inserting, enter values that match with constraints 5. Repeat the INSERT command to insert more rows 6. Now create a view as follows: CREATE OR REPLACE VIEW EMPMGR AS SELECT A.EMPNO, A.EMPNAME, B.EMPNAME MGRNAME FROM EMP A, EMP B WHERE A.MGR=B.EMPNO; 7. To display the result, enter the SELECT command as follows: SELECT * FROM EMPMGR;

-33. REVERSE A NUMBER Problem Def: A PL/SQL program to reverse a number. Algorithm: Program: SET SERVEROUTPUT ON SET VERIFY OFF DECLARE NUM NUMBER(6):=# REV VARCHAR2(6); R NUMBER(1); BEGIN WHILE (NUM != 0) LOOP R := MOD(NUM,10); REV := REV || R; NUM := TRUNC(NUM/10); END LOOP; DBMS_OUTPUT.PUT_LINE(Reversed Number is ||REV); END; / Input: Output:

-44. FINDING STRONG NUMBER Problem Def: A PL/SQL program to find whether the given number is strong or not Algorithm: Program: SET SERVEROUTPUT ON SET VERIFY OFF DECLARE NUM NUMBER(5) := # S NUMBER := 0; R NUMBER(1); F NUMBER; TEMP NUMBER(5); BEGIN TEMP := NUM; WHILE (NUM != 0) LOOP F := 1; R := MOD(NUM,10); FOR I IN 1..R LOOP F := F * I; END LOOP; S := S + F; NUM := TRUNC(NUM/10); END LOOP; IF S=TEMP THEN DBMS_OUTPUT.PUT_LINE(TEMP|| is Strong Number); ELSE DBMS_OUTPUT.PUT_LINE(TEMP|| is not Strong Number); END IF; END; / Input/Output:

-55. SALESMAN COMMISSION Problem Def: A PL/SQL program to calculate the commission of salesmen. The base table is COM and the columns are SMCODE, SMNAME, AMOUNT and COMM. The commission is to be calculated as per the following rules: i. If amount is more than 10000 then commission is 20% on amount ii. If amount between 7500 and 10000 then commission is 15% on amount iii. If amount between 5000 and 7500 then commission is 10% on amount iv. Otherwise commission is 5% on amount Solution: 1. Create base table in SQL as follows: CREATE TABLE COM ( SMCODE VARCHAR2(6) PRIMARY KEY, SMNAME VARCHAR2(25) NOT NULL, AMOUNT NUMBER(8,2) CHECK (AMOUNT>0), COMM NUMBER(7,2) ); 2. Enter records using INSERT command as follows:
INSERT INTO COM(SMCODE,SMNAME,AMOUNT) VALUES(&SMCODE,&SMNAME,&AMOUNT);

3. Repeat the INSERT command to enter more records. 4. Now write the program and execute it. Program:
DECLARE CO COM.COMM%TYPE; AMT COM.AMOUNT%TYPE; BEGIN FOR SM IN (SELECT SMCODE,AMOUNT FROM COM) LOOP AMT := SM.AMOUNT; IF AMT >= 10000 THEN CO := AMT * 20/100; ELSIF AMT >= 7500 THEN CO := AMT * 15/100; ELSIF AMT >= 5000 THEN CO := AMT * 10/100; ELSE CO := AMT * 5/100; END IF; UPDATE COM SET COMM=CO WHERE SMCODE=SM.SMCODE; END LOOP; COMMIT; END; /

To view the output, enter SELECT command as follows at SQL prompt: SELECT * FROM COM ORDER BY SMCODE; OUTPUT:

-66. STUDENT RANK ALLOCATION Problem Def: A PL/SQL program to allocate rank for the students. The base table is having the columns HTNO, SUB1, SUB2, SUB2, TOTAL, RESULT and RANK. Solution: 1. Create a base table STURANK as follows in SQL CREATE TABLE STURANK ( HTNO NUMBER(8) PRIMARY KEY, SUB1 NUMBER(3) CHECK (SUB1 BETWEEN 0 AND 100), SUB2 NUMBER(3) CHECK (SUB2 BETWEEN 0 AND 100), SUB3 NUMBER(3) CHECK (SUB3 BETWEEN 0 AND 100), TOTAL NUMBER(3), RESULT VARCHAR2(4), RANK NUMBER(3) ); 2. Enter records using INSERT command as follows:
INSERT INTO STURANK(HTNO,SUB1,SUB2,SUB3) VALUES(&HTNO,&SUB1,&SUB2,&SUB3);

3. Repeat the INSERT command to enter more records. 4. Now write the program and execute it. Program:
DECLARE R STURANK.RANK%TYPE:=0; TOT STURANK.TOTAL%TYPE; RES STURANK.TOTAL%TYPE; BEGIN FOR STD IN (SELECT HTNO,SUB1,SUB2,SUB3 FROM STURANK) LOOP TOT := STD.SUB1+ :STD.SUB2 + :STD.SUB3; IF STD.SUB1<34 OR STD.SUB2<34 OR STD.SUB3<34 THEN RES := FAIL; ELSE RES := PASS; END IF; UPDATE STURANK SET TOTAL=TOT, RESULT=RES WHERE HTNO=STD.HTNO; END LOOP; COMMIT; TOT := 0; FOR STD IN (SELECT HTNO, TOTAL FROM STURANK WHERE RESULT=PASS ORDER BY TOTAL DESC) LOOP

-7IF STD.TOTAL != TOT THEN R := R + 1; TOT := STD.TOTAL; END IF; UPDATE STURANK SET RANK=R WHERE HTNO=STD.HTNO; TOT := STD.TOTAL; END LOOP; COMMIT; END; /

To view the output, enter SELECT command as follows at SQL prompt: SELECT * FROM STURANK ORDER BY RESULT DESC, TOTAL DESC; OUTPUT:

7. EMPLOYEE NET SALARY Problem Def: A PL/SQL program to calculate the HRA, Income Tax, Gross Salary and Net Salary. The base table is PAY and the columns are EMPNO, ENAME, GCODE, BASIC, DA, HRA, ITAX, GSAL, NETSAL. Solution: 1. Create base table in SQL as follows: CREATE TABLE PAY ( EMPNO NUMBER(4) PRIMARY KEY, ENAME VARCHAR2(25) NOT NULL, GCODE CHAR(1) CHECK (GCODE IN(A,B,C)), BASIC NUMBER(8,2) CHECK (BASIC>0), DA NUMBER(7,2) CHECK (DA>0), HRA NUMBER(7,2), ITAX NUMBER(7,2), GSAL NUMBER(7,2), NETSAL NUMBER(7,2) ); 2. Enter records using INSERT command as follows:
INSERT INTO PAY(EMPNO,ENAME,GCODE,BASIC,DA) VALUES(&EMPNO,&NAME,&GRADE,&BASIC,&DA);

-83. Repeat the INSERT command to enter more records. 4. Now write the program and execute it. Program:
DECLARE H PAY.HRA%TYPE; IT PAY.ITAX%TYPE; GS PAY.GSAL%TYPE; NS PAY.NETSAL%TYPE; BEGIN FOR TMP IN (SELECT EMPNO,GCODE,BASIC,DA FROM PAY) LOOP IF TMP.GCODE=A THEN H := TMP.BASIC * 30/100; IT := TMP.BASIC * 18/100; ELSIF TMP.GCODE=B THEN H := TMP.BASIC * 25/100; IT := TMP.BASIC * 15/100; ELSIF TMP.GCODE=C THEN H := TMP.BASIC * 20/100; IT := TMP.BASIC * 10/100; END IF; GS := TMP. BASIC + H + TMP.DA; NS := GS IT; UPDATE PAY SET HRA=H, GSAL=GS, NETSAL=NS WHERE EMPNO=TMP.EMPNO; END LOOP; COMMIT; END; /

To view the output, enter SELECT command as follows at SQL prompt: SELECT * FROM PAY ORDER BY EMPNO; OUTPUT:

-98. ELECTRICITY BILL Problem Def: A PL/SQL program to calculate the electrical charges as per the rates given below. The base table is EBILL and the columns are CNO, CNAME, CATE, PMR, CMR, RATE and TOTAMT. i. If Category is INDUSTRY then Rs.10/- per unit. ii. If Category is DOMESTIC then Rs.7.50/- per unit. iii. If Category is AGRI then Rs.5/- per unit. Solution: 5. Create base table in SQL as follows: CREATE TABLE EBILL ( CNO NUMBER(6) PRIMARY KEY, CNAME VARCHAR2(25) NOT NULL,
CATE VARCHA2(10) CHECK (CATE IN(INDUSTRY,DOMESTIC,AGRI)),

PMR NUMBER(6) CHECK (PMR>0), CMR NUMBER(6), RATE NUMBER(5,2), TOTAMT NUMBER(8,2), CHECK (CMR>=PMR) ); 6. Enter records using INSERT command as follows:
INSERT INTO EBILL(CNO,CNAME,CATE,PMR,CMR) VALUES(&CNO,&CNAME,&CATEGORY,&PMR,&CMR);

7. Repeat the INSERT command to enter more records. 8. Now write the program and execute it. Program:
DECLARE R EBILL.RATE%TYPE; TAMT EBILL.TOTAMT%TYPE; UNITS NUMBER(6); BEGIN FOR TMP IN (SELECT CNO,CATE,PMR,CMR FROM EBILL) LOOP UNITS := TMP.CMR TMP. PMR; IF TMP.CATE=INDUSTRY THEN R := 10; ELSIF TMP.CATE=DOMESTIC THEN R := 7. 50; ELSIF TMP.CATE=AGRI THEN R := 5; END IF;

- 10 TAMT := UNITS * R; UPDATE EBILL SET RATE=R, TOTAMT=TAMT WHERE CNO=TMP.CNO; END LOOP; COMMIT; END; /

To view the output, enter SELECT command as follows at SQL prompt: SELECT * FROM EBILL ORDER BY CNO; OUTPUT:

9. X CLASS RESULT PROCESSING Problem Def: A PL/SQL program to process X class results. The base table is TENTH and the columns are HTNO, SNAME, ENG, TEL, HIN, MAT, SCI, SOC, TOT, RESULT. Solution: 1. Create base table in SQL as follows: CREATE TABLE TENTH ( HTNO NUMBER(6) PRIMARY KEY, SNAME VARCHAR2(25) NOT NULL,
ENG NUMBER(3) CHECK (ENG BETWEEN 0 AND 100), TEL NUMBER(3) CHECK (TEL BETWEEN 0 AND 100), HIN NUMBER(3) CHECK (HIN BETWEEN 0 AND 100), MAT NUMBER(3) CHECK (MAT BETWEEN 0 AND 100), SCI NUMBER(3) CHECK (SCI BETWEEN 0 AND 100), SOC NUMBER(3) CHECK (SOC BETWEEN 0 AND 100), TOT NUMBER(3), RESULT VARCHAR2(10)

); 2. Enter records using INSERT command as follows:


INSERT INTO TENTH(HTNO,SNAME,ENG,TEL,HIN,MAT,SCI,SOC) VALUES(&HTNO,&SNAME,&ENG, &TEL, &HIN, &MAT, &SCI, &SOC);

3. Repeat the INSERT command to enter more records. 4. Now write the program and execute it.

- 11 Program:
DECLARE T TENTH.TOT%TYPE; R TENTH.RESULT%TYPE; BEGIN FOR STD IN (SELECT * FROM TENTH) LOOP T := STD.ENG + STD.TEL + STD.HIN + STD.MAT + STD.SCI + STD.SOC; IF (STD.ENG<34 OR STD.TEL<34 OR STD.HIN<20 OR STD.MAT<34 OR STD.SCI<34 OR STD.SOC<34) THEN R := FAIL; ELSIF T >= 360 THEN R := I CLASS; ELSIF T >= 300 THEN R := II CLASS; ELSE R := III CLASS; END IF; UPDATE TENTH SET TOT=T, RESULT=R WHERE HTNO=STD.HTNO; END LOOP; COMMIT; END; /

To view the output, enter SELECT command as follows at SQL prompt: SELECT * FROM TENTH ORDER BY HTNO; OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy