0% found this document useful (0 votes)
0 views23 pages

DBMS Lab Manual

This document is a laboratory manual for the Database Management System course (BCS403) at Rajarajeshwari College of Engineering, prepared by Mr. Ghouse Pasha. It includes installation instructions for Oracle database 11g, step-by-step queries for creating and managing an Employee table, and a set of viva questions for students. The manual covers various SQL operations such as creating tables, inserting records, and modifying table structures.

Uploaded by

knpavithra.95
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)
0 views23 pages

DBMS Lab Manual

This document is a laboratory manual for the Database Management System course (BCS403) at Rajarajeshwari College of Engineering, prepared by Mr. Ghouse Pasha. It includes installation instructions for Oracle database 11g, step-by-step queries for creating and managing an Employee table, and a set of viva questions for students. The manual covers various SQL operations such as creating tables, inserting records, and modifying table structures.

Uploaded by

knpavithra.95
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/ 23

RAJARAJESWARI COLLEGE OF ENGINEERING

No: 14, Ramohalli Cross, Kumbalgodu, Mysore Road, Bangalore-560 074

A MANUAL FOR

Database Management System Laboratory


(BCS403)

II SEMESTER

PREPARED
BY
Mr. Ghouse Pasha
Assistant Professor
Dept. of Computer Science & Engineering
[IoT, Cyber security including Block chain
Technology]R.R.C.E, Bangalore

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


[IoT, Cyber security including Block chain Technology]
2023 - 24
Database Management System BCS403

PREFACE

This manual is intended for the VTU fourth semester Computer Science & Engineering,
Database Management System Lab. I have provided description of the installation of
various components required to carry out the lab. Also, it contains stepwise
information on the usability of the tool Oracle database 11g being used for the
execution of programs. The programs are well tested and simplified providing useful
information as to how to execute them. A set of viva questions has also been included
for the benefit of the students.

Dept. of CSE[IC], R.R.C.E 2023-24


Database Management System BCS403

WEEK-1
QUERIES USING DDL AND DML

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


Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION)
a. Create a user and grant all permissions to the user.
b. Insert the any three records in the employee table and use rollback. Check the
result.
c. Add primary key constraint and not null constraint to the employee table.
d. Insert null values to the employee table and verify the result.

SOLUTION:

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

--Create user query

SYNTAX:
create user ‘username’@localhost identified by ‘password’;

EXAMPLE:
create user ‘yamuna’@localhost identified by ‘rrce’;

--Provide roles/--Assigning privileges

grant select ,insert, update, delete on *.* to 'yamuna'@localhost;


Query OK, 0 rows affected (0.04 sec)

flush privileges;
Query OK, 0 rows affected (0.07 sec)
--Drop the user
drop user 'yamuna'@localhost;
Query OK, 0 rows affected (0.01 sec)

b) mysql> create database Example;


Query OK, 1 row affected (0.01 sec)

mysql> use Example;


Database changed
mysql> create table employee(
empno int,
ename varchar(20),
job varchar(20),
manager_no int,
sal int,
commission int);
Dept of CSE [IC], RRCE Page 1
Database Management System BCS403

Query OK, 0 rows affected (0.01 sec)

mysql> desc employee;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| empno | int | YES | | NULL | |

| ename | varchar(20) | YES | | NULL | |

| job | varchar(20) | YES | | NULL | |

| manager_no | int | YES | | NULL | |

| sal | int | YES | | NULL | |

| commission | int | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

6 rows in set (0.00 sec)

Dept of CSE [IC], RRCE Page 2


Database Management System BCS403

b) Insert the any three records in the employee table and use rollback. Check the
result.

mysql> insert into employee values(1,'yamuna','assistant professor',11,60000,1000);


Query OK, 1 row affected (0.01 sec)

