0% found this document useful (0 votes)
18 views

Bcs403 Manual

Uploaded by

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

Bcs403 Manual

Uploaded by

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

BCS403

Prog 1: Create a table called Employee & execute the following.

Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)

 Create a user and grant all permissions to the user.


 Insert the any three records in the employee table contains attributes.
EMPNO, ENAME JOB, MANAGER_NO, SAL, COMMISSION and use rollback. Check
the result.
 Add primary key constraint and not null constraint to the employee table.
 Insert null values to the employee table and verify the result.
STEP 1: Create a user and grant all permissions:

--Connect as a privileged user (e.g., SYS or SYSTEM)


CREATE USER emp_user IDENTIFIED BY password;

--Grant necessary privileges to emp_user (adjust privileges as needed)


GRANT CONNECT, RESOURCE, DBA TO emp_user;

STEP2:Create the Employee Table:

--Connect as the newly created user


CONNECT emp_user/password

--Create the Employee table


CREATE TABLE employee (
EMPNO NUMBER,
ENAME VARCHAR (50),
JOB VARCHAR2 (50),
MANAGER_NO NUMBER,
SAL NUMBER,
COMMISSION NUMBER);

STEP 3: Insert records to the Employee Table and use Rollback:

--Insert any three records into the Employee table


INSERT INTO Employee VALUES (1, ‘Raman’, ‘Manager’, 45000, 5000);
INSERT INTO Employee VALUES (2, ‘Govind’, ’Engineer’, 30000, 2000);
INSERT INTO Employee VALUES (3, “Shyam’, ‘ Tester’, 25000, 1000);

--Use rollback to undo the insertions


ROLLBACK;

--Check that the records were rolled back (should return 0 rows)
SELECT *FROM Employee;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

STEP 4: Add primary key constraint and not null constraint:

--Alter table to add primary key constraint on EMPNO


ALTER TABLE Employee
ADD CONSTRAINT pk_employee PRIMARY KEY (EMPNO);

---- Alter table to add NOT NULL constraints on required columns


ALTER TABLE Employee
MODIFY (ENAME VARCHAR2(50) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
SAL NUMBER NOT NULL);

Step 5: Insert null values to the employee table and verify the result:

---- Attempt to insert a record with a NULL value in a NOT NULL column (ENAME)
INSERT INTO VALUES (4, NULL, 'Tester', 2, 3000, 200);

-- This insert will fail due to the NOT NULL constraint on ENAME

-- Check the result by querying the table


SELECT * FROM Employee;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

Prog 2. Create a table called Employee that contains the following attributes.
Employee (EMPNO, ENAME, JOB, MGR, SALARY) and execute the
following

 Add a column commission with domain to the Employee table


 Insert any five records into the table.
 Update the column details of job.
 Rename the column of Employee table using alter command.
 Delete the employee whose EmpNo is 105

CREATE TABLE Employee (


EMPNO INTEGER PRIMARY KEY,
ENAME VARCHAR (50),
JOB VARCHAR (50),
MGR INTEGER,
SAL DECIMAL (10)
);

STEP 1: Add a column commission with domain to the employee table

ALTER TABLE Employee


ADD COMMISSION DECIMAL(10);

STEP 2: Insert any five records into the table.

INSERT INTO Employee VALUES (101, 'Raj Kumar', 'Manager', NULL, 80000, 5000);
INSERT INTO Employee VALUES (102, 'Shiva Kumar', 'Analyst', 101, 60000, 3000);
INSERT INTO Employee VALUES (103, 'Arun Kashyap', 'Clerk', 102, 40000, 1000);
INSERT INTO Employee VALUES (104, 'Ravi Gowda', 'Sales Executive', 101, 70000, 4000);
INSERT INTO Employee VALUES (105, 'Anand Yadav', 'Clerk', 102, 38000, 800);

STEP 3: Update the column details of job.

UPDATE Employee
SET JOB = 'Senior Manager'
WHERE EMPNO = 101;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

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

UPDATE Employee
SET JOB = 'Office Clerk'
WHERE EMPNO = 105;

UPDATE Employee
SET JOB = 'Sales Manager'
WHERE EMPNO = 104;

STEP 4: Rename the Column of Employee Table

ALTER TABLE Employee


RENAME COLUMN ENAME TO EMP_NAME;

STEP 5: Delete the Employee Whose EMPNO is 105

DELETE FROM Employee


WHERE EMPNO = 105;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

Prog 3. Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM),
Group by, Orderby. Employee (E_id, E_name, Age, Salary)

 Create Employee table containing all Records E_id, E_name, Age, Salary.
 Count number of employee names from employee table.
 Find the Maximum age from employee table.
 Find the Minimum age from employee table.
 Find salaries of employee in Ascending Order.
 Find grouped salaries of employees.

STEP 1: Create Employee table:

CREATE TABLE Employee (


E_id INTEGER PRIMARY KEY,
E_name VARCHAR (100),
Age INTEGER,
Salary INTEGER (10)
);

STEP 2: Insert Five Records into the Table

