Database Management System Lab Component (BCS403) - MyBlogosphere
Database Management System Lab Component (BCS403) - MyBlogosphere
MYBLOGOSPHERE
Sharing my Inner Ramblings
In this blog post, you will find solutions for the Database Management
System Lab Component (BCS403) course work for the IV semester of VTU
university. To follow along, you will need to have up a machine running
any flavour of GNULinux OS. We recommend using the MySQL database
for this lab. The solutions have been tested on Ubuntu 22.04 OS. You can
find the lab syllabus on the university’s website or here below.
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 1/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
4 / 4
BCS403 Download
All these solutions have been maintained at the following git repository
shown below. If you want to contribute send me a PR.
https://gitlab.com/lab_manuals/current/iv-
semester/bcs403_database_management_system_lab_component
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 2/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
Question 1
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.
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 command.
+------------+---------------+------+-----+---------+-------+
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 3/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
| 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
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;
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 4/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
mysql> COMMIT;
You can now see how the rollback operation can be used above.
Adding Constraints
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 5/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
9 | Field | Type | Null | Key | Default | Extr
10 +------------+---------------+------+-----+---------+-----
11 | EMPNO | int | NO | PRI | NULL |
12 | ENAME | varchar(255) | YES | | NULL |
13 | JOB | varchar(255) | YES | | NULL |
14 | MANAGER_NO | int | YES | | NULL |
15 | SAL | decimal(10,2) | YES | | NULL |
16 | COMMISSION | decimal(10,2) | YES | | NULL |
17 +------------+---------------+------+-----+---------+-----
18 6 rows in set (0.00 sec)
19
20 mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO
21 -> VALUES (1, 'Ranjan', 'Manager', NULL, 5000.00, 1000
22 ERROR 1062 (23000): Duplicate entry '1' for key 'Employee.
23
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>
mysql> SELECT * FROM Employee;
+-------+---------------+---------+------------+---------+---
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | CO
+-------+---------------+---------+------------+---------+---
| 1 | Kavana Shetty | Manager | NULL | 5000.00 |
| 4 | Ranjan | Manager | NULL | 5000.00 |
+-------+---------------+---------+------------+---------+---
2 rows in set (0.00 sec)
Question 2
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 6/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
Solution
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 7/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COM
-> VALUES
-> (101, 'Radha Bai', 'Manager', NULL, 5000.00, 1000.
-> (102, 'Krishna Kumar', 'Developer', 101, 4000.00,
-> (103, 'Abdul Sattar', 'Salesperson', 102, 3000.00,
-> (104, 'Bob Johnson', 'Accountant', 101, 4500.00, N
-> (105, 'Amartya Sen', 'HR Manager', 101, 4800.00, 8
Query OK, 5 rows affected (0.12 sec)
Records: 5 Duplicates: 0 Warnings: 0
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 8/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
6 rows in set (0.00 sec)
Question 3
Solution
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 9/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| E_id | int | NO | PRI | NULL | |
| E_name | varchar(255) | YES | | NULL | |
| Age | int | YES | | NULL | |
| Salary | decimal(10,2) | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 10/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
+--------+
| 40 |
+--------+
1 row in set (0.01 sec)
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 11/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
In these queries:
`E_name` column.
ascending order.
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
-- INSERT TRIGGER
DELIMITER //
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 12/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
CREATE TRIGGER after_insert_salary_difference
AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @my_sal_diff = CONCAT('salary inserted is ', NEW.SALARY
END;//
DELIMITER ;
-- UPDATE TRIGGER
DELIMITER //
DELIMITER ;
-- DELETE TRIGGER
DELIMITER //
DELIMITER ;
Once the triggers are created, you can perform `INSERT`, `UPDATE`, or
`DELETE` operations on the `CUSTOMERS` table to observe the salary
For example:
mysql>
mysql> SELECT @my_sal_diff AS SAL_DIFF;
+-----------------------------+
| SAL_DIFF |
+-----------------------------+
| salary inserted is 50000.00 |
+-----------------------------+
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 13/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
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)
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
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
USE COMPANY05;
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 14/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
Age INT,
Salary DECIMAL(10, 2)
);
To create a cursor for the `Employee` table, extract values using the cursor,
and then close the cursor in MySQL, you’ll need to use stored procedures
that support cursor operations.
DELIMITER //
DELIMITER ;
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 15/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
We fetch values into the variables and process them within the loop
(for demonstration, we print the values using a `SELECT` statement).
The loop continues until all rows are fetched (`@finished = 1`).
+------------------------------------------------------------
| Employee_Info
+------------------------------------------------------------
| Employee ID: 2, Name: Ramesh Kumar, Age: 25, Salary: 45000.
+------------------------------------------------------------
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.0
+------------------------------------------------------------
1 row in set (0.07 sec)
+------------------------------------------------------------
| Employee_Info
+------------------------------------------------------------
| Employee ID: 5, Name: Rehman Khan, Age: 32, Salary: 58000.0
+------------------------------------------------------------
1 row in set (0.07 sec)
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 16/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
Within the loop, you can perform operations or output the values of
each row.
The `CLOSE` statement closes the cursor after processing all rows.
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
First, let’s create the `N_RollCall` and `O_RollCall` tables with similar
structure:
USE ROLLCALL;
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 17/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
-> (1, 'Shivanna', '1995-08-15'),
-> (3, 'Cheluva', '1990-12-10');
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0
Let’s insert some sample data into the `N_RollCall` table, including
records that are common with `O_RollCall`:
DELIMITER //
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 18/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
DELIMITER ;
`O_RollCall` table.
The cursor loop continues until all records from `N_RollCall` have
been processed.
After executing the procedure, verify the records in the `O_RollCall` table
to confirm that new records from `N_RollCall` have been inserted, while
existing common records have been skipped:
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.
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 19/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
Solution
1. Start MongoDB.
mongosh
If you want to use a specific database, switch to that database using the
`use` command. If the database doesn’t exist, MongoDB will create it
bookDB> db.createCollection("ProgrammingBooks")
5. INSERT operations
bookDB> db.ProgrammingBooks.insertMany([
{
title: "Clean Code: A Handbook of Agile Software Craftsma
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 20/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
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-Orie
author: "Erich Gamma, Richard Helm, Ralph Johnson, John V
category: "Software Design",
year: 1994
},
{
title: "Introduction to Algorithms",
author: "Thomas H. Cormen, Charles E. Leiserson, Ronald L
category: "Algorithms",
year: 1990
},
{
title: "Python Crash Course: A Hands-On, Project-Based In
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
})
bookDB> db.ProgrammingBooks.find().pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsma
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-Orie
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John V
category: 'Software Design',
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 21/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L
category: 'Algorithms',
year: 1990
},
{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based In
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
}
]
7. Update Operations
bookDB>db.ProgrammingBooks.updateOne(
{ title: "Clean Code: A Handbook of Agile Software Craftsma
{ $set: { author: "Robert C. Martin (Uncle Bob)" } }
)
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 22/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsma
author: 'Robert C. Martin (Uncle Bob)',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
}
]
bookDB> db.ProgrammingBooks.updateMany(
{ year: { $lt: 2010 } },
{ $set: { category: "Classic Programming Books" } }
)
8. Delete Operations
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 23/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
To delete a specific book from the collection (e.g., delete a book by title):
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`.
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 24/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
If you are also looking for other Lab Manuals, head over to my following
blog :
Prabodh C P
Prabodh C P is a faculty in the Dept of CSE SIT, Tumkur and also currently
a Research Scholar pursuing PhD in IIT Hyderabad. He conducts online
classes for C, C++, Python. For more info call +919392302100
PREVIOUS POST
NEXT POST
RELATED POSTS
SYSTEM PROGRAMMING LABORATORY In this blog post, you will find ...
In this blog post, you will find solutions for the ...
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 25/26
6/7/24, 9:08 PM Database Management System Lab Component (BCS403) - MyBlogosphere
Artificial Intelligence Lab Component (BAD402)
In this blog post, you will find solutions for the ...
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Comment*
Name*
Email*
Website
Post Comment
https://moodle.sit.ac.in/blog/database-management-system-lab-component-bcs403/ 26/26