0% found this document useful (0 votes)
9 views15 pages

DBMS_Lab_program

The document outlines a series of SQL and MongoDB operations, including creating databases and tables, inserting and modifying records, applying constraints, and using triggers and cursors. It also details CRUD operations in MongoDB, such as creating collections, inserting documents, updating records, and deleting data. Additionally, it includes examples of aggregate functions and PL/SQL blocks for merging data between tables.

Uploaded by

gayathri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

DBMS_Lab_program

The document outlines a series of SQL and MongoDB operations, including creating databases and tables, inserting and modifying records, applying constraints, and using triggers and cursors. It also details CRUD operations in MongoDB, such as creating collections, inserting documents, updating records, and deleting data. Additionally, it includes examples of aggregate functions and PL/SQL blocks for merging data between tables.

Uploaded by

gayathri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

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 DATABASE COMPANY;


USE COMPANY;
CREATE TABLE Employee (EMPNO INT,ENAME
VARCHAR(50),
JOB VARCHAR(50),MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2)
);
DESC Employee;
--new user
CREATE USER IF NOT EXISTS 'admin' @'localhost'
IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON Employee TO 'admin'
@'localhost';
--mysql -u admin -p type in terminal and
password is password
start transaction;
INSERT INTO Employee
VALUES (1, 'Kavana', 'Manager', NULL, 10000.00,
1000.00);
INSERT INTO Employee
VALUES (2, 'Ram Charan', 'Developer', 1,
4000.00, NULL);
commit;
INSERT INTO Employee
VALUES ( 3, 'vijay Singh',
'Salesperson', 2, 3000.00, 500.00 );
rollback;
SELECT * FROM Employee;

-- Add Primary Key Constraint


ALTER TABLE Employee
ADD CONSTRAINT pk_Employee PRIMARY KEY (EMPNO);
ALTER TABLE Employee
MODIFY ENAME VARCHAR(50) NOT NULL,
MODIFY JOB VARCHAR(50) NOT NULL,
MODIFY SAL DECIMAL(10, 2) NOT NULL;
INSERT INTO Employee VALUES (44, 'Ranjan',
'Manager', NULL, 55000.00, 1000.00);
SELECT * FROM Employee;

