DBMS Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

SRI VENKATESHWARA COLLEGE OF ENGINEERING

DBMS LAB MANUAL


BCS403

1
SRI VENKATESHWARA COLLEGE OF ENGINEERING

1. Create a table called Employee & execute the following.


Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)

1. Create a user and grant all permissions to the user.


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.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.

AIM:
Using basic database management operations using SQL, specifically focusing on user creation, table
creation, data manipulation, and constraint application. The tasks include creating a new user with full
permissions, performing data insertion and rollback operations, adding constraints to the table, and
verifying the enforcement of these constraints.

1. Create a user and grant all permissions to the user


CREATE USER Employee IDENTIFIED BY ‘User’;
GRANT ALL PRIVILEGES TO Employee;

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.

CREATE TABLE Employee (


EMPNO INT,
ENAME VARCHAR(50),
JOB VARCHAR(50),
MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2)
);

3. Add primary key constraint and not null constraint to the employee table.
ALTER TABLE Employee.

ADD CONSTRAINT PK_EMPNO PRIMARY KEY (EMPNO);


ALTER TABLE Employee

2
SRI VENKATESHWARA COLLEGE OF ENGINEERING

MODIFY EMPNO INT NOT NULL,


MODIFY ENAME VARCHAR(50) NOT NULL,
MODIFY JOB VARCHAR(50) NOT NULL,
MODIFY MANAGER_NO INT NOT NULL,
MODIFY SAL DECIMAL(10, 2) NOT NULL,
MODIFY COMMISSION DECIMAL(10, 2) NOT NULL;

4. Insert null values to the employee table and verify the result.

INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,


COMMISSION) VALUES (4, NULL, 'Tester', 104, NULL, NULL);

SELECT * FROM Employee;

2. Create a table called Employee that contain attributes EMPNO,ENAME,JOB,


MGR,SAL & execute the following.
1. Add a column commission with domain to the Employeetable.
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.
AIM: To demonstrate database management operations involving table modification, data insertion,
data update, column renaming, and record deletion using SQL commands.

CREATE TABLE Employee (


EMPNO INT PRIMARY KEY,
ENAME VARCHAR(50) NOT NULL,
JOB VARCHAR(50) NOT NULL,
MGR INT,
SAL DECIMAL(10, 2) NOT NULL
);

1. Add a column commission with domain to the Employeetable.

ALTER TABLE Employee ADD COMMISSION DECIMAL(10, 2);

2. Insert any five records into the table.

INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)


VALUES (101, 'John Doe', 'Manager', 100, 50000.00, 5000.00);

3
SRI VENKATESHWARA COLLEGE OF ENGINEERING

INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)


VALUES (102, 'Jane Smith', 'Developer', 101, 45000.00, 4500.00);

INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)


VALUES (103, 'Alice Brown', 'Analyst', 101, 47000.00, 4700.00);

INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)


VALUES (104, 'Bob White', 'Tester', 102, 43000.00, 4300.00);

INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)


VALUES (105, 'Charlie Black', 'Support', 102, 42000.00, 4200.00);

3. Update the column details of job.

UPDATE Employee SET JOB = 'Senior Developer' WHERE EMPNO = 102;

4. Rename the column of Employ table using alter command.

ALTER TABLE Employee RENAME COLUMN ENAME TO EMP_NAME;

5. Delete the employee whose Empno is 105.

DELETE FROM Employee WHERE EMPNO = 105;

3. Queries using aggregate functions(COUNT,AVG,MIN,MAX,SUM),Group