mysql> insert into employee values(2,'yamunashree','assistant


professor',12,60000,2000),(3,'smitha','Assistant Profeesor',13,70000,3000);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from employee;


+-------+-------------+---------------------+------------+-------+------------+
| empno | ename | job | manager_no | sal | commission |
+-------+-------------+---------------------+------------+-------+------------+
| 1 | yamuna | assistant professor | 11 | 60000 | 1000 |
| 2 | yamunashree | assistant professor | 12 | 60000 | 2000 |
| 3 | smitha | Assistant Profeesor | 13 | 70000 | 3000 |
+-------+-------------+---------------------+------------+-------+------------+
3 rows in set (0.00 sec)

mysql> start transaction;


Query OK, 0 rows affected (0.00 sec)

mysql> insert into employee values(4,'sinchana','assistant professor',11,60000,1000);


Query OK, 1 row affected (0.00 sec)

mysql> insert into employee values(5,'Dinesh','assistant professor',11,60000,1000);


Query OK, 1 row affected (0.00 sec)

mysql> select * from employee;


+-------+-------------+---------------------+------------+-------+------------+
| empno | ename | job | manager_no | sal | commission |
+-------+-------------+---------------------+------------+-------+------------+
| 1 | yamuna | assistant professor | 11 | 60000 | 1000 |
| 2 | yamunashree | assistant professor | 12 | 60000 | 2000 |
| 3 | smitha | Assistant Profeesor | 13 | 70000 | 3000 |
| 4 | sinchana | assistant professor | 11 | 60000 | 1000 |
| 5 | Dinesh | assistant professor | 11 | 60000 | 1000 |
+-------+-------------+---------------------+------------+-------+------------+
5 rows in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.07 sec)

mysql> select * from employee;


+-------+-------------+---------------------+------------+-------+------------+
| empno | ename | job | manager_no | sal | commission |
+-------+-------------+---------------------+------------+-------+------------+
| 1 | yamuna | assistant professor | 11 | 60000 | 1000 |

Dept of CSE [IC], RRCE Page 3


Database Management System BCS403

| 2 | yamunashree | assistant professor | 12 | 60000 | 2000 |


| 3 | smitha | Assistant Profeesor | 13 | 70000 | 3000 |
+-------+-------------+---------------------+------------+-------+------------+
3 rows in set (0.00 sec)

c) Add primary key constraint and not nullconstraint to the employeetable.


mysql> alter table employee modify empno int primary key;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table employee modify ename varchar(20) not null;


Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc employee;


+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| empno | int | NO | PRI | NULL | |
| ename | varchar(20) | NO | | NULL | |
| job | varchar(20) | YES | | NULL | |
| manager_no | int | YES | | NULL | |
| sal | int | YES | | NULL | |
| commission | int | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)4 rows in set (0.00 sec)

d) Insert null values to the employee table and verify the result.
mysql> insert into employee
values(5,'Dinesh','assistant professor',11,60000,NULL);

Query OK, 1 row affected (0.01 sec)

mysql> insert into employee


values(4,'sinchana','assistant professor',11,NULL,1000);

Query OK, 1 row affected (0.01 sec)

mysql> select * from employee;

+-------+-------------+---------------------+------------+-------+----
--------+

| empno | ename | job | manager_no | sal


| commission |

+-------+-------------+---------------------+------------+-------+----
--------+
Dept of CSE [IC], RRCE Page 4
Database Management System BCS403

| 1 | yamuna | assistant professor | 11 |


60000 | 1000 |

| 2 | yamunashree | assistant professor | 12 |


60000 | 2000 |

| 3 | smitha | Assistant Profeesor | 13 |


70000 | 3000 |

| 4 | sinchana | assistant professor | 11 |


NULL | 1000 |

| 5 | Dinesh | assistant professor | 11 |


60000 | NULL |

+-------+-------------+---------------------+------------+-------+----
--------+

5 rows in set (0.00 sec)

Dept of CSE [IC], RRCE Page 5


Database Management System BCS403

WEEK-2
CREATION OF TABLES

1) Create a table called Employee with the following structure.

Name Type
Empno Int
Ename Varchar2(10)
Job Varchar2(10)
Mgr Int
Sal Int

a. AddacolumncommissionwithdomaintotheEmployeetable.
b. Insert any five records into the table.
c. Update the column details of job
d. Rename the column of Employ table using alter command.
e. Delete the employee whose Empno is 105.

