Lab Manual - 20240506 - 120531 - 0000 (4) - 1
Lab Manual - 20240506 - 120531 - 0000 (4) - 1
Lab Manual - 20240506 - 120531 - 0000 (4) - 1
OF ENGINEERING
AFFILIATED TO VTU | RECOGNIZED BY GOVT. OF KARNATAKA
APPROVED BY AICTE
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DBMS LAB
M A N U A L
BCS403
Prepared by:
Divya K. K.
Jalaluddeen B M
Asst. Professor
Department of CSE, PACE
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 theuser.
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.
-- Create Employee table
CREATE TABLE Employee (
EMPNO INT,
ENAME VARCHAR(50),
JOB VARCHAR(50),
MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2)
);
1. Create a user and grant all permissions to theuser.
-- Create a new user
CREATE USER myuser IDENTIFIED BY password;
-- Grant all permissions to the new user on the Employee table
GRANT ALL PRIVILEGES ON Employee TO myuser;
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.
-- Insert three records into the Employee table
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
VALUES
(1, 'John Doe', 'Manager', 1001, 5000.00, 1000.00),
(2, 'Jane Smith', 'Analyst', 1001, 4000.00, NULL),
(3, 'Mike Johnson', 'Clerk', 1002, 3000.00, 500.00);
-- Use ROLLBACK to undo the changes (not committing the transaction)
ROLLBACK;
3. Add primary key constraint and not null constraint to the employee table.
-- Add Primary Key constraint on EMPNO
ALTER TABLE Employee
ADD CONSTRAINT PK_Employee PRIMARY KEY (EMPNO);
-- Add NOT NULL constraint to EMPNO column
ALTER TABLE Employee
MODIFY EMPNO INT NOT NULL;
-- Add NOT NULL constraint to ENAME column
ALTER TABLE Employee
MODIFY ENAME VARCHAR(50) NOT NULL;
-- Add NOT NULL constraint to JOB column
ALTER TABLE Employee
MODIFY JOB VARCHAR(50) NOT NULL;
4. Insert null values into the Employee table and verify the result. (Note: Since we added NOT
NULL constraints, inserting null values will result in an error):
-- Try to insert a record with NULL values into the Employee table
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
VALUES (4, NULL, 'Intern', NULL, NULL, NULL);
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.
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
DELIMITER //
CREATE TRIGGER customers_insert_salary_change
BEFORE INSERT ON customers
FOR EACH ROW
BEGIN
DECLARE new_salary DECIMAL(10, 2);
SET new_salary = NEW.salary;
SET @message = CONCAT('New salary: ', new_salary);
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @message;
END;
//
DELIMITER ;
DELIMITER //
CREATE TRIGGER customers_update_salary_change
BEFORE UPDATE ON customers
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
DECLARE new_salary DECIMAL(10, 2);
SET old_salary = OLD.salary;
SET new_salary = NEW.salary;
SET @message = CONCAT('Salary difference: ', (new_salary -
old_salary));
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @message;
END;
//
DELIMITER ;
DELIMITER //
CREATE TRIGGER customers_delete_salary_change
BEFORE DELETE ON customers
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
SET old_salary = OLD.salary;
SET @message = CONCAT('Salary before deletion: ', old_salary);
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @message;
END;
//
DELIMITER ;
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)
DELIMITER //
CREATE PROCEDURE extract_employee_values()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE emp_id INT;
DECLARE emp_name VARCHAR(255);
DECLARE emp_age INT;
DECLARE emp_salary DECIMAL(10, 2);
DECLARE cur CURSOR FOR
SELECT E_id, E_name, Age, Salary FROM Employee;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
emp_loop: LOOP
FETCH cur INTO emp_id, emp_name, emp_age, emp_salary;
IF done THEN
LEAVE emp_loop;
END IF;
-- Do something with the extracted values
-- For example, you can print them
SELECT emp_id, emp_name, emp_age, emp_salary;
END LOOP;
CLOSE cur;
END //
DELIMITER ;
-- This statement calls the stored procedure to extract employee values
CALL extract_employee_values();
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
Step 4: Follow the installation prompts and customize the installation according to
your need.
Step 5: At this stage, a prompt will pop which can be used to configure the setting
of the MongoDB Compass.