DBMS Lab Manual (BCS403)
DBMS Lab Manual (BCS403)
SYSTEM[DBMS]
LABORATORY MANUAL
[BCS403-Integrated]
B.E
(II YEAR-2022 scheme – IV SEM)
(2023‐2024)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING in AIML
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & MACHINE LEARNING
Solution Experiment 1:
Create a table called Employee & execute the following.
Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
CREATE TABLE Employee (
EMPNO INT,
ENAME VARCHAR(100),
JOB VARCHAR(50),
MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2),
);
Question 1
1. Create a user and grant all permissions to the user.
Solution
For MySQL:
Replace 'newuser' with the desired username and 'password' with a strong password for the user.
This command grants all privileges on all databases and tables ( *.*) to the new user. The
WITH GRANT OPTION allows the new user to grant permissions to other users.
For Oracle:
Replace newuser with the desired username and password with a strong password for the user.
Question 2:
2. Insert the any three records in the employee table contains attributes, EMPNO,
ENAME JOB, MANAGER_NO, SAL,COMMISSION and use rollback.
Solution
To insert records into the Employee table and demonstrate the use of ROLLBACK, follow
these steps in SQL. I'll show you how to insert three records and then roll back the
transaction, which undoes the inserts.
-- Start a transaction
SQL>START TRANSACTION;
Question 3:
3. Add primary key constraint and not null constraint to the employee table
Solution:
SQL>CREATE TABLE Employee (
EMPNO INT PRIMARY KEY,
ENAME VARCHAR(50) NOT NULL,
JOB VARCHAR(50),
MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2),
FOREIGN KEY (MANAGER_NO) REFERENCES Employee(EMPNO)
);
Question 4
4. Insert null values to the employee table and verify the result.
Solution:
To insert NULL values into the Employee table for certain columns, you can simply omit those
columns from the INSERT statement, or explicitly insert NULL where needed. Here's how you
can do it:
1. Inserting Records:
For EMPNO 104, ENAME 'Alice Johnson', JOB 'Assistant', and SAL 45000
are provided. Since MANAGER_NO and COMMISSION are not provided,
they will be inserted as NULL.
For EMPNO 105, ENAME 'Bob Roberts', JOB 'Intern', and SAL 30000 are
provided. Additionally, MANAGER_NO is not provided and will default to
NULL. COMMISSION is explicitly set to NULL.
Experiment 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 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.
Question 3:
3.Update the column details of job
Solution:
SQL>UPDATE Employee
SET JOB = 'Senior Developer'
WHERE EMPNO = 2;
SQL>UPDATE Employee
SET JOB = 'Executive Assistant'
WHERE EMPNO = 4;
SQL>UPDATE Employee
SET JOB = 'Junior Developer'
WHERE EMPNO = 5;
Question 4:
4. Rename the column of Employ table using alter command.
Solution:
SQL>ALTER TABLE Employee RENAME COLUMN MGR TO
MANAGER_NO
SQL>ALTER TABLE Employee RENAME COLUMN ENAME TO
EMP_NAME;
SQL>Describe Employee;
Question 5:
5.Delete the employee whose Empno is 105.
SQL>DELETE FROM Employee WHERE EMPNO = 105;
SQL> Select * from Employee;
Experiment 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 employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
Question 1:
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
Solution:
SQL>CREATE TABLE Employee (
E_id INT PRIMARY KEY,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
SQL>INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES
(1, 'John Doe', 30, 50000.00),
(2, 'Jane Smith', 28, 60000.00),
(3, 'Alice Johnson', 35, 70000.00),
(4, 'Bob Roberts', 32, 55000.00),
(5, 'Emily Brown', 27, 48000.00);
Question 2:
2. Count number of employee names from employee table
Solution:
To count the number of employee names from the Employee table, you can use the COUNT()
function in SQL. Here's how you can do it:
If you want to count all rows regardless of whether E_name is null or not, you can use:
Question 4:
4. Find the Minimum age from employee table.
Solution:
SQL> SELECT MIN(Age) AS MinimumAge FROM Employee;
Question 5:
5. Find salaries of employee in Ascending Order.
Solution:
Ascending Order
SQL> SELECT E_id, E_name, Salary FROM Employee ORDER BY Salary
ASC;
Descending Order
SQL>SELECT E_id, E_name, Salary FROM Employee ORDER BY Salary
DESC;
Question 6:
6. Find grouped salaries of employees.
Solution:
SQL>SELECT Salary, COUNT(*) AS NumEmployees FROM Employee GROUP
BY Salary;
This SQL query will count the number of employees ( NumEmployees) for each unique salary
(Salary) value in the Employee table.
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)
Step 1
Create CUSTOMERS Table- CUSTOMERS (ID, NAME, AGE, ADDRESS,
SALARY)
SQL>CREATE TABLE CUSTOMERS (
ID INT PRIMARY KEY,
NAME VARCHAR(255),
AGE INT,
ADDRESS VARCHAR(255),
SALARY DECIMAL(10, 2)
);
SQL>DESC CUSTOMERS;
Question:
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.
Solution:
DELIMITER //
CREATE TRIGGER salary_difference_trigger
AFTER INSERT OR UPDATE OR DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
DECLARE new_salary DECIMAL(10, 2)
IF (OLD.SALARY IS NOT NULL AND NEW.SALARY IS NOT NULL) THEN
SET old_salary = OLD.SALARY;
SET new_salary = NEW.SALARY;
IF (old_salary <> new_salary) THEN
SELECT CONCAT('Salary difference: $', ABS(new_salary - old_salary))
AS 'Salary Difference'
FROM DUAL;
END IF;
END IF;
END;
//
DELIMITER ;
DELIMITER //: Changes the delimiter to // so that the entire trigger definition can
be included in a single SQL statement.
CREATE TRIGGER salary_difference_trigger : Starts the definition of the trigger
named salary_difference_trigger .
AFTER INSERT OR UPDATE OR DELETE ON CUSTOMERS : Specifies that the trigger
should fire after INSERT, UPDATE, or DELETE operations on the CUSTOMERS
table.
FOR EACH ROW: Indicates that the trigger should be executed once for each row
affected by the operation.
BEGIN ... END: Encloses the body of the trigger.
DECLARE old_salary DECIMAL(10, 2); and DECLARE new_salary DECIMAL(10, 2); :
Declares variables to store the old and new salary values.
The IF statement checks if both the old and new salary values are not NULL.
If the old and new salaries are different, it calculates the absolute difference
between them and displays the result using a SELECT statement.
FROM DUAL;: Used in MySQL to execute expressions without selecting data from
any table.
DELIMITER ;: Resets the delimiter back to semicolon.
This trigger will display the salary difference whenever a row is inserted, updated, or
deleted in the CUSTOMERS table. Adjust the data types and lengths as needed based on
your requirements.
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)
Step 1
Create Employee Table- Employee(E_id, E_name, Age, Salary)
SQL>CREATE TABLE Employee (
E_id INT PRIMARY KEY,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
Question:
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.
Solution:
-- Declare variables
SQL>DECLARE emp_id INT;
DECLARE emp_name VARCHAR(255);
DECLARE emp_age INT;
DECLARE emp_salary DECIMAL(10, 2);
here's a PL/SQL block of code using a parameterized cursor to merge data from the
N_RollCall table into the O_RollCall table:
SQL>DECLARE
v_n_id N_RollCall.ID%TYPE;
v_n_name N_RollCall.Name%TYPE;
v_n_status N_RollCall.Status%TYPE;
v_exists BOOLEAN;
CURSOR n_cursor IS
SELECT ID, Name, Status
FROM N_RollCall;
BEGIN
-- Open the cursor
OPEN n_cursor;
We declare variables to store the data retrieved from the N_RollCall table (v_n_id,
v_n_name, v_n_status), as well as a boolean variable ( v_exists) to check if the data
already exists in O_RollCall.
We define a cursor ( n_cursor) to select data from the N_RollCall table.
We open the cursor to start fetching data.
We loop through each row in the cursor.
Inside the loop, we fetch the data from the cursor into the variables.
We check if the data with the same ID already exists in the O_RollCall table. If it
does not exist ( v_exists = 0), we insert the data into the O_RollCall table.
After processing all rows, we close the cursor.
Experiment 7
Install an Open Source NoSQL Data base MangoDB & perform basic
CRUD(Create, Read,Update & Delete) operations. Execute MangoDB basic
Queries using CRUD operations.
After installing MongoDB, you need to start the MongoDB server. Here's how you can
do it:
Windows:
cd C:\Program Files\MongoDB\Server\<version>\bin
3.Run the mongod command to start the MongoDB server.
Mongo
Create (Insert):
To insert documents into a collection, you can use the insertOne() or
insertMany() method.
To check the current database you are using, you can use the db command:
db
This command will display the name of the current database.
View Collections
To view the collections in the current database, you can use the show collections
command:
show collections
This command will list all collections in the current database.
Find Documents
To view the documents in a collection, you can use the find() method:
db.collectionName.find()
Replace collectionName with the name of the collection you want to query. For example, if you
inserted documents into a collection named employees, you can use:
db.employees.find()
To query specific documents based on certain criteria, you can use the find() method
with a query condition:
db.collectionName.find({ criteria })
Replace collectionName with the name of the collection and criteria with the query condition. For
example, to find documents where the name field is "John":