SOLUTION:

SQL> create table employee1(empno int, ename varchar(10),job


varchar(10),mgr int ,sal int);

Table created.

Query OK, 0 rows affected (0.11 sec)

mysql> desc employee1;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| empno | int | YES | | NULL | |

| ename | varchar(10) | YES | | NULL | |

| job | varchar(10) | YES | | NULL | |

| mgr | int | YES | | NULL | |

| sal | int | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

5 rows in set (0.00 sec)

Dept of CSE [IC], RRCE Page 6


Database Management System BCS403

a. Add a column commission with domain to the Employee table.

mysql> alter table employee1 add(commission int);

Query OK, 0 rows affected (0.06 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc employee1;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| empno | int | YES | | NULL | |

| ename | varchar(10) | YES | | NULL | |

| job | varchar(10) | YES | | NULL | |

| mgr | int | YES | | NULL | |

| sal | int | YES | | NULL | |

| commission | int | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

6 rows in set (0.00 sec)

b. Insert any five records into the table.

mysql> insert into employee1 values(4,'sinchana','Professor',11,60000,1000);

Query OK, 1 row affected (0.01 sec)

mysql> insert into employee1


values(1,'yamuna','Professor',11,70000,1000),(2,'smitha','manager',12,80000,3000);

Query OK, 2 rows affected (0.07 sec)

Records: 2 Duplicates: 0 Warnings: 0

Dept of CSE [IC], RRCE Page 7


Database Management System BCS403

mysql> insert into employee1


values(5,'shree','Developer',11,70000,1000),(3,'sowmya','Tester',13,80000,3000);
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from employee1;


+-------+----------+-----------+------+-------+------------+
| empno | ename | job | mgr | sal | commission |
+-------+----------+-----------+------+-------+------------+
| 4 | sinchana | Professor | 11 | 60000 | 1000 |
| 1 | yamuna | Professor | 11 | 70000 | 1000 |
| 2 | smitha | manager | 12 | 80000 | 3000 |
| 5 | shree | Developer | 11 | 70000 | 1000 |
| 3 | sowmya | Tester | 13 | 80000 | 3000 |
+-------+----------+-----------+------+-------+------------+

5 rows in set (0.00 sec)

c. Update the column details of job

SQL>update employee1 set job='trainee'


where empno=1;

1 row updated.

update employee1 set job='trainee' where


empno=1;

Query OK, 1 row affected (0.07 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee1;


+-------+----------+-----------+------+-------+------------+
| empno | ename | job | mgr | sal | commission |
+-------+----------+-----------+------+-------+------------+
| 4 | sinchana | Professor | 11 | 60000 | 1000 |
| 1 | yamuna | trainee | 11 | 70000 | 1000 |
| 2 | smitha | manager | 12 | 80000 | 3000 |
| 5 | shree | Developer | 11 | 70000 | 1000 |
| 3 | sowmya | Tester | 13 | 80000 | 3000 |
+-------+----------+-----------+------+-------+------------+

5 rows in set (0.00 sec)5 rows in set (0.00


sec)

d. Rename the column of Employ table using alter command.

SQL>alter table employee1 rename column mgr to


manager_no;

Dept of CSE [IC], RRCE Page 8


Database Management System BCS403

Table altered.

SQL>desc employee1;

mysql> desc employee1;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| empno | int | YES | | NULL | |

| ename | varchar(10) | YES | | NULL | |

| job | varchar(10) | YES | | NULL | |

| manager_no | int | YES | | NULL | |

| sal | int | YES | | NULL | |

| commission | int | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

6 rows in set (0.00 sec)

e. Delete the employee whose Empno is 5.

mysql> delete from employee1 where empno=5;


Query OK, 1 row affected (0.01 sec)

mysql> select * from employee1;


+-------+----------+-----------+------------+-------+------------+
| empno | ename | job | manager_no | sal | commission |
+-------+----------+-----------+------------+-------+------------+
| 4 | sinchana | Professor | 11 | 60000 | 1000 |
| 1 | yamuna | trainee | 11 | 70000 | 1000 |
| 2 | smitha | manager | 12 | 80000 | 3000 |
| 3 | sowmya | Tester | 13 | 80000 | 3000 |
+-------+----------+-----------+------------+-------+------------+
4 rows in set (0.00 sec)

Dept of CSE [IC], RRCE Page 9


Database Management System BCS403

WEEK-3

QUERIES USING AGGREGATE FUNCTIONS

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


Group by,Orderby,Having.

mysql> select * from employee;

E_NO E_name Age Sal


1 yamuna 34 50000
2 shree 57 70000
3 asha 34 40000
11 yamunashree 34 50000
12 yamunashree 34 50000
13 smitha 35 NULL
14 smitha 35 NULL
15 sinchana 30 NULL

a)Create Employee table containing all Records.


SQL> create table employee(eno int, ename varchar(10),age
int, sal int);
Table created.
mysql> desc employee;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| empno | int | NO | PRI | NULL | |
| ename | varchar(20) | NO | | NULL | |
| age | int | YES | | NULL | |
| sal | int | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

b)Count number of employee names from employee table.