INSERT INTO Employee VALUES (101, 'Arjun Jha', 30200);


INSERT INTO Employee VALUES (102, 'Bharath Bhat', 25060);
INSERT INTO Employee VALUES (103, 'Charan Rao', 355000);
INSERT INTO Employee VALUES (104, 'Dhruva Mehta', 128000);
INSERT INTO Employee VALUES (105, 'Aamir Sharief', 407000);

STEP 3: Count the number of employee names from the employee table

SELECT COUNT(E_NAME) AS "NUMBER OF EMPLOYEES"


FROM EMPLOYEE;

STEP 4: Find the Maximum age from the employee table

SELECT MAX(AGE) AS "SENIOR"


FROM EMPLOYEE;

STEP 5: Find the Maximum age from the employee table

SELECT MAX(AGE) AS "JUNIOR"


FROM EMPLOYEE;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

STEP 6: Find salaries of employees in ascending order

SELECT E_NAME, SALARY


FROM EMPLOYEE
ORDER BY SALARY ASC;

STEP 7: Find grouped salaries of employees

SELECT Age, SUM(SALARY) AS "TOTAL SALARY"


FROM EMPLOYEE
GROUP BY AGE;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

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

STEP 1: Create the CUSTOMERS Table

CREATE TABLE CUSTOMERS (


ID NUMBER PRIMARY KEY,
NAME VARCHAR2(100),
AGE NUMBER,
ADDRESS VARCHAR2(200),
SALARY NUMBER
);

STEP 2: Create the Trigger

set serveroutput ON;


CREATE OR REPLACE TRIGGER salary_difference_trigger
AFTER INSERT OR UPDATE OR DELETE
ON CUSTOMERS
FOR EACH ROW
DECLARE
v_old_salary CUSTOMERS.SALARY%TYPE;
v_new_salary CUSTOMERS.SALARY%TYPE;
v_salary_diff CUSTOMERS.SALARY%TYPE;
BEGIN
IF INSERTING THEN
v_old_salary := 0;
v_new_salary := :NEW.SALARY;
ELSIF UPDATING THEN
v_old_salary := :OLD.SALARY;
v_new_salary := :NEW.SALARY;
ELSIF DELETING THEN
v_old_salary := :OLD.SALARY;
v_new_salary := 0;
END IF;

v_salary_diff := v_new_salary - v_old_salary;

DBMS_OUTPUT.PUT_LINE('Salary difference: ' || v_salary_diff);


END;
/

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

STEP 3: Test the Trigger: Now, perform INSERT, UPDATE, and DELETE operations on the
CUSTOMERS table to see the trigger in action.

Insert Operation:

INSERT INTO CUSTOMERS


VALUES (1, 'Braham Kumar', 30, 'Bangalore', 8000);

Update Operation:

UPDATE CUSTOMERS
SET SALARY = 6000
WHERE ID = 1;

Delete Operation:

DELETE FROM CUSTOMERS


WHERE ID = 1;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

Prog 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)
STEP 1: Create the Employee Table

CREATE TABLE Employee (


E_id NUMBER PRIMARY KEY,
E_name VARCHAR2(100),
Age NUMBER,
Salary NUMBER
);

STEP 2: Insert values in Employee Table


INSERT INTO EMPLOYEE VALUES (111, 'Mukund Murari', 30, 50000);
INSERT INTO EMPLOYEE VALUES (222, 'Govind Shrihari', 32, 55000);
INSERT INTO EMPLOYEE VALUES (333, 'Anand Ram', 28, 48000);

STEP 3: Create a Cursor

set serveroutput ON;


DECLARE

v_E_id Employee.E_id%TYPE;
v_E_name Employee.E_name%TYPE;
v_Age Employee.Age%TYPE;
v_Salary Employee.Salary%TYPE;

CURSOR employee_cursor IS
SELECT E_id, E_name, Age, Salary FROM Employee;
BEGIN

OPEN employee_cursor;

LOOP
FETCH employee_cursor INTO v_E_id, v_E_name, v_Age, v_Salary;

EXIT WHEN employee_cursor%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('E_id: ' || v_E_id || ', E_name: ' || v_E_name || ', Age: ' ||
v_Age || ', Salary: ' || v_Salary);
END LOOP;

CLOSE employee_cursor;
END;
/

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

Prog 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.

STEP 1: Create Table N_RollCall

CREATE TABLE N_RollCall (


id NUMBER,
name VARCHAR2(100),
roll_date DATE
);

STEP 2: Insert records in N_RollCall Table

