DBMS Manual
DBMS Manual
Solution:
Verify the structure of this newly created Employee table using the DESC command.
mysql> DESC COMPANY.Employee;
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Now logout and login with the new account credentials. Press Ctrl+D to logout.
Command to login with new user account is shown below.
Now we have successfully logged with our 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.
mysql> USE COMPANY;
Database changed
mysql> INSERT INTO Employee VALUES (1, Suma HC', 'Manager', NULL, 5000.00,
1000.00);
mysql> COMMIT;
mysql> INSERT INTO VALUES (2, 'Riya Singh’, 'Developer', 1, 4000.00, NULL);
mysql> INSERT INTO Employee VALUES (3, 'Hema Shree', 'Salesperson', 2, 3000.00,
500.00);
ADDING CONSTRAINTS
ADD PRIMARY KEY CONSTRAINT
mysql> INSERT INTO Employee VALUES (1, 'Ranjan', 'Manager', NULL, 5000.00,
1000.00);
mysql> INSERT INTO Employee VALUES (4, 'Ranjan', 'Manager', NULL, 5000.00,
1000.00);
mysql>
mysql> SELECT * FROM Employee;
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.
Solution
Creating the Employee Table
Solution
1. Creating the Employee Table
In these queries:
• 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.
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.
-- INSERT TRIGGER
DELIMITER //
DELIMITER ;
-- UPDATE TRIGGER
DELIMITER //
DELIMITER ;
-- DELETE TRIGGER
DELIMITER //
DELIMITER ;
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)
USE COMPANY05;
DELIMITER //
SET @finished = 1;
DELIMITER ;
7.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.
1. Create the Tables
First, let’s create the N_RollCall and O_RollCall tables with similar structure:
USE ROLLCALL;
Let’s insert some sample data into the N_RollCall table, including records that are common
with O_RollCall:
DELIMITER //
DELIMITER ;
• The stored procedure merge_rollcall_data uses a cursor (n_cursor) to iterate through
the records of the N_RollCall table.
• Inside the cursor loop (cursor_loop), each record (n_id, n_name, n_date)
from N_RollCall is fetched and checked against the O_RollCall table.
• If the record does not already exist in O_RollCall (checked using NOT EXISTS), it is
inserted into O_RollCall.
• The cursor loop continues until all records from N_RollCall have been processed.
• The cursor is then closed (CLOSE n_cursor).
Please refer to the blog below which contains detailed procedure of installing Open Source
NoSQL Data base MongoDB.
https://moodle.sit.ac.in/blog/setting-up-mongodb-on-ubuntu/
Mongosh
bookDB> db.createCollection("ProgrammingBooks")
This command will create an empty ProgrammingBooks collection in the current database
(bookDB).
5. INSERT operations
a. Insert 5 Documents into the ProgrammingBooks Collection :
Now, insert 5 documents representing programming books into
the ProgrammingBooks collection using the insertMany() method:
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
})
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 } ]
b. Find Documents Matching a Condition
7. Update Operations
a. Update a Single Document
To update a specific book (e.g., change the author of a book):
bookDB>db.ProgrammingBooks.updateOne(
{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },
{ $set: { author: "Robert C. Martin (Uncle Bob)" } }
)
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'),
8. Delete Operations
a. Delete a Single Document
To delete a specific book from the collection (e.g., delete a book by title):
You can check whether the specified document is deleted by displaying the contents of the
collection.
b. Delete Multiple Documents
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.
c. Delete All Documents in the Collection:
To delete all documents in a collection (e.g., ProgrammingBooks), use
the deleteMany() method with an empty filter {}:
To delete a collection named ProgrammingBooks, use the drop() method with the name of
the collection:
bookDB> db.ProgrammingBooks.drop()
true
bookDB>
bookDB> db.ProgrammingBooks.drop()
true
bookDB>