SQL> select count(ename) from
employee;
OUTPUT: COUNT(ENAME)

Dept of CSE [IC], RRCE Page 10


Database Management System BCS403

c)Find the Maximum age from employee table.


SQL>select max(age) from employee;
OUTPUT:
MAX(AGE)
57
d)Find the Minimum age from employee table.
SQL>select min(age)
from employee;

OUTPUT: MIN(AGE)
30

Display the Sum of age employee table.

SQL> select sum(age) from employee;


OUTPUT: SUM(AGE)
293

Display the Average age from Employee table.

SQL> select avg(age) from employee;

OUTPUT: AVG(AGE)
36.250

--Create a View for employee table.

SQL>create or replace view A as select age from employee where age<30;


Query OK, 0 rows affected (0.01 sec)

View created.

--Display views

SQL>select * from A;

+------+
| age |
+------+
| 34 |
| 34 |
| 34 |
| 34 |
| 35 |
| 35 |
| 30 |
+------+
7 rows in set (0.00 sec)

e) Find salaries of employee in Ascending Order.(orderbyclause)


SQL> select ename, sal from
employee order by sal;

Dept of CSE [IC], RRCE Page 11


Database Management System BCS403

+-------------+-------+
| ename | sal |
+-------------+-------+
| smitha | NULL |
| smitha | NULL |
| sinchana | NULL |
| asha | 40000 |
| yamuna | 50000 |
| yamunashree | 50000 |
| yamunashree | 50000 |
| shree | 70000 |
+-------------+-------+
8 rows in set (0.00 sec)

f) Find grouped salaries of employees.(group byclause)


SQL> select sal from employee
group by sal;

+-------+
| sal |
+-------+
| 50000 |
| 70000 |
| 40000 |
| NULL |
+-------+
4 rows in set (0.00 sec)

Dept of CSE [IC], RRCE Page 12


Database Management System BCS403

WEEK-4 :TRIGGERS

1. 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 values and new values:

CUSTOMERS table:

ID NAME AGE ADDRESS SALARY


1 Alive 24 Khammam 2000
2 Bob 27 Kadappa 3000
3 Catri 25 Guntur 4000
4 Dena 28 Hyderabad 5000
5 Eeshwar 27 Kurnool 6000
6 Farooq 28 Nellur 7000

create table customers(id int,name char(20),age int,address varchar(30),salary int);

Query OK, 0 rows affected (0.03 sec)

mysql> insert into customers values(1,'yamuna',32,'tnarasipur',60000),(2,'shree',34,'mysore',400000);

Query OK, 2 rows affected (0.01 sec)

Records: 2 Duplicates: 0 Warnings: 0

insert into customers values(3,'anu',32,'tnagar',60000),(4,'yamunashree',34,'mysore',40000);

create table salary_difference_log(

old_salary int,

new_salary int,

salary_difference int );

mysql> DELIMITER //

mysql> create trigger salary_update_trigger

before update on customers

for each row

begin

declare old_salary int;

declare new_salary int;

Dept of CSE [IC], RRCE Page 13


