Dbms Lab Manual
Dbms Lab Manual
Dbms Lab Manual
Lab Manual
1|Page
Question 1
Create a table called Employee & execute the following.
Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION)
Solution
Lets login with the root account as shown below. Create a database COMPANY and
switch to it using the USE command.
+-------------------+
| Tables_in_COMPANY |
+-------------------+
| Employee |
+-------------------+
1 row in set (0.00 sec)
You can verify the structure of this newly created Employee table using the DESC ap.
2|Page
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| EMPNO | int | YES | | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MANAGER_NO | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
| COMMISSION | decimal(10,2) | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
Now logout and login with the new account credentials. Press Ctrl+D to logout.
Command to login with new user account is shown below.
$ mysql -u dbuser -p
Enter password:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Now you have successfully logged with your new account. Change the current
database to COMPANY database using USE command. Now we will illustrate how
to insert records and also the COMMIT and ROLLBACK facilities.
Database changed
-- START A TRANSACTION
mysql> START TRANSACTION;
3|Page
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
-> VALUES (1, 'Kavana Shetty', 'Manager', NULL, 5000.00, 1000.00);
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
VALUES (3, 'Honey Singh', 'Salesperson', 2, 3000.00, 500.00);
4|Page
mysql> SELECT * FROM Employee;
+-------+---------------+---------+------------+---------+------------+
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+-------+---------------+---------+------------+---------+------------+
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00 |
+-------+---------------+---------+------------+---------+------------+
1 row in set (0.00 sec)
You can now see how the rollback operation can be used above.
Adding Constraints
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
-> VALUES (1, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);
ERROR 1062 (23000): Duplicate entry '1' for key 'Employee.PRIMARY'
Since EMPNO field is the primary key it cannot have duplicate values, hence we see
that the insert operation fails when provided with a duplicate value.
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
-> VALUES (4, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);
Query OK, 1 row affected (0.16 sec)
5|Page
mysql>
mysql> SELECT * FROM Employee;
+-------+---------------+---------+------------+---------+------------+
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+-------+---------------+---------+------------+---------+------------+
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00 |
| 4 | Ranjan | Manager | NULL | 5000.00 | 1000.00 |
+-------+---------------+---------+------------+---------+------------+
2 rows in set (0.00 sec)
We just illustrated as to how to add not null constraint to the Employee table. We
see that the first insert doesn’t violate null constraint, however the second insert does
violate null constraint as ENAME field cannot be null.
Question 2
Create a table called Employee that contain attributes EMPNO,ENAME,JOB, MGR,SAL
&
execute the following.
Solution
Creating the Employee Table
mysql> CREATE DATABASE COMPANY02;
Query OK, 1 row affected (0.16 sec)
6|Page
+---------------------+
| Employee |
+---------------------+
1 row in set (0.00 sec)
7|Page
| 103 | Abdul Sattar | Salesperson | 102 | 3000.00 | 500.00 |
| 104 | Bob Johnson | Accountant | 101 | 4500.00 | NULL |
| 105 | Amartya Sen | HR Manager | 101 | 4800.00 | 800.00 |
+-------+---------------+-------------+------+---------+------------+
5 rows in set (0.00 sec)
8|Page
| 101 | Radha Bai | Manager | NULL | 5000.00 | 1000.00 |
| 102 | Krishna Kumar | Senior Developer | 101 | 4000.00 | NULL |
| 103 | Abdul Sattar | Salesperson | 102 | 3000.00 | 500.00 |
| 104 | Bob Johnson | Accountant | 101 | 4500.00 | NULL |
+-------+---------------+------------------+------------+---------+------------+
4 rows in set (0.00 sec)
Question 3
Queries using aggregate functions(COUNT,AVG,MIN,MAX,SUM),Group by,Orderby.
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.
Solution
1. Creating the Employee Table
mysql> CREATE DATABASE COMPANY03;
Query OK, 1 row affected (0.09 sec)
9|Page
4 rows in set (0.00 sec)
10 | P a g e
1 row in set (0.01 sec)
In these queries:
11 | P a g e
COUNT(E_name) counts the number of non-NULL values in the E_name column.
MAX(Age) finds the maximum age among the employees.
MIN(Age) finds the minimum age among the employees.
ORDER BY Salary ASC sorts the employees based on their salaries in ascending
order.
GROUP BY Salary groups employees by their salaries and counts the number of
employees for each salary.
Question 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
1. Create the CUSTOMERS Table
First, create the CUSTOMERS table with the specified columns:
12 | P a g e
2. Create Trigger for INSERT Operation
-- INSERT TRIGGER
DELIMITER //
DELIMITER ;
DELIMITER ;
DELIMITER ;
For example:
13 | P a g e
mysql>
mysql> SELECT @my_sal_diff AS SAL_DIFF;
+-----------------------------+
| SAL_DIFF |
+-----------------------------+
| salary inserted is 50000.00 |
+-----------------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT @my_sal_diff AS SAL_DIFF;
+----------------------------+
| SAL_DIFF |
+----------------------------+
| salary deleted is 55000.00 |
+----------------------------+
1 row in set (0.00 sec)
Each operation (INSERT, UPDATE, DELETE) will trigger the respective trigger
(after_insert_salary_difference, after_update_salary_difference, after_delete_sa
lary_difference), which will display the salary change or difference associated with
that operation.
By using separate triggers for each operation and utilizing the OLD and NEW keywords
appropriately within the trigger bodies, you can effectively capture and handle
changes to the SALARY column in the CUSTOMERS table in MySQL. You can adjust the
trigger logic and message formatting as needed based on your specific
requirements.
14 | P a g e
Question 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.
CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)
Solution
1. Creating the Employee Table and insert few records
CREATE DATABASE COMPANY05;
USE COMPANY05;
DELIMITER //
15 | P a g e
OPEN emp_cursor;
DELIMITER ;
16 | P a g e
+----------------------------------------------------------+
1 row in set (0.07 sec)
+---------------------------------------------------------------+
| Employee_Info |
+---------------------------------------------------------------+
| Employee ID: 2, Name: Ramesh Kumar, Age: 25, Salary: 45000.00 |
+---------------------------------------------------------------+
1 row in set (0.07 sec)
+-------------------------------------------------------------+
| Employee_Info |
+-------------------------------------------------------------+
| Employee ID: 3, Name: Seema Banu, Age: 35, Salary: 62000.00 |
+-------------------------------------------------------------+
1 row in set (0.07 sec)
+--------------------------------------------------------------+
| Employee_Info |
+--------------------------------------------------------------+
| Employee ID: 4, Name: Dennis Anil, Age: 28, Salary: 52000.00 |
+--------------------------------------------------------------+
1 row in set (0.07 sec)
+--------------------------------------------------------------+
| Employee_Info |
+--------------------------------------------------------------+
| Employee ID: 5, Name: Rehman Khan, Age: 32, Salary: 58000.00 |
+--------------------------------------------------------------+
1 row in set (0.07 sec)
This example demonstrates how to create and use a cursor in MySQL to extract
values from the Employee table row by row. Adjust the cursor query and processing
logic based on your table structure and desired operations.
17 | P a g e
Question 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
To accomplish this task in MySQL, we can use a stored procedure with a
parameterized cursor to merge data from one table ( N_RollCall) into another table
(O_RollCall) while skipping existing data. We’ll iterate through the records
of N_RollCall and insert them into O_RollCall only if they do not already exist.
USE ROLLCALL;
Let’s insert some sample data into the N_RollCall table, including records that are
common with O_RollCall:
18 | P a g e
mysql> -- Insert sample records into N_RollCall
mysql> INSERT INTO N_RollCall (student_id, student_name, birth_date)
-> VALUES
-> (1, 'Shivanna', '1995-08-15'), -- Common record with O_RollCall
-> (2, 'Bhadramma', '1998-03-22'),
-> (3, 'Cheluva', '1990-12-10'), -- Common record with O_RollCall
-> (4, 'Devendra', '2000-05-18'),
-> (5, 'Eshwar', '1997-09-03');
Query OK, 5 rows affected (0.21 sec)
Records: 5 Duplicates: 0 Warnings: 0
DELIMITER //
19 | P a g e
END LOOP;
DELIMITER ;
20 | P a g e
Question 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
1. Installing Open Source NoSQL Data base MongoDB
Please refer to the blog below which contains detailed procedure of installing Open
Source NoSQL Data base MongoDB.
1. Start MongoDB.
mongosh
bookDB> db.createCollection("ProgrammingBooks")
21 | P a g e
5. INSERT operations
bookDB> db.ProgrammingBooks.insertMany([
{
title: "Clean Code: A Handbook of Agile Software Craftsmanship",
author: "Robert C. Martin",
category: "Software Development",
year: 2008
},
{
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
category: "JavaScript",
year: 2008
},
{
title: "Design Patterns: Elements of Reusable Object-Oriented Software",
author: "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides",
category: "Software Design",
year: 1994
},
{
title: "Introduction to Algorithms",
author: "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein",
category: "Algorithms",
year: 1990
},
{
title: "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
author: "Eric Matthes",
category: "Python",
year: 2015
}
])
bookDB> db.ProgrammingBooks.insertOne({
title: "The Pragmatic Programmer: Your Journey to Mastery",
author: "David Thomas, Andrew Hunt",
category: "Software Development",
year: 1999
})
22 | P a g e
a. Find All Documents
bookDB> db.ProgrammingBooks.find().pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Software Design',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Algorithms',
year: 1990
},
{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
},
{
_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Software Development',
year: 1999
}
]
23 | P a g e
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
}
]
7. Update Operations
bookDB>db.ProgrammingBooks.updateOne(
{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },
{ $set: { author: "Robert C. Martin (Uncle Bob)" } }
)
24 | P a g e
To update multiple books (e.g., update the category of books published before
2010):
bookDB> db.ProgrammingBooks.updateMany(
{ year: { $lt: 2010 } },
{ $set: { category: "Classic Programming Books" } }
)
//verify the update operation by displaying books published before year 2010
bookDB> db.ProgrammingBooks.find({ year: { $lt: 2010 } }).pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin (Uncle Bob)',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Classic Programming Books',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Classic Programming Books',
year: 1990
},
{
_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Classic Programming Books',
year: 1999
}
]
8. Delete Operations
To delete a specific book from the collection (e.g., delete a book by title):
25 | P a g e
{ acknowledged: true, deletedCount: 1 }
You can check whether the specified document is deleted by displaying the contents
of the collection.
To delete multiple books based on a condition (e.g., delete all books published
before 1995):
You can check whether the specified documents were deleted by displaying the
contents of the collection.
To delete a collection named ProgrammingBooks , use the drop() method with the name
of the collection:
bookDB> db.ProgrammingBooks.drop()
true
bookDB>
After deleting the collection, you can verify that it no longer exists by listing all
collections in the database using the command show collections.
26 | P a g e
27 | P a g e