Database Management Systems Lab Manual
Database Management Systems Lab Manual
SEMESTER : IV
INDEX
Experiment.1
Solution:
Create a table called Employee & execute the following.
Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
SQL> CREATE TABLE EMPLOYEE (
2 EMPNO NUMBER,
3 ENAME VARCHAR2(20),
4 JOB VARCHAR2(20),
5 MANAGER_NO NUMBER,
6 SAL NUMBER,
7 COMMISSION NUMBER);
Table created.
SQL> exit;
PRIVILEGE
-------------------------------------
CREATE CLUSTER
CREATE INDEXTYPE
CREATE OPERATOR
CREATE PROCEDURE
CREATE SEQUENCE
CREATE SESSION
CREATE SYNONYM
CREATE TABLE
CREATE TRIGGER
CREATE TYPE
CREATE VIEW
11 rows selected.
----------------------------------------------------------------------------------------------------------------------------------------------
2. Insert the any three records in the employee table contains attributes EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION AND use rollback. Check the result.
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (1, 'JAY', 'SALES', 100, 1000, 50);
1 row created.
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (2, 'TIM', 'FINANCE', 100, 2000, 50);
1 row created.
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (3, 'SAM', ’HR', 100, 5000, 50);
1 row created.
SQL>ROLLBACK;
Rollback complete.
SQL> SELECT * FROM EMPLOYEE;
No rows selected
3. Add primary key constraint and not constraint to the employee table.
SQL> ALTER TABLE EMPLOYEE ADD CONSTRAINT PK_EMPNO PRIMARY KEY(EMPNO);
Table altered.
SQL> ALTER TABLE EMPLOYEE MODIFY ENAME NOT NULL;
Table altered.
SQL>DESC EMPLOYEE;
Name Null? Type
--------------------------------------------------------- ---------------------- -------------------------------------------------------
EMPNO NOT NULL NUMBER
ENAME NOT NULL VARCHAR2(20)
JOB VARCHAR2(20)
MANAGER_NO NUMBER
SAL NUMBER
COMMISSION NUMBER
4.Insert null values to the employee table and verify the result.
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (NULL,’KIM’, ‘HR’, 100, 1000, 50);
VALUES (NULL, ‘KIM’, ‘HR’, 100, 5000, 50)
*
ERROR at line 2:
ORA-01400: cannot insert NULL into (“SCOTT”. “EMPLOYEE”.” EMPNO”)
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (5, NULL, ‘HR’, 100, 5000, 50);
VALUES (5, NULL, ‘HR’, 100, 5000, 50)
*
ERROR at line 2:
ORA-01400: cannot insert NULL into (“SCOTT”. “EMPLOYEE”. “ENAME”)
Experiment.2
Create a table called employee that contains attributes EMPNO, ENAME, JOB, MGR, SAL & execute the
following
1. Add a column commission with domain to the Employee table.
2. Insert any five records into the table.
3. Update the column details of job
4. Rename the column of Employ table using alter command.
5. Delete the employee whose Empno is 105.
Solution:
Create a table called Employee that contain attributes EMPNO, ENAME, JOB, MGR, SAL &
execute the following.
SQL> CREATE TABLE EMPLOYEE (
2 EMPNO NUMBER,
3 ENAME VARCHAR2(20),
4 JOB VARCHAR2(20),
5 MGR NUMBER,
6 SAL NUMBER);
Table created.
Experiment – 3
Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM), Group by, Order by.
Solution:
1.Create Employee table containing all Records E_id, E_name, Age, Salary.
SQL>CREATE TABLE EMPLOYEE3(
2 E_ID NUMBER,
3 E_NAME VARCHAR2(20),
4 AGE NUMBER,
5 SALARY NUMBER);
Table created.
SQL>DESC EMPLOYEE3;
NAME NULL? TYPE
------------------------------ ----------------------------------- ----------------------------------
E_ID NUMBER
E_NAME VARCHAR2(20)
AGE NUMBER
SALARY
NUMBER
SQL>INSERT INTO EMPLOYEE3 (E_ID, E_NAME, AGE, SALARY)
2 VALUES (101,’JAY’,20, 10000);
1 row created
SQL>INSERT INTO EMPLOYEE3 (E_ID, E_NAME, AGE, SALARY)
2 VALUES (102,’TIM’,30,20000);
1 row created
Experiment 4:
Create a row level trigger for the customers table that would fire for INSERT or UPDATE or DELETE operations
performed on the CUSTOMERS table. This trigger will display the salary difference between the old & new
Salary.
CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
Solution:
CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
2 ID NUMBER,
3 NAME VARCHAR2(20),
4 AGE NUMBER,
5 ADDRESS VARCHAR2(20),
6 SALARY NUMBER);
Table created
ID NUMBER
NAME VARCHAR2(20)
AGE NUMBER
ADDRESS VARCHAR2(20)
SALARY NUMBER
VALUES(1001,’JAY’,20,’BANGALORE’,10000);
1 row created.
VALUES(1002,’RIM’,30,’MYSORE’,20000);
1 row created.
VALUES(1003,’SAM’,35,’BANGALORE’,30000);
1 row created.
VALUES(1004,’JOHN’,25,’BANGALORE’,40000);
1 row created.
VALUES(1005,’SIMI’,40,’MYSORE’,15000);
1 row created.
SQL>COMMIT;
Commit complete.
ID NUMBER
NAME VARCHAR2(20)
AGE NUMBER
ADDRESS VARCHAR2(20)
SALARY NUMBER
OLD_SALARY NUMBER
NEW_SALARY NUMBER
SAL_DIFF NUMBER
SQL> commit;
Commit complete.
SQL> SELECT * FROM customers;
ID NAME AGE ADDRESS SALARY OLD_SALARY NEW_SALARY SAL_DIFF
---------- ---------- ---------- --------------- ------------ ------------------ --------------------- -----------------
1001 JAY 20 Bangalore 20000 35000 20000 -15000
1002 TIM 30 Mysore 40000 20000 20000 20000
1003 SAM 35 Bangalore 30000
1004 John 25 Bangalore 40000
1005 Simi 40 Mysore 15000
EXPERIMENT-5
Create cursor for Employee table & extract the values from the table. Declare the variables, Open the cursor &
extract the values from the cursor. Close the cursor.
Employee (E_id, E_name, Age, Salary)
Solution:
SQL> CREATE TABLE Employee3(
2 E_name VARCHAR2(20),
3 E_id NUMBER,
4 Age NUMBER,
5 Salary NUMBER
6) ;
Table created.
SQL> DESC Employee3;
Name Null? Type
---------------------------------- --------------- ----------------------------
E_NAME VARCHAR2(20)
E_ID NUMBER
AGE NUMBER
SAL NUMBER
SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (101, 'JAY', 20, 10000);
1 row created.
SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (102, 'TIM', 30, 20000);
1 row created.
SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (103, 'SAM', 35, 25000);
1 row created.
SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (104, 'John', 40, 30000);
1 row created.
SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (105, 'Simi', 45, 50000);
1 row created.
SQL> COMMIT;
Commit complete.
ID NAME AGE SALARY
2 v_id employee3.E_id%type;
3 v_name employee3.E_name%type;
4 v_age employee3.age%type;
5 v_salary employee3.salary%type;
6
7 CURSOR cur_employee3 is
16
17 END LOOP;
18 CLOSE cur_employee3;
19 END;
20 /
E_id :101 E_name: JAY age :20 salary :10000
E_id :102 E_name: TIM age :30 salary :20000
E_id :103 E_name: SAM age :35 salary :25000
E_id :104 E_name: John age :40 salary :30000
E_id :105 E_name: Simi age :45 salary :50000
Experiment 6:
Write a PL/SQL block of code using parameterized Cursor, that will merge the data available in the newly
created table N_RollCall with the data available in the table O_RollCall. If the data in the first table already exist
in the second table then that data should be skipped.
Solution :
3 name varchar2(20),
4 address varchar2(30));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> COMMIT;
Commit complete.
11
12 --Parameterized cursor
13 CURSOR c_O_RollCall (p_rollnumber NUMBER)
14 IS SELECT rno,name,address
15 FROM O_RollCall a
16 WHERE (rno = p_rollnumber OR p_rollnumber IS NULL);
17
18 BEGIN
19 -- Open cursor
20 OPEN c_O_RollCall(v_cursor_param);
21 LOOP
22 FETCH c_O_RollCall INTO v_rno, v_name, v_address;
23 EXIT WHEN c_O_RollCall%NOTFOUND;
24
25 -- check if rno already exists in N_RollCall table
26 SELECT COUNT (*)
27 INTO v_count
28 FROM N_RollCall
29 WHERE rno = v_rno;
30
31 IF v_count = 0 THEN
32
33 INSERT INTO N_RollCall (rno, name, address)
34 VALUES (v_rno, v_name, v_address);
35
36 COMMIT;
37
38 DBMS_OUTPUT.PUT_LINE (‘Data merged successfully: rno:’||v_no);
39 ELSE
40 DBMS_OUTPUT.PUT_LINE ('rno skipped: '||v_rno);
41 END IF
42 END LOOP;
43 END;
44 /
Data merged successfully! Rno:3
PL/SQL procedure successfully completed.
SQL> SELECT * FROM N_rollcall;
RNO NAME ADDRESS
--------- ------------------------------ ---------------------------
3 Rekha MYSORE
SQL> DECLARE
2 v_rno O_RollCall.rno%TYPE;
3 v_name O_RollCall.name%TYPE;
4 v_address O_RollCall.address%TYPE;
5
6 v_count number;
7 v_cursor_param O_RollCall.rno%TYPE:= 4;
8
9 /* Specify the rno to be copied from O_rollcall into N_rollcall table, or
10 Specify NULL if all the rno's needs to be copied which are not existing in N_rollcall table */
11
12 --Parameterized cursor
13 CURSOR c_O_RollCall (p_rollnumber NUMBER)
14 IS SELECT rno,name,address
15 FROM O_RollCall a
16 WHERE (rno = p_rollnumber OR p_rollnumber IS NULL);
17
18 BEGIN
19 -- Open cursor
20 OPEN c_O_RollCall(v_cursor_param);
21 LOOP
22 FETCH c_O_RollCall INTO v_rno, v_name, v_address;
23 EXIT WHEN c_O_RollCall%NOTFOUND;
24
SQL> DECLARE
2 v_rno O_RollCall.rno%TYPE;
3 v_name O_RollCall.name%TYPE;
4 v_address O_RollCall.address%TYPE;
5
6 v_count number;
7 v_cursor_param O_RollCall.rno%TYPE:= 3;
8
9 /* Specify the rno to be copied from O_rollcall into N_rollcall table, or
10 Specify NULL if all the rno's needs to be copied which are not existing in N_rollcall table */
11
12 --Parameterized cursor
13 CURSOR c_O_RollCall (p_rollnumber NUMBER)
14 IS SELECT rno, name, address
15 FROM O_RollCall a
16 WHERE (rno = p_rollnumber OR p_rollnumber IS NULL);
17
18 BEGIN
19 -- Open cursor
20 OPEN c_O_RollCall(v_cursor_param);
21 LOOP
22 FETCH c_O_RollCall INTO v_rno, v_name, v_address;
23 EXIT WHEN c_O_RollCall%NOTFOUND;
24
25 -- check if rno already exists in N_RollCall table
26 SELECT COUNT (*)
27 INTO v_count
28 FROM N_RollCall
29 WHERE rno = v_rno;
30
31 IF v_count = 0 THEN
32
33 INSERT INTO N_RollCall (rno, name, address)
34 VALUES (v_rno, v_name, v_address);
35
36 COMMIT;
37
Experiment 7:
Install an Open Source NoSQL Data base MongoDB & perform basic CRUD (Create,
Read, Update & Delete) operations. Execute MongoDB basic Queries using CRUD
operations.
Solution:
//Create operation
//Read Operation
//Update operations
//Delete operation