Lab Manual - 20240506 - 120531 - 0000 (4) - 1

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

P A COLLEGE

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. 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.


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

1. Create Employee table containing all Records E_id, E_name, Age, Salary.

--Insert Records into the Employee Table:


INSERT INTO Employee (E_id, E_name, Age, Salary)
VALUES
(1, 'John Doe', 30, 5000.00),
(2, 'Jane Smith', 25, 4000.00),
(3, 'Mike Johnson', 35, 6000.00),
(4, 'Sarah Williams', 28, 4500.00),
(5, 'Chris Brown', 32, 5500.00);

2. Count the number of employee names from the Employee table:

3. Find the maximum age from the Employee table:

4. Find the minimum age from the Employee table:

5. Find salaries of employees in ascending order:

6. Find grouped salaries of employees:


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)
CREATE TABLE customers (
ID INT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(255),
AGE INT,
ADDRESS VARCHAR(255),
SALARY DECIMAL(10, 2)
);
INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (1, 'John Doe', 30, '123 Main St', 50000);
INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (2, 'Jane Smith', 25, '456 Elm St', 60000);
INSERT INTO customers (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (3, 'Mike Johnson', 35, '789 Oak St', 70000);

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)

CREATE TABLE Employee (


E_id INT PRIMARY KEY,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
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, 60000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (3, 'Alice
Johnson', 35, 70000.00);

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

CREATE TABLE N_RollCall (


column1 INT,
column2 VARCHAR(50),
-- Add more columns as needed
);
CREATE TABLE O_RollCall (
column1 INT,
column2 VARCHAR(50),
-- Add more columns as needed
);
DELIMITER //
CREATE PROCEDURE MergeRollCallData()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE v_column1 INT; -- Declare variables for each column
DECLARE v_column2 VARCHAR(50); -- Adjust the data type and length
-- Declare cursor for selecting data from N_RollCall
DECLARE new_roll_call_cur CURSOR FOR
SELECT column1, column2 FROM N_RollCall; -- Adjust column names
-- Declare handlers for exceptions
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN new_roll_call_cur;
read_loop: LOOP
FETCH new_roll_call_cur INTO v_column1, v_column2;
-- Assign variables for each column
IF done THEN
LEAVE read_loop;
END IF;
-- Check if record already exists in O_RollCall
IF NOT EXISTS (
SELECT * FROM O_RollCall
WHERE column1 = v_column1 AND column2 = v_column2
-- Replace with actual column names
) THEN
-- If record doesn't exist, insert into O_RollCall
INSERT INTO O_RollCall VALUES (v_column1, v_column2);
-- Replace with actual column names
END IF;
END LOOP;
CLOSE new_roll_call_cur;
SELECT 'Data merge complete.';
END//
DELIMITER ;
7. Install an Open Source NoSQL Data base MangoDB & perform basic CRUD(Create, Read,
Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.

Steps to Install MongoDB Compass on Windows


Follow these easy steps to download and install MongoDB Compass on Windows:

Step 1: Firstly go MongoDb website and download MongoDB Compass.

Step 2: Unzip File after downloading.


Step 3: Double click the installer icon.

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.

You might also like