by,Orderby.
Employee(E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employeetable
3. Find the Maximum age from employee table.
4. Find the Minimum age from employeetable.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees

AIM: To demonstrate the use of aggregate functions and SQL clauses like GROUP BY and
ORDER BY. The tasks include creating a table, inserting records, and performing various
queries to count records, find maximum and minimum values, sort data, and group data based
on specific attributes.
1. Create Employee table containing all Records E_id, E_name, Age, Salary.

CREATE TABLE Employee (


E_id INT PRIMARY KEY,

4
SRI VENKATESHWARA COLLEGE OF ENGINEERING

E_name VARCHAR(50) NOT NULL,


Age INT NOT NULL,
Salary DECIMAL(10, 2) NOT NULL
);

INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (1, 'John Doe', 30, 50000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (2, 'Jane Smith', 25, 45000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (3, 'Alice Brown', 28, 47000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (4, 'Bob White', 35, 43000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (5, 'Charlie Black', 32, 42000.00);

2. Count number of employee names from employeetable.

SELECT COUNT(E_name) AS Employee_Count FROM Employee;

3. Find the Maximum age from employee table

SELECT MAX(Age) AS Max_Age FROM Employee;

4. Find the Minimum age from employeetable.

SELECT MIN(Age) AS Min_Age FROM Employee;

5. Find salaries of employee in Ascending Order.

SELECT Salary FROM Employee ORDER BY Salary ASC;

6. Find grouped salaries of employees

SELECT Salary, COUNT(E_id) AS Employee_Count FROM Employee GROUP BY Salary;

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)

5
SRI VENKATESHWARA COLLEGE OF ENGINEERING

AIM:
To create a row-level trigger for the CUSTOMERS table that fires for INSERT, UPDATE, or
DELETE operations and displays the salary difference between the old and new salary.

 The trigger will use the BEFORE clause to ensure it executes before the INSERT, UPDATE, or
DELETE operations.
 It will check for INSERT, UPDATE, and DELETE operations using the WHEN clause and
conditional logic.

CREATE OR REPLACE TRIGGER trg_salary_diff


BEFORE INSERT OR UPDATE OR DELETE ON CUSTOMERS
FOR EACH ROW
DECLARE
v_salary_diff NUMBER;
BEGIN
IF INSERTING THEN
-- For INSERT operation, old salary does not exist
DBMS_OUTPUT.PUT_LINE('New customer inserted with ID: ' || :NEW.ID || ' and Salary:
' || :NEW.SALARY);
ELSIF UPDATING THEN
-- For UPDATE operation, calculate salary difference
v_salary_diff := :NEW.SALARY - :OLD.SALARY;
DBMS_OUTPUT.PUT_LINE('Customer ID: ' || :OLD.ID || ' salary changed from ' ||
:OLD.SALARY || ' to ' || :NEW.SALARY);
DBMS_OUTPUT.PUT_LINE('Salary difference: ' || v_salary_diff);
ELSIF DELETING THEN
-- For DELETE operation, new salary does not exist
DBMS_OUTPUT.PUT_LINE('Customer deleted with ID: ' || :OLD.ID || ' and Salary: ' ||
:OLD.SALARY);
END IF;
END;
/

TESTING
INSERT A NEW CUSTOMER.

INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY) VALUES (1,
'John Doe', 30, '123 Elm St', 50000);
UPDATE A CUSTOMER'S SALARY.

6
SRI VENKATESHWARA COLLEGE OF ENGINEERING

UPDATE CUSTOMERS SET SALARY = 55000 WHERE ID = 1;

DELETE A CUSTOMER.

DELETE FROM CUSTOMERS WHERE ID = 1;

5. Create cursor for Employee table & extract the values from the table.
Declare the variables ,Open the cursor & extrct the values from the cursor.
Close the cursor.
Employee(E_id, E_name, Age, Salary)

To create a cursor for the Employee table, extract values, and demonstrate the complete
lifecycle of a cursor in Oracle PL/SQL (declare, open, fetch, and close).

DECLARE
-- Declare variables to hold the values from the Employee table
v_E_id Employee.E_id%TYPE;
v_E_name Employee.E_name%TYPE;
v_Age Employee.Age%TYPE;
v_Salary Employee.Salary%TYPE;

-- Declare the cursor for the Employee table


CURSOR emp_cursor IS
SELECT E_id, E_name, Age, Salary FROM Employee;

BEGIN
-- Open the cursor
OPEN emp_cursor;

-- Fetch the values from the cursor


LOOP
FETCH emp_cursor INTO v_E_id, v_E_name, v_Age, v_Salary;
EXIT WHEN emp_cursor%NOTFOUND;

-- Display the fetched values


DBMS_OUTPUT.PUT_LINE('E_id: ' || v_E_id || ', E_name: ' || v_E_name || ', Age: ' ||
v_Age || ', Salary: ' || v_Salary);
END LOOP;

7
SRI VENKATESHWARA COLLEGE OF ENGINEERING

-- Close the cursor


CLOSE emp_cursor;
END;
/

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.

#First we need to create table

CREATE TABLE O_RollCall (


Roll_ID NUMBER PRIMARY KEY,
Roll_Name VARCHAR2(100)
);

CREATE TABLE N_RollCall (


Roll_ID NUMBER PRIMARY KEY,
Roll_Name VARCHAR2(100)
);

#
SET SERVEROUTPUT ON;

DECLARE
-- Variables to hold the values fetched from the cursor
v_Roll_ID N_RollCall.Roll_ID%TYPE;
v_Roll_Name N_RollCall.Roll_Name%TYPE;

-- Cursor to fetch data from N_RollCall


CURSOR c_n_rollcall IS
SELECT Roll_ID, Roll_Name FROM N_RollCall;

-- Function to check if a Roll_ID already exists in O_RollCall


FUNCTION is_existing(p_Roll_ID IN NUMBER) RETURN BOOLEAN IS
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count

8
SRI VENKATESHWARA COLLEGE OF ENGINEERING

FROM O_RollCall
WHERE Roll_ID = p_Roll_ID;

RETURN v_count > 0;


END;
BEGIN
-- Open the cursor
OPEN c_n_rollcall;

-- Fetch the values from the cursor


LOOP
FETCH c_n_rollcall INTO v_Roll_ID, v_Roll_Name;
EXIT WHEN c_n_rollcall%NOTFOUND;

-- Check if the Roll_ID already exists in O_RollCall


IF NOT is_existing(v_Roll_ID) THEN
-- If not exists, insert the record into O_RollCall
INSERT INTO O_RollCall (Roll_ID, Roll_Name)
VALUES (v_Roll_ID, v_Roll_Name);

-- Optionally, display a message indicating the record has been inserted


DBMS_OUTPUT.PUT_LINE('Inserted Roll_ID: ' || v_Roll_ID || ',
Roll_Name: ' || v_Roll_Name);
ELSE
-- Optionally, display a message indicating the record was skipped
DBMS_OUTPUT.PUT_LINE('Skipped Roll_ID: ' || v_Roll_ID || ',
Roll_Name: ' || v_Roll_Name);
END IF;
END LOOP;

-- Close the cursor


CLOSE c_n_rollcall;

-- Commit the transaction


COMMIT;
END;
/

9
SRI VENKATESHWARA COLLEGE OF ENGINEERING

AIM:

To install an open-source NoSQL database, MongoDB, and perform basic CRUD (Create,
Read, Update, Delete) operations.

10

You might also like