0% found this document useful (0 votes)
29 views14 pages

LAB-3 Employee (E - Id, E - Name, Age, Salary)

The document outlines various labs focusing on SQL and PL/SQL operations, including creating tables, performing aggregate functions, triggers, cursors, and merging data between tables. It also covers installing and performing CRUD operations in MongoDB, detailing steps for setup and examples of database interactions. Each lab provides specific SQL commands and PL/SQL blocks to demonstrate the concepts discussed.

Uploaded by

samhithap2005
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)
29 views14 pages

LAB-3 Employee (E - Id, E - Name, Age, Salary)

The document outlines various labs focusing on SQL and PL/SQL operations, including creating tables, performing aggregate functions, triggers, cursors, and merging data between tables. It also covers installing and performing CRUD operations in MongoDB, detailing steps for setup and examples of database interactions. Each lab provides specific SQL commands and PL/SQL blocks to demonstrate the concepts discussed.

Uploaded by

samhithap2005
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/ 14

LAB-3

Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM),


Group by, and Order by. 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 TABLE Employee (


E_id INT PRIMARY KEY,
E_name VARCHAR(100),
Age INT,
Salary DECIMAL(10, 2)
);

Desc Employee;
Insert the values to Employee table;
INSERT INTO Employee VALUES(1, 'John Doe', 30, 50000);
INSERT INTO Employee VALUES(2, 'Jane Smith', 25, 60000);
INSERT INTO Employee VALUES(3, 'Alice Johnson', 35, 55000);
INSERT INTO Employee VALUES(4, 'Chris Lee', 28, 70000);
INSERT INTO Employee VALUES(5, 'Eve Brown', 22, 45000);

2. Count number of employee names from employee table


SELECT COUNT(Ename) AS NumberOfEmployees
FROM Employee;
select * from employee;
3. Find the Maximum age from employee table.
SELECT MAX(Age) AS MaxAge FROM Employee;
4. Find the Minimum age from employee table.
SELECT MIN(Age) AS MinAge FROM Employee;
5. Find salaries of employee in Ascending Order.
SELECT Salary FROM Employee ORDER BY Salary ASC;
6. Find grouped salaries of employees.
SELECT Salary, COUNT(*) AS NumberOfEmployees
FROM Employee
GROUP BY Salary;
LAB-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 TABLE CUSTOMERS (
ID INT PRIMARY KEY,
NAME VARCHAR(100),
AGE INT,
ADDRESS VARCHAR(255),
SALARY DECIMAL(10, 2)
);

-- Create the trigger


CREATE OR REPLACE TRIGGER salary_difference_trigger
AFTER INSERT OR UPDATE OR DELETE ON CUSTOMERS
FOR EACH ROW
DECLARE
old_salary DECIMAL(10, 2);
new_salary DECIMAL(10, 2);
salary_diff DECIMAL(10, 2);
BEGIN
-- Initialize old_salary and new_salary based on the type of operation
IF INSERTING THEN
old_salary := 0;
new_salary := :NEW.SALARY;
ELSIF UPDATING THEN
old_salary := :OLD.SALARY;
new_salary := :NEW.SALARY;
ELSIF DELETING THEN
old_salary := :OLD.SALARY;
new_salary := 0;
END IF;

-- Calculate the salary difference


salary_diff := new_salary - old_salary;

-- Display the salary difference


DBMS_OUTPUT.PUT_LINE('Salary difference: ' || salary_diff);
END;
/
-- Enable server output
SET SERVEROUTPUT ON;

-- Perform operations to test the trigger


-- Insert a new record
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS,
SALARY) VALUES (1, 'John Doe', 30, '123 Elm St', 50000);

-- Update the salary of the customer


UPDATE CUSTOMERS SET SALARY = 55000 WHERE ID = 1;

-- Delete the customer record


DELETE FROM CUSTOMERS WHERE ID = 1;

LAB-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)

CREATE TABLE Employee (


E_id INT PRIMARY KEY,
E_name VARCHAR(50) NOT NULL,
Age INT NOT NULL,
Salary DECIMAL(10, 2) NOT NULL
);

-- Insert some sample data into the Employee table


INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (1,
'Alice', 30, 60000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (2,
'Bob', 25, 50000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (3,
'Charlie', 35, 70000);
DECLARE
-- Declare the cursor
CURSOR emp_cursor IS
SELECT E_id, E_name, Age, Salary FROM Employee;

-- Declare variables to hold the values fetched from the cursor


v_E_id Employee.E_id%TYPE;
v_E_name Employee.E_name%TYPE;
v_Age Employee.Age%TYPE;
v_Salary Employee.Salary%TYPE;
BEGIN
-- Open the cursor
OPEN emp_cursor;

-- Loop to fetch each row from the cursor


LOOP
-- Fetch the values into the variables
FETCH emp_cursor INTO v_E_id, v_E_name, v_Age, v_Salary;

-- Exit the loop when no more rows are returned


EXIT WHEN emp_cursor%NOTFOUND;
-- Display the fetched values
DBMS_OUTPUT.PUT_LINE('E_id: ' || v_E_id || ', E_name: ' ||
v_E_name || ', Age: ' || v_Age || ', Salary: ' || v_Salary);
END LOOP;

-- Close the cursor


CLOSE emp_cursor;
END;
/
SET SERVEROUTPUT ON;

LAB-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 TABLE N_RollCall (


ID INT PRIMARY KEY,
Name VARCHAR(50),
AttendanceDate DATE
);

CREATE TABLE O_RollCall (


ID INT PRIMARY KEY,
Name VARCHAR(50),
AttendanceDate DATE
);
INSERT INTO N_RollCall (ID, Name, AttendanceDate) VALUES (1,
'Alice', TO_DATE('2024-01-01', 'YYYY-MM-DD'));

INSERT INTO N_RollCall VALUES (2, 'bob', TO_DATE('2024-02-


01','YYYY-MM-DD'));
SELECT * FROM n_ROLLCALL;
INSERT INTO N_RollCall (ID, Name, AttendanceDate) VALUES (3,
'JOHN', TO_DATE('2024-03-05','YYYY-MM-DD'));

INSERT INTO O_RollCall (ID, Name, AttendanceDate) VALUES (1,


'Alice', TO_DATE('2024-01-01', 'YYYY-MM-DD'));
INSERT INTO O_RollCall (ID, Name, AttendanceDate) VALUES (4,
'David', TO_DATE('2024-01-04', 'YYYY-MM-DD'));
INSERT INTO O_RollCall (ID, Name, AttendanceDate) VALUES (5,
'Eve', TO_DATE('2024-01-05', 'YYYY-MM-DD'));

DECLARE
-- Parameterized cursor to select rows from N_RollCall
CURSOR n_rollcall_cursor IS
SELECT ID, Name, AttendanceDate FROM N_RollCall;
-- Variables to hold the fetched values
v_ID N_RollCall.ID%TYPE;
v_Name N_RollCall.Name%TYPE;
v_AttendanceDate N_RollCall.AttendanceDate%TYPE;
BEGIN
-- Loop through each row in N_RollCall
OPEN n_rollcall_cursor;
LOOP
-- Fetch the data into variables
FETCH n_rollcall_cursor INTO v_ID, v_Name,
v_AttendanceDate;
-- Exit the loop when no more rows are returned
EXIT WHEN n_rollcall_cursor%NOTFOUND;
-- Check if the data already exists in O_RollCall
BEGIN
-- Attempt to insert the data into O_RollCall
INSERT INTO O_RollCall (ID, Name, AttendanceDate)
VALUES (v_ID, v_Name, v_AttendanceDate);
EXCEPTION
-- Handle the case where the ID already exists in O_RollCall
WHEN DUP_VAL_ON_INDEX THEN
NULL; -- Do nothing, skip the duplicate record
END;
END LOOP;
CLOSE n_rollcall_cursor;
-- Commit the transaction to save the changes
COMMIT;
-- Output message
DBMS_OUTPUT.PUT_LINE('Data merge completed successfully.');
END;
/

-- Enable DBMS_OUTPUT to see the output


SET SERVEROUTPUT ON;

SELECT * FROM O_ROLLCALL;


SELECT * FROM n_ROLLCALL;

LAB-7
To install and perform basic CRUD (Create, Read, Update, Delete) operations on an open-source
NoSQL database like MongoDB, follow these steps.

Examples of basic CRUD operations using MongoDB.

Sure, I'll guide you through installing MongoDB on Windows and performing basic CRUD
operations. MongoDB is a popular NoSQL database. Here's how you can get started:

Step 1: Install MongoDB on Windows

1. Download MongoDB:
o Go to the MongoDB download page.
o Select your operating system as Windows.
o Click on the "Download" button.

2. Install MongoDB:
o Run the downloaded .msi installer.
o Follow the installation wizard. Make sure to select "Complete" installation.
o When prompted, choose to install MongoDB as a service. This way, MongoDB will start
automatically with your system.

3. Set Up the Data Directory:


o By default, MongoDB will use C:\data\db as the data directory. Create this directory if
it doesn’t exist:

sh
Copy code
mkdir C:\data\db

4. Start MongoDB:
o If MongoDB is installed as a service, it should start automatically. If not, you can start it
manually by running:

sh
Copy code
"C:\Program Files\MongoDB\Server\5.0\bin\mongod.exe"

5. Install MongoDB Compass (Optional):


o MongoDB Compass is a graphical interface for MongoDB. It can be downloaded from
the MongoDB website.
o Follow the installation instructions.

Step 2: Perform Basic CRUD Operations

1. Connect to MongoDB:
o Open a command prompt and start the MongoDB shell by running:

sh
Copy code
"C:\Program Files\MongoDB\Server\5.0\bin\mongo.exe"

2. Create a Database:
o Switch to a new or existing database:

sh
Copy code
use mydatabase

3. Create a Collection and Insert Documents:

js
Copy code
db.mycollection.insertOne({ name: "Alice", age: 25, city: "New York" });
db.mycollection.insertMany([
{ name: "Bob", age: 30, city: "San Francisco" },
{ name: "Charlie", age: 35, city: "Los Angeles" }
]);

4. Read Documents:
o Find all documents in a collection:

js
Copy code
db.mycollection.find().pretty();

o Find specific documents:

js
Copy code
db.mycollection.find({ name: "Alice" }).pretty();

5. Update Documents:
o Update a single document:

js
Copy code
db.mycollection.updateOne({ name: "Alice" }, { $set: { age:
26 } });

o Update multiple documents:

js
Copy code
db.mycollection.updateMany({ city: "San Francisco" }, { $set: {
city: "SF" } });

6. Delete Documents:
o Delete a single document:

js
Copy code
db.mycollection.deleteOne({ name: "Alice" });

o Delete multiple documents:

js
Copy code
db.mycollection.deleteMany({ city: "Los Angeles" });

MongoDB CRUD Operations Example

Here's a more comprehensive example demonstrating these CRUD operations:

js
Copy code
// Create a new database and collection
use companyDB
// Insert documents into the collection
db.employees.insertMany([
{ empID: 1, name: "John Doe", position: "Manager", salary: 70000 },
{ empID: 2, name: "Jane Smith", position: "Developer", salary: 60000 },
{ empID: 3, name: "Sam Brown", position: "Developer", salary: 60000 },
{ empID: 4, name: "Sara Wilson", position: "Designer", salary: 50000 }
]);

// Read documents from the collection


// Find all employees
db.employees.find().pretty();

// Find employees with position 'Developer'


db.employees.find({ position: "Developer" }).pretty();

// Update documents in the collection


// Update a single employee's salary
db.employees.updateOne({ empID: 1 }, { $set: { salary: 75000 } });

// Update all developers' salaries


db.employees.updateMany({ position: "Developer" }, { $set: { salary:
65000 } });

// Delete documents from the collection


// Delete a single employee
db.employees.deleteOne({ empID: 4 });

// Delete all employees with salary 65000


db.employees.deleteMany({ salary: 65000 });

Running MongoDB Compass (Optional)

1. Open MongoDB Compass:


o After installing, open MongoDB Compass.

2. Connect to the Database:


o By default, Compass will connect to mongodb://localhost:27017. Just click
"Connect".

3. Perform CRUD Operations:


o Use the graphical interface to interact with your databases and collections.
o You can insert, update, delete, and find documents using the GUI.

This should give you a good start on using MongoDB on Windows and performing basic CRUD
operations.

You might also like