INSERT INTO N_RollCall VALUES (1, 'Chethan Simha, '12-24-2015');


INSERT INTO N_RollCall VALUES (2, 'Prem Kumari', '04-20-2017');
INSERT INTO N_RollCall VALUES (3, 'Bhavani Singh', '08-28-2007');

STEP 3: Create Table O_RollCall

CREATE TABLE O_RollCall (


id NUMBER,
name VARCHAR2(100),
roll_date DATE
);

STEP 4: Insert data in O_RollCall Table

INSERT INTO O_RollCall VALUES (4, 'Ankit Kumar', '04-12-2013');


INSERT INTO O_RollCall VALUES (5, 'Abhishek Singh', '09-25-2014');
INSERT INTO O_RollCall VALUES (6, 'Abhijith Sriram', '04-28-2016');

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

STEP 5: Write the PL/SQL Block:

set serveroutput ON;

DECLARE
v_n_rollcall_id N_RollCall.id%TYPE;
v_n_rollcall_name N_RollCall.name%TYPE;
v_n_rollcall_date N_RollCall.roll_date%TYPE;

CURSOR c_merge_rollcall_data IS
SELECT id, name, roll_date
FROM N_RollCall nrc
WHERE NOT EXISTS (
SELECT 1
FROM O_RollCall orc
WHERE orc.id = nrc.id
AND orc.name = nrc.name
AND orc.roll_date = nrc.roll_date
);
BEGIN

OPEN c_merge_rollcall_data;
LOOP
FETCH c_merge_rollcall_data INTO v_n_rollcall_id, v_n_rollcall_name,
v_n_rollcall_date;
EXIT WHEN c_merge_rollcall_data%NOTFOUND;

INSERT INTO O_RollCall (id, name, roll_date)


VALUES (v_n_rollcall_id, v_n_rollcall_name, v_n_rollcall_date);
END LOOP;

CLOSE c_merge_rollcall_data;

COMMIT;

DBMS_OUTPUT.PUT_LINE('Data merged successfully from N_RollCall to


O_RollCall.');
EXCEPTION
WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);


ROLLBACK;
STEP 6: To see the output command

SELECT * FROM O_RollCall;

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

Prog 7. Install an Open Source NoSQL Data base MongoDB & perform basic
CRUD (Create, Read, Update& Delete) operations. Execute MongoDB basic
queries using CRUD operations.
Performing Basic CRUD Operations:
1. Create Operation:

use basictask;

2. Insert Operation:
a) Insert single documents:

db.users.insertOne({
name: "Braham Kumar",
age: 25,
email: "[email protected]",
status: "inactive"
});

b) Insert multiple documents:


db.users.insertMany([
{
name: "Braham Kumar",
age: 25,
email: "[email protected]",
status: "inactive"
},
{
name: "Shubham Kumar",
age: 35,
email: "[email protected]",
status: "active"
},
{
name: "Bikash Singh",
age: 28,
email: "[email protected]",
status: "active"
},
{
name: "Shoaib Akhtar",
age: 28,
email: "[email protected]",
status: "active"
}
]);

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

3. Read Query:
a) Find all documents:

db.users.find();

OUTPUT

[
{
_id: ObjectId('666c78f13c52fc36f3cdcdf6'),
name: 'Braham Kumar',
age: 25,
email: '[email protected]',
status: 'inactive'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf7'),
name: 'Shubham Kumar',
age: 35,
email: '[email protected]',
status: 'active'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf8'),
name: 'Bikash Singh',
age: 28,
email: '[email protected]',
status: 'active'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf9'),
name: 'Shoaib Akhtar',
age: 28,
email: '[email protected]',
status: 'active'
}
]

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

3. Read Query:
b) Find documents with specific criteria (example: find all users with age greater than 25):
db.users.find({ age: { $gt: 25 } });

OUTPUT

[
{
_id: ObjectId('666c78f13c52fc36f3cdcdf7'),
name: 'Shubham Kumar',
age: 35,
email: '[email protected]',
status: 'active'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf8'),
name: 'Bikash Singh',
age: 28,
email: '[email protected]',
status: 'active'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf9'),
name: 'Shoaib Akhtar',
age: 28,
email: '[email protected]',
status: 'active'
}
]

OUTPUT

4. Update Query:

a) Update a single document:

db.users.updateOne(

{ name: "Bikash Singh" },

{ $set: { age: 31 } }

);

db.users.find({ age: { $gt: 31 } });

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

OUTPUT

[
{
_id: ObjectId('666c78f13c52fc36f3cdcdf7'),
name: 'Shubham Kumar',
age: 35,
email: '[email protected]',
status: 'active'
}
]

b) Update multiple documents:

db.users.updateMany(
{ age: { $gt: 28 } },
{ $set: { status: "inactive" } }
);
db.users.find({ age: { $gt: 28 } });

5. Delete Query:

a) Delete a single document:

db.users.deleteOne({ name: "Bikash Singh" });

b) Delete multiple documents:

db.mycollection.deleteMany({ age: { $gt: 28 } });

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL


BCS403

Basic Queries:

a) Count documents in a collection:

db.users.count();

b) Sorting documents:

db.users.find().sort({ age: 1 }); // Ascending order


db.users.find().sort({ age: -1 }); // Descending order

c) Limiting the number of documents returned:

db.users.find().limit(5);

d) Aggregation queries (e.g., group by and aggregate functions):

db.users.aggregate([
{ $group: { _id: "$age", count: { $sum: 1 } } }
]);

DEPT. OF AI&ML, VKIT 2024 DBMS LAB MANUAL

You might also like