Database Management System BCS403

declare salary_diff int;

set old_salary = old.salary;

set new_salary = new.salary;

set salary_diff = new_salary - old_salary;

insert into salary_difference_log (old_salary, new_salary, salary_difference)

values (old_salary, new_salary, salary_diff);

end;

//

Query OK, 0 rows affected (0.01 sec)

mysql> update customers set salary=25000 where id=1;

-> //

Query OK, 1 row affected (0.07 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from salary_difference_log;

-> //

+------------+------------+-------------------+

| old_salary | new_salary | salary_difference |

+------------+------------+-------------------+

| 15000 | 25000 | 10000 |

+------------+------------+-------------------+

1 row in set (0.00 sec)

mysql> select * from customers;

+------+-------------+------+------------+--------+

| id | name | age | address | salary |

+------+-------------+------+------------+--------+

| 1 | yamuna | 32 | tnarasipur | 25000 |

| 2 | shree | 34 | mysore | 400000 |

| 3 | anu | 32 | tnagar | 60000 |

| 4 | yamunashree | 34 | mysore | 40000 |

Dept of CSE [IC], RRCE Page 14


Database Management System BCS403

+------+-------------+------+------------+--------+

4 rows in set (0.00 sec)

Triggercreated.

Here following two points are important and should be noted carefully:

OLD and NEW references are not available for table level triggers, rather you can use
them for record level triggers.

If you want to query the table in the same trigger, then you should use the AFTER
keyword, because triggers can query the table or change it again only after the initial
changes are applied and the table is back in a consistent state.

Above trigger has been written in such a way that it will fire before any DELETE or
INSERT or UPDATE operation on the table, but you can write your trigger on a single or
multiple operations, for example BEFORE DELETE, which will fire whenever a record
will be deleted using DELETE operation on the table.

Let us perform some DML operations on the CUSTOMERS table. Here is one INSERT
statement, which will create a new record in the table:

When a record is created in CUSTOMERS table, above create trigger display_salary_changes will be
fired and it will display the following result:

Oldsalary:15000
Newsalary:25000
Salarydifference:10000

Dept of CSE [IC], RRCE Page 15


Database Management System BCS403

WEEK- 5: CURSORS

DEFINITION OF A CURSOR

1. Cursor can be created to store the values from table temporally.


2. In execution these values fetch from cursor for access the database
 Create cursor fetch the values from the table
 Declare the variables
 Open the cursor
 Fetch the values from the cursor
 Close the cursor

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)

CURSOR EXAMPLE:

mysql> use yamuna3;

Database changed

mysql> select * from employee;

+-------+-------------+---------------------+------------+--------+------------+

| empno | ename | job | manager_no | sal | commission |

+-------+-------------+---------------------+------------+--------+------------+

| 1 | sowmya | Tester | 13 | 80000 | 3000 |

| 2 | yamunashree | professor | 11 | 20000 | 1000 |

| 3 | shree | tester | 33 | 300000 | 12000 |

| 5 | Dinesh | assistant professor | 11 | 60000 | NULL |

| 6 | shree | Developer | 11 | 70000 | 1000 |

| 7 | sowmya | Tester | 13 | 80000 | 3000 |

| 8 | shree | Developer | 11 | 70000 | 1000 |

+-------+-------------+---------------------+------------+--------+------------+

Dept of CSE [IC], RRCE Page 16


Database Management System BCS403

7 rows in set (0.02 sec)

mysql> delimiter $$

create procedure list_name()

begin

declare is_done integer default 0;

declare s_name varchar(100) default 0;

declare stud_cursor cursor for

select ename from employee;

declare continue handler for not found set is_done=1;

open stud_cursor;

get_list:loop

fetch stud_cursor into s_name;

select s_name;

if is_done=1 then

leave get_list;

end if;

end loop get_list;

close stud_cursor;

end $$

Output: Query OK, 0 rows affected (0.01 sec)

mysql> call list_name4();

-> $$

+--------+

| s_name |

+--------+

Dept of CSE [IC], RRCE Page 17


Database Management System BCS403

| sowmya |