INSERT INTO Employee VALUES (NULL, 'Nitu',


'Tester', NULL, 3500.00, 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 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.

CREATE DATABASE CMPNY;


USE CMPNY;
CREATE TABLE Employee (
EMPNO INT,
ENAME VARCHAR(50),
JOB VARCHAR(50),
MGR INT,
SAL DECIMAL(10, 2));
SHOW TABLES;

ALTER TABLE Employee


ADD COLUMN COMMISSION DECIMAL(10, 2);
DESC Employee;

INSERT INTO Employee VALUES( 101, 'Geeta', 'Manager',


NULL,45000.00,1000.00);
INSERT INTO Employee VALUES(102, 'Krishna',
'Developer',101,80000.00, NULL );
INSERT INTO Employee VALUES(103,'Abdul', 'Sales
person',102,30000.00,500.00);
INSERT INTO Employee VALUES(104,'Rita','Accountant', 101,45000.00,
NULL );
INSERT INTO Employee VALUES(105, 'Amart','HR
Manager',101,58000.00,800.00 );

SELECT * FROM Employee;

UPDATE Employee
SET JOB = 'Senior Developer'
WHERE EMPNO = 102;

SELECT * FROM Employee;

ALTER TABLE Employee CHANGE COLUMN MGR MANAGER_ID INT;


DESC Employee;

DELETE FROM Employee


WHERE EMPNO = 105;

SELECT * FROM Employee;

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.

CREATE DATABASE COMPANY03;

USE COMPANY03;

CREATE TABLE Employee ( E_id INT PRIMARY KEY, E_name VARCHAR(255), Age INT, Salary
DECIMAL(10, 2) );

INSERT INTO Employee VALUES (101, 'Samarth', 30, 50000.00);

INSERT INTO Employee VALUES (102, 'Ramesh ', 25, 45000.00);


INSERT INTO Employee VALUES (103, 'Seema ', 35, 62000.00);

INSERT INTO Employee VALUES (104, 'Dennis Anil', 28, 52000.00);

INSERT INTO Employee VALUES (105, 'Rehman’, 32, 58000.00);

INSERT INTO Employee VALUES (106, 'Pavan Gowda', 40, 70000.00);

INSERT INTO Employee VALUES (107, 'Shruthi', 27, 48000.00);

INSERT INTO Employee VALUES (108, 'Sandesh', 29, 52000.00);

INSERT INTO Employee VALUES (109, 'Vikram ', 33, 62000.00);

INSERT INTO Employee VALUES (110, 'Praveen ', 26, 46000.00);

INSERT INTO Employee VALUES (111, 'Sophia ', 31, 55000.00);

INSERT INTO Employee VALUES (112, 'Darshan', 34, 63000.00);

SELECT COUNT(E_name) AS Total_Employees FROM Employee;

SELECT MAX(Age) AS MaxAge FROM Employee;

SELECT MIN(Age) AS MinAge FROM Employee;

SELECT E_name, Salary

FROM Employee

ORDER BY Salary ASC;

SELECT Salary, COUNT(*) AS EmployeeCount

FROM Employee

GROUP BY 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. CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)
CREATE DATABASE COMPANY04;
USE COMPANY04;
CREATE TABLE CUSTOMERS ( ID INT PRIMARY KEY, NAME VARCHAR(255),
AGE INT, ADDRESS VARCHAR(255), SALARY DECIMAL(10, 2) );

-- INSERT TRIGGER
DELIMITER //
CREATE TRIGGER after_insert
AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @ins_sal = CONCAT('Inserted salary is ', NEW.SALARY);
END; //
DELIMITER ;

-- UPDATE TRIGGER
CREATE TRIGGER after_update
AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @upd_sal = CONCAT('After updating salary difference is ', NEW.SALARY -
OLD.SALARY);
END;

-- DELETE TRIGGER
CREATE TRIGGER after_delete
AFTER DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
SET @del_sal = CONCAT('Deleted salary is ', OLD.SALARY);
END;

-- test INSERT TRIGGER


INSERT INTO CUSTOMERS VALUES (101, 'Shankara', 35, '123 Main St',
50000.00);
SELECT @ins_sal;

-- test UPDATE TRIGGER


UPDATE CUSTOMERS SET SALARY = 65000.00 WHERE ID = 101;
SELECT @upd_sal;

-- test DELETE TRIGGER


DELETE FROM CUSTOMERS WHERE ID = 101;
SELECT @del_sal;

drop trigger after_delete;

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)

MYsql : mysql -u root -p

Password : root123

CREATE DATABASE COMPANY05;

USE COMPANY05;

CREATE TABLE Employee ( E_id INT, E_name VARCHAR(255),

Age INT, Salary DECIMAL(10, 2) );

INSERT INTO Employee (E_id, E_name, Age, Salary)

VALUES (1, 'Samarth', 30, 50000.00), (2, 'Ramesh Kumar', 25, 45000.00),

(3, 'Seema Banu', 35, 62000.00), (4, 'Dennis Anil', 28, 52000.00),

(5, 'Rehman Khan', 32, 58000.00);

DELIMITER //

CREATE PROCEDURE fetch_employee_data()

BEGIN
-- Declare variables to store cursor values

DECLARE emp_id INT;

DECLARE emp_name VARCHAR(255);

DECLARE emp_age INT;

DECLARE emp_salary DECIMAL(10, 2);

-- Declare a cursor for the Employee table

DECLARE emp_cursor CURSOR FOR

SELECT E_id, E_name, Age, Salary

FROM Employee;

-- Declare a continue handler for the cursor

DECLARE CONTINUE HANDLER FOR NOT FOUND

SET @finished = 1;

OPEN emp_cursor;

-- Initialize a variable to control cursor loop

SET @finished = 0;

-- Loop through the cursor results

cursor_loop: LOOP

-- Fetch the next row from the cursor into variables

FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;

-- Check if no more rows to fetch

IF @finished = 1 THEN

LEAVE cursor_loop;

END IF;

-- Output or process each row (for demonstration, print the values)


SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ', emp_age, ', Salary: ',
emp_salary) AS Employee_Info;

END LOOP;

CLOSE emp_cursor;

END//

delimiter ;

CALL fetch_employee_data();

drop procedure fetch_employee_data;

Output:
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 DATABASE ROLLCALL1;

USE ROLLCALL1;

-- Create N_RollCall table

CREATE TABLE N_RollCall ( stud_id INT PRIMARY KEY, stud_name VARCHAR(55), b_date DATE);

-- Create O_RollCall table with common data


CREATE TABLE O_RollCall ( stud_id INT PRIMARY KEY, stud_name VARCHAR(55), b_date DATE );

-- Insert data into O_RollCall

INSERT INTO O_RollCall (stud_id, stud_name, b_date) VALUES (1, 'Shiva', '1995-08-15'),

(3, 'selva', '1990-12-10');

select * from O_RollCall;

-- Insert sample records into N_RollCall

INSERT INTO N_RollCall (stud_id, stud_name, b_date) VALUES

(1, 'Shiva', '1995-08-15'), -- Common record with O_RollCall

(2, 'Alia', '1998-03-22'),

(3, 'selva', '1990-12-10'), -- Common record with O_RollCall

(4, 'Denis', '2000-05-18'),

(5, 'Eshwar', '2005-09-03');

select * from N_RollCall;

-- merge_rollcall_data stored procedure

--DELIMITER //: changes the default SQL delimiter ";" to "//" so that MySQL does not mistakenly
execute the procedure before it is fully written.

CREATE PROCEDURE merge_rollcall_data()

BEGIN

DECLARE done INT DEFAULT FALSE;

DECLARE id INT;

DECLARE sname VARCHAR(55);

DECLARE b_date DATE;

-- Declare cursor for N_RollCall table

DECLARE stud_cursor CURSOR FOR SELECT * FROM N_RollCall;


-- Declare handler for cursor

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN stud_cursor;

cursor_loop: LOOP

-- Fetch data from cursor into variables

FETCH stud_cursor INTO id, sname, b_date;

-- Check if no more rows to fetch

IF done THEN LEAVE cursor_loop;

END IF;

-- Check if the data already exists in O_RollCall

IF NOT EXISTS ( SELECT * FROM O_RollCall WHERE stud_id = id ) THEN

-- Insert the record into O_RollCall

INSERT INTO O_RollCall (stud_id, stud_name, b_date)

VALUES (id, sname, b_date);

END IF;

END LOOP;

CLOSE stud_cursor;

END//

DELIMITER ;

CALL merge_rollcall_data();

-- Select all records from O_RollCall

SELECT * FROM O_RollCall;

drop procedure merge_rollcall_data;


7. Install an Open Source NoSQL Data base MangoDB & perform basic CRUD(Create, Read,
Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.

1. Install the MongoDB and Mongosh .

2. Open the Command Prompt and type the command “mongosh” to switch to the MongoDB
Shell

3. Create a Database with the name “MyMongoDB”

 use MyMongoDB

4. Command “show dbs” – Lists the Databases

5. Create a Collection with name Orders:

 db.createCollection("orders”)

MongoDB doesn't use the rows and columns. It stores the data in a document format. A collection is
a group of documents

Command “show collections” - Lists the collections

use bookDB

db.createCollection("ProgrammingBooks")

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

])

// Display all documents in the 'ProgrammingBooks' collection

db.ProgrammingBooks.find().pretty()

db.ProgrammingBooks.insertOne({

title: "The Pragmatic Programmer: Your Journey to Mastery",

author: "David Thomas, Andrew Hunt",

category: "Software Development",

year: 1999

})
db.ProgrammingBooks.find().pretty()

//find books published after the year 2000

db.ProgrammingBooks.find({ year: { $gt: 2000 } }).pretty()

//change the author of a book

db.ProgrammingBooks.updateOne(

{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },

{ $set: { author: "Robert C. Martin (Uncle Bob)" } }

db.ProgrammingBooks.find({ year: { $eq: 2008 } }).pretty()

//update multiple books

db.ProgrammingBooks.updateMany(

{ year: { $lt: 2010 } },

{ $set: { category: "Classic Programming Books" } }

db.ProgrammingBooks.find({ year: { $lt: 2010 } }).pretty()

//Delete a Single Document

db.ProgrammingBooks.deleteOne({ title: "JavaScript: The Good Parts" })

//delete multiple documents

db.ProgrammingBooks.deleteMany({ year: { $lt: 1995 } })

//delete multiple documents

db.ProgrammingBooks.deleteMany({})

//delete collection
db.ProgrammingBooks.drop()

You might also like