Advanced RDBMS: Rdbms Lab Table Creation Create The Following Tables
Advanced RDBMS: Rdbms Lab Table Creation Create The Following Tables
Advanced RDBMS: Rdbms Lab Table Creation Create The Following Tables
RDBMS LAB Table Creation Create the following tables: /* Table Creation */ CREATE TABLE RagEmployee (EmpNo Char(4) Not Null, EmpName Varchar2(30) Default ' ', DeptNo Char(4), Location Char(4) Default ' ', Manager Char(4) Default ' ', Desig Varchar2(20) Default ' ', Basic Number(8,2) Default 0, HRA Number(8,2) Default 0, DA Number(8,2) Default 0, Other_All Number(8,2) Default 0, PF Number(8,2) Default 0, Other_Dedn Number(8,2) Default 0) ; CREATE TABLE RagDeptMast (DeptNo Char(4) Not Null, DeptName Varchar2(30) Default ' ') ; CREATE Table RagSALES (Sale_Date Date, Item_No Number(8), Quantity Number(8) Default 0, Sale_By Char(4), Rate Number(8,2) Default 0) ; CREATE TABLE RagORDER (Order_Date Date, Order_ID Number(6) Default 0, Cust_ID Number(6), Item_No Number(8), Quantity Number(8) Default 0, Rate Number(8,2) Default 0) ; CREATE TABLE RagItem_Mast (Item_No Number(8) Not Null, Item_Name Varchar2(30) Default ' ', Item_Rate Number(8,2) Default 0, Last_Upd_Date Date) ;
Page 1
Advanced RDBMS
CREATE Table RagCustMast (Cust_ID Number(8), Cust_Name Varchar2(30) Default ' ', Cust_Addr1 Varchar2(30) Default ' ', Cust_Addr2 Varchar2(30) Default ' ', Cust_Addr3 Varchar2(30) Default ' ') ; CREATE TABLE RagSupplier (Supp_Code Number(3) Default 0, Supp_Name Varchar2(50) Default ' ', Address1 Varchar2(30) Default ' ', Address2 Varchar2(30) Default ' ', Address3 Varchar2(30) Default ' ', Address4 Varchar2(30) Default ' ') ; /* End of Table Creation */ Create the following constraints for the above tables /* Constraints Creation */ Alter Table RagEmployee Add Constraint RagEmp_PK Primary Key(EmpNo) ; Alter Table RagDeptMast Add Constraint RagDept_PK Primary Key(DeptNo) ; Alter Table RagEmployee Add Constraint RagEmp_Dept_FK Foreign Key(DeptNo) References RagDeptMast(DeptNo) ; ALTER TABLE RagItem_Mast Add Constraint RagItem_Mast_PK Primary Key(Item_No) ; ALTER TABLE RagSales Add Constraint RagSales_Item_FK Foreign Key(Item_No) References RagItem_Mast(Item_No) ; ALTER TABLE RagOrder Add Constraint RagOrder_Item_FK Foreign Key(Item_No) References RagItem_Mast(Item_No) ; Alter table RagCustMast Add Constraint RagCustMast_PK Primary Key(Cust_ID) ; Alter table RagOrder Add Constraint RagOrder_Cust_ID_FK Foreign Key(Cust_ID) References RagCustMast(Cust_ID) ; /* End of Constraints Creation */ Page 2
Advanced RDBMS
Insert the following records into the above tables: /* Data Insertion */ INSERT INTO RagDeptMast(DeptNo VALUES('100','Administration') ; INSERT INTO RagDeptMast(DeptNo VALUES('101','Personnel') ; INSERT INTO RagDeptMast(DeptNo VALUES('102','Marketing') ; INSERT INTO RagDeptMast(DeptNo VALUES('103','Development') ; INSERT INTO RagDeptMast(DeptNo VALUES('104','Sales') ; INSERT INTO RagDeptMast(DeptNo VALUES('105','Testing') ; , DeptName) , DeptName) , DeptName) , DeptName) , DeptName) , DeptName)
INSERT INTO RagEmployee (EmpNo , EmpName , DeptNo , Location , Desig , Basic , HRA , DA , Other_All , PF , Other_Dedn) VALUES ('1000', 'Niranjan', '100', 'CHEN', 'Manager', 5000, 1200, 1400, 2000, 60.00, 0) ; INSERT INTO RagEmployee (EmpNo , EmpName , DeptNo , Location , Desig , Basic , HRA , DA , Other_All , PF , Other_Dedn) VALUES ('1001', 'Ramkumar', '101', 'CHEN', 'Executive', 8000, 1500, 1800, 2300, 96.00, 0) ; INSERT INTO RagEmployee (EmpNo , EmpName , DeptNo , Location , Desig , Basic , HRA , DA , Other_All , PF , Other_Dedn) VALUES ('1002', 'Dinesh Kumar Dubey', '102', 'CHEN', 'Senior Executive',
Page 3
Advanced RDBMS
10000, 2000, 1800, 2600, 120.00, 0) ; INSERT INTO RagEmployee (EmpNo , EmpName , DeptNo , Location , Desig , Basic , HRA , DA , Other_All , PF , Other_Dedn) VALUES ('1003', 'Balaguru', '103', 'CHEN', 'Senior DBA', 15000, 1600, 2500, 3000, 180.00, 0) ; INSERT INTO RagEmployee (EmpNo , EmpName , DeptNo , Location , Desig , Basic , HRA , DA , Other_All , PF , Other_Dedn) VALUES ('1004', 'Ramesh', '104', 'CHEN', 'Senior Mktg. Exec.', 11000, 1600, 1900, 2200, 132.00, 0) ; /* End of Data Insertion */ Question 1: ~~~~~~~~~ Write a PL/SQL block, which accepts principal, duration (in months), and rate of interest and calculate maturity amount. Solution: DECLARE PRINCIPAL NUMBER := &PRINCIPAL; MONTHS NUMBER := &MONTHS; ROI NUMBER := &ROI; MAMOUNT NUMBER ; BEGIN MAMOUNT := PRINCIPAL + (PRINCIPAL * ROI/100) * MONTHS; DBMS_OUTPUT.PUT_LINE(MAMOUNT); END; Question 2: ~~~~~~~~~
Page 4
Advanced RDBMS
Accept radius and calculate area of circle. Use variable pi as constant. DECLARE RADIUS NUMBER := &RADIUS; PI NUMBER := 3.14; AREA NUMBER; BEGIN AREA := PI * (RADIUS * RADIUS); DBMS_OUTPUT.PUT_LINE(AREA IS ||AREA); END; Question 3: ~~~~~~~~~ Solution: Write a PL/SQL block to accept a number and display whether it is less than, or equal, or greater than 10. DECLARE N NUMBER := 5; BEGIN IF N = 10 THEN DBMS_OUTPUT.PUT_LINE(N|| EQUAL TO 10); ELSIF N < 10 THEN DBMS_OUTPUT.PUT_LINE(N|| LESS THAN 10); ELSIF N > 10 THEN DBMS_OUTPUT.PUT_LINE(N||GREATERTHAN 10); END IF; END; Question 4: ~~~~~~~~~ Write a PL/SQL block to generate EVEN, ODD numbers less than 100 using any One loop Solution: BEGIN FOR I IN 1..100 LOOP IF MOD(I,2) != 0 THEN DBMS_OUTPUT.PUT_LINE(I); END IF; END LOOP;
Page 5
Advanced RDBMS
END;
Question : 5 ~~~~~~~~~ Create a PL/SQL block to display employee details in ascending order of employee names Solution: DECLARE varEmpNo Char(4) := ' '; varEmpName Varchar2(30) := ' '; CURSOR Emp_Cur IS SELECT EmpNo, EmpName FROM RagEmployee ORDER BY EmpName Asc ; BEGIN dbms_output.put_line('Employee Details : ') ; dbms_output.put_line('======================') ; dbms_output.put_line('Data as Stored : ') ; dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~~~') ; FOR I IN(SELECT EmpNo, EmpName FROM RagEmployee) LOOP dbms_output.put_line(I.EmpName) ; END LOOP ; OPEN Emp_Cur ; FETCH Emp_Cur INTO varEmpNo, varEmpName ; IF Emp_Cur%NOTFOUND THEN RETURN ; END IF ; dbms_output.put_line(' ') ; dbms_output.put_line('Data after Sorting : ') ; dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~~~') ; LOOP IF Emp_Cur%NOTFOUND THEN EXIT ; END IF ; dbms_output.put_line(varEmpName) ; FETCH Emp_Cur INTO varEmpNo, varEmpName ;
Page 6
Advanced RDBMS
END LOOP ; CLOSE Emp_Cur ; END ; Question : 6 ~~~~~~~~~~~~ Create a PL/SQL Block to display values from employee table using For loop. Solution: DECLARE varEmpNo Char(4) := ' ' ; varEmp Char(4) := ' ' ; varNetPay Number(15,2) := 0 ; BEGIN varEmpNo := &varEmp ; FOR I IN (SELECT * FROM RagEmployee WHERE EmpNo = varEmpNo) LOOP varNetPay := (I.Basic + I.HRA + I.DA + I.Other_All - I.PF I.Other_Dedn) ; dbms_output.put_line('Employee Salary Details') ; dbms_output.put_line('=======================') ; dbms_output.put_line('Emp. Det : '||I.EmpNo||'-'||I.EmpName) ; dbms_output.put_line('Basic : '||I.Basic) ; dbms_output.put_line('HRA : '||I.HRA) ; dbms_output.put_line('DA : '||I.DA) ; dbms_output.put_line('Other Allowance : '||I.Other_All) ; dbms_output.put_line('Gross Salary : '||(I.Basic + I.HRA + I.DA + I.Other_All)) ; dbms_output.put_line('Deductions : ') ; dbms_output.put_line('----------------- ') ; dbms_output.put_line('PF : '||I.PF) ; dbms_output.put_line('Other Deduction : '||I.Other_Dedn) ; dbms_output.put_line('Tot. Deductions : '||(I.PF + I.Other_Dedn)) ; dbms_output.put_line('Net Salary : '||varNetPay) ; END LOOP ; END ; /
Page 7
Advanced RDBMS
Question: 7 : ~~~~~~~~~ Create a PL/SQL Block to reverse a string using function. Solution: Declare varString Varchar2(30) ; varInputStr Varchar2(30) ; varRet_String Varchar2(30) ; Begin varInputStr := '&varString' ; SELECT RagReverse(varInputStr) INTO varRet_String FROM DUAL ; dbms_output.Put_Line(' ') ; dbms_output.Put_Line(' ') ; dbms_output.Put_Line('Orignal String = ' || varInputStr) ; dbms_output.Put_Line('Reversed String = ' || varRet_String) ; End ; CREATE OR REPLACE FUNCTION RagReverse (strOrg_String Varchar2) RETURN VarChar2 AS varLen Number(2) ; strRev_String Varchar2(30) ; Begin varLen := LENGTH(strOrg_String) ; LOOP IF varLen < 1 THEN EXIT ; END IF ; strRev_String := strRev_String || Substr(strOrg_String,VarLen,1) ; VarLen := varlen - 1 ; END Loop ; RETURN strRev_Stringz ; End ; Question : 8 ~~~~~~~~~ Create a PL/SQL Block to find the stock rate and print a suitable error message using Exceptions. Input Stock Qty and Value. Solution: DECLARE ZQty Number(8) := 0 ; ZValue Number(15,2) := 0 ; Page 8
Advanced RDBMS
ZStock_Qty Number(8) := 0 ; ZStock_Value Number(15,2) := 0 ; ZStock_Rate Number(8,2) := 0 ; BEGIN ZStock_Qty := &ZQty ; ZStock_Value := &ZValue ; ZStock_Rate := Round(ZStock_Value / ZStock_Qty,2) ; Dbms_Output.Put_line('Calculated Rate is : '||ZStock_Rate) ; Dbms_Output.Put_Line(' ') ; Dbms_Output.Put_Line(' ') ; Exception WHEN OTHERS THEN Dbms_output.Put_LIne(' Error Message : '||SqlErrm) ; END ; Question : 9 ~~~~~~~~~ Create a PL/SQL Block to accept values and swap them using function. Solution: /* PL/SQL Block for getting the values */ DECLARE varValue1 varValue2 Number(4) := 0 ; Number(4) := 0 ;
varValue3 Number(4) := 0 ; varValue4 Number(4) := 0 ; varDisp_Msg Varchar2(100) := 0; BEGIN varValue3 := &varValue1 ; varValue4 := &varValue2 ; SELECT Print_Swap_Values(varValue3, varValue4, 'O') INTO varDisp_Msg FROM DUAL ; dbms_output.put_line(varDisp_Msg) ; varValue3 := (varValue3 + varValue4) ; varValue4 := (varValue3 - varValue4) ; varValue3 := (varValue3 - varValue4) ; SELECT Print_Swap_Values(varValue3, varValue4, 'N') INTO varDisp_Msg FROM DUAL ; Page 9
Advanced RDBMS
dbms_output.put_line(varDisp_Msg) ; END ; / /* Function returning the original/swapped Values */ CREATE OR REPLACE FUNCTION Print_Swap_Values (varFirst_Value Number, varSecond_Value Number, varStatus CHAR) RETURN CHAR AS varDisp_Message Varchar2(100) ; BEGIN IF varStatus = 'O' THEN varDisp_Message := 'Values before Swapping : '||varFirst_Value||' and '||varSecond_Value ; ELSE varDisp_Message := 'Values after Swapping : '||varFirst_Value||' and '||varSecond_Value ; END IF ; RETURN varDisp_Message ; END ;
Question : 10 ~~~~~~~~~ Create a trigger to restrict data entry on Weekends. Solution: CREATE OR REPLACE TRIGGER Employee_Trigger BEFORE INSERT OR UPDATE OR DELETE ON RagEmployee FOR EACH ROW DECLARE varWeek_of_Day Varchar2(20) ; BEGIN SELECT Trim(To_Char(SysDate,'Day')) INTO varWeek_of_Day FROM DUAL ; IF UPPER(varWeek_of_Day) IN('SATURDAY','SUNDAY') THEN Raise_Application_Error(-20001,'No data entry permitted on Saturdays and Sundays') ; END IF ; END ; Page 10
Advanced RDBMS
Question : 11 ~~~~~~~~~ Delete records from sales table and display the number of record deleted. DECLARE varDelCtr Number(5) := 0 ; BEGIN FOR I IN(SELECT A.*, A.Rowid FROM RAGSALES A) LOOP DELETE FROM RAGSALES WHERE Rowid = I.Rowid ; varDelCtr := varDelCtr + 1 ; END LOOP ; dbms_output.put_line('No. of Records Deleted : '||varDelCtr) ; END ; Question : 12 ~~~~~~~~~ Create a Sequence and insert values into supplier table. CREATE SEQUENCE RagSequence Increment By 1 Start With 100 MaxValue 250 NoCycle ; INSERT INTO RagSupplier (Supp_Code, Supp_Name, Address1, Address2, Address3, Address4) VALUES (RagSequence.NextVal, 'Hindustan Lever Ltd.,', '3, Annie Besant Road', 'Worli', 'Mumbai - 600 020','Maharashtra') ; INSERT INTO RagSupplier (Supp_Code, Supp_Name, Address1, Address2, Address3, Address4) VALUES (RagSequence.NextVal, 'Pfizer Limited,', '5, Annie Besant Road', 'Worli', 'Mumbai - 600 020','Maharashtra') ;
Page 11
Advanced RDBMS
Exercise: 1. Create the table EMPUNION with the following attributes: ID NUMBER(4), LEADER VARCHAR2(20), DETAIL EMPNT SAL NUMBER(7,3) 2. Create a table ANIMAL_TYPE with the following properties: ID BREED NAME BIRTHDATE NUMBER, VARCHAR2(20), VARCHAR2(20), DATE
3. Add PRIMARY KEY constraint on the attribute ID of the table ANIMAL_TYPE. 4. Insert 3 records in ANIMAL_TYPE table. Values to be inserted in Dept table 301, Labrador, 21/03/99 302, Alsation, 23/06/99 Page 12 303, Bull Dog, 24/03/98
Advanced RDBMS
5. Insert 5 records in EMPUNION table. Values to be inserted in EMPUNION table 001, vijay, Chennai,20000 002, Valli, Bangalore,30000 003, shalini, Delhi,35000 004, Bhaskar, Mumbai,40000 005, Jai, Ahmedabad,15000
6. Select all records from the ANIMAL_TYPE table and EMPUNION table. 7. Select only the LEADER with ID 002 from the EMPUNION table. 8. List all the LEADRES whose name begin with v; 9. Give a command to create a table(sample_emp) with the same structure as the EMPUNION table, without any values in it. 10. Create a view called emp_view consisting of the ID, LEADER, and DETAIL for the EMPUNION department 301. 11. Give a query to calculate the total salary of the LEADER in the EMPUNION table and list the total salary column name has Net salary.(total salary = salary + commission). PL/SQL 1. Load and run following block after correcting errors: Declare No number; Msg Varchar2(4); Begin No:=&no; Msg:=decode(sign(no),1,+ive,-1,-ive,zero); Dbms_output.put_line(Msg); End;
Page 13
Advanced RDBMS
2. Write a PL/SQL block which accepts principal, duration (in months), rate of interest and calculate maturity amount. 3. Write a PL/SQL block to accept a number and display whether it is less than, or equal, or greater than 10. (Note: do it with and without ELSEIF clause) 4. Create a table Sales Table EMPNO NUMBER(3), ENAME VARCHAR2(25), MONTH CHAR(2), TARGET NUMBER(9,2), ACHIEVED CHAR(1), should allow Y or N TOT_SALES NUMBER(9,2) Insert few records to sales table. Write a block, which deletes the records from the sales table and reports number of records have been deleted
Page 14