+--------+

1 row in set (0.00 sec)

+-------------+

| s_name |

+-------------+

| yamunashree |

+-------------+

1 row in set (0.00 sec)

+--------+

| s_name |

+--------+

| shree |

+--------+

1 row in set (0.00 sec)

+--------+

| s_name |

+--------+

| Dinesh |

+--------+

1 row in set (0.01 sec)

+--------+

| s_name |

+--------+

| shree |

Dept of CSE [IC], RRCE Page 18


Database Management System BCS403

+--------+

1 row in set (0.01 sec)

+--------+

| s_name |

+--------+

| sowmya |

+--------+

1 row in set (0.01 sec)

+--------+

| s_name |

+--------+

| shree |

+--------+

1 row in set (0.01 sec)

Dept of CSE [IC], RRCE Page 19


Database Management System BCS403

WEEK- 6: PL/SQL code

6 . Write a PL/SQL code using parameterized Cursor, that will merge the data available
in the newly created table N_RollCall with the data available in table O_RollCall. If the
data in the first table already exist in the second table then that data should be skipped.

CREATE OR REPLACE PROCEDURE merge_roll_call_data AS


CURSOR n_rollcall_cursor IS
SELECT * FROM N_RollCall;
v_id N_RollCall.id%TYPE;
v_name N_RollCall.name%TYPE;

-- Add other columns as needed

BEGIN
FOR n_rec IN n_rollcall_cursor LOOP
-- Check if the record exists in O_RollCall
SELECT id
INTO v_id
FROM O_RollCall
WHERE id = n_rec.id;

-- If the record doesn't exist, insert it into O_RollCall


EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO O_RollCall(id, name /* Add other columns as needed */)
VALUES (n_rec.id, n_rec.name /* Add other column values as needed */);
WHEN OTHERS THEN
-- Handle other exceptions if necessary
NULL;
END LOOP;

COMMIT;
END merge_roll_call_data;

(or)
merge into old c1 using new c2
on (c1.id=c2.id)
when matched then update set
c1.name=c2.name
when not matched then
insert values(c2.id,c2.name);

Dept of CSE [IC], RRCE Page 20


Database Management System BCS403

WEEK- 7: NOSQL Database

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

MongoDB is a NoSQL database, and it doesn't require "installation" in the traditional sense as it is often
run as a service. However, you can set it up on your local machine or use a cloud-based service. Below are
the basic steps to get started with MongoDB and perform CRUD operations:

1. Install MongoDB: You can download and install MongoDB from the official website
(https://www.mongodb.com/try/download/community). Follow the installation instructions provided for
your operating system.
2. Start MongoDB Server: After installation, start the MongoDB server. If you've installed it locally, you
might run it as a service or manually start it using commands like mongod on the terminal.
3. Access MongoDB Shell: MongoDB provides a shell interface (mongo) that allows you to interact with the
database. Open a new terminal or command prompt window and run the mongo command to start the
MongoDB shell.
4. Perform CRUD Operations:
 Create (Insert): To insert documents into a collection, you use the insertOne() or
insertMany() methods.
Example: db.collectionName.insertOne({ field1: value1, field2: value2, ... });

 Read (Query): To query documents from a collection, you use the find() method.
/* q Example: db.collectionName.find({ /* query criteria */ });

 Update: To update documents in a collection, you use the updateOne() or updateMany()


methods.
/ Example: db.collectionName.updateOne({ /* filter criteria */ }, { $set: { /* update fields */ } });*/
} });
 Delete: To delete documents from a collection, you use the deleteOne() or deleteMany()
methods.
Example: db.collectionName.deleteOne({ /* filter criteria */ }); });
5. Execute Basic Queries:
Let's assume you have a collection named users:
 Insert: db.users.insertOne({ name: "John", age: 30 });
 Read: db.users.find();
 Update: db.users.updateOne({ name: "John" }, { $set: { age: 35 } });
 Delete: db.users.deleteOne({ name: "John" });
6. Exit MongoDB Shell: To exit the MongoDB shell, type exit.

Dept of CSE [IC], RRCE Page 21

You might also like