DBMS Lab Manual BCS403
DBMS Lab Manual BCS403
************************************************
Name
USN
Section
Lab Batch
Day / Time
************************************************
AY: 2023-2024
AMC ENGINEERING COLLEGE, BENGALURU – 560083
Department of computer Science & Engineering (Data Science)
(Affiliated to Visvesvaraya Technological University, Belagavi & Recognised by AICTE, New Delhi)
EXPERIMENT 1:
AIM:
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 the user.
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.
DESIGN:
TABLE NAME: employee
Fields/Column Domain/Data Format (SQL Size Syntax
name keyword)
EMPNO Number (number) 32-bit integer empno number(10)
ENAME Character (VARCHAR2) 10 Characters ename varchar2(10)
JOB Character (VARCHAR2) 10 Characters job varchar2(10)
MANAGER_NO Number (number) 32-bit integer manager_no number(10)
SAL Number (number) 32-bit integer sal number(10)
COMMISSION Number (number) 32-bit integer commission number(10)
Practice Query:
Drop the table created
SQL> DROP TABLE employee;
P2. Insert the any three records in the employee table contains attributes EMPNO, ENAME JOB,
MANAGER_NO, SAL, COMMISSION and use rollback. Check the result.
SOLUTION:
SYNTAX:
INSERT INTO table (column1, column2, ... column_n ) VALUES (expression1, expression2, ...
expression_n );
SQL>
1. insert into employee (empno, ename, job, manager_no, sal, commission)
values(1,'Abhi','Manager',1,20000,1000);
P3. Add primary key constraint and not null constraint to the employee table.
SOLUTION:
To add NOT NULL constraint to existing column to the employee table.
SYNTAX: ALTER TABLE tablename MODIFY columnname NOT NULL NOVALIDATE;
SQL>
P4: Insert null values to the employee table and verify the result.
To Insert null values, use the insert command and pass the column value as null. Only NULLABLE
columns accept the null values.
SQL>
1. insert into employee (empno, ename, job, manager_no, sal, commission)
values(5,'Kumar','Admin',NULL,NULL,NULL);
2. insert into employee (empno, ename, job, manager_no, sal, commission) values (6,'Suhan’, NULL,
NULL, NULL, NULL);
3. insert into employee (empno, ename, job, manager_no, sal, commission) values (7, NULL, NULL,
NULL, NULL, NULL);
Verify the Result
SQL> select * from employee;
Conclusion:
All the queries as described in the experiment 1 is executed on the oracle 10g DBMS system. The results
are recorded and verified. The modification for the queries is also executed to understand the complete
importance of the CREATE, INSERT and SELECT SQL queries.
EXPERIMENT 2:
AIM:
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.
DESIGN:
TABLE NAME: employee
Fields/Column Domain/Data Format (SQL Size Syntax
name keyword)
EMPNO Number (number) 32-bit integer empno number(10)
ENAME Character (VARCHAR2) 10 Characters ename varchar2(10)
JOB Character (VARCHAR2) 10 Characters job varchar2(10)
MGR Number (number) 32-bit integer mgr number(10)
SAL Number (number) 32-bit integer sal number(10)
Practice Query:
Check the result of insertion of rows into the employee table.
SQL> select * from employee;
Practice Query:
Check the result of updated rows in the employee table.
SQL> select * from employee;
Practice Query:
Check the result of altered column in the employee table.
SQL> desc employee;
Practice Query:
Check the result of deleted row in the employee table.
SQL> select * from employee;
EXPERIMENT 3:
AIM:
Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM), Group by, 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.
DESIGN:
TABLE NAME: employee
Fields/Column Domain/Data Format (SQL Size Syntax
Name keyword)
EMPNO Number (number) 32-bit integer empno number(10)
ENAME Character (VARCHAR2) 10 Characters ename varchar2(10)
Age Number (number) 32-bit integer age number(10)
SAL Number (number) 32-bit integer sal number(10)
Practice Query:
Check the result of the employee table.
SQL> desc employee;
Practice Query:
Check the result of insertion of rows in the employee table.
SQL> select * from employee;
Practice Query:
SQL> select avg(age) from employee;
Practice Query:
Find salaries of employee in DescendingOrder.
SQL> select ename,sal from employee order by sal desc;
Practice Query:
Find employee name and salaries of employee whose age is below 40 and salary is above 5000.
SQL> select ename, sal from employee where age<40 group by ename, sal having sal>5000;
EXPERIMENT 4:
AIM:
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)
DESIGN:
TABLE NAME: customers
Fields/Column Domain/Data Format (SQL Size Syntax
name keyword)
ID Number (number) 32-bit integer id number(10)
NAME Character (VARCHAR2) 30 Characters name varchar2(30)
AGE Number (number) 32-bit integer age number(10)
ADDRESS Number (number) 32-bit integer address varchar2(50)
SALARY Number (number) 32-bit integer salary number(10)
PRE QUERIES:
Create a table called customers & execute the following.
customers (ID, NAME, AGE, ADDRESS, SALARY)
SOLUTION:
SQL> create table customers (id number(10), name varchar2(30), age number(10), address varchar2(50),
salary number(10));
2. insert into customers (id, name, age, address, salary) values(102,'Rohan',42, 'Tumkuru', 10000);
3. insert into customers (id, name, age, address, salary) values(103,'Kumar',38, 'Belagavi', 9000);
4. insert into customers (id, name, age, address, salary) values(104,'Chandu',35, 'Tiptur ', 9000);
5. insert into customers (id, name, age, address, salary) values(105,'Keerthi',36, 'Mangaluru',9000);
Practice Query:
Check the result of insertion of rows in the employee table.
SQL> select * from employee;
EXPERIMENT 5:
AIM:
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)
DESIGN:
TABLE NAME: employee
Fields/Column Domain/Data Format (SQL Size Syntax
name keyword)
E_ID Number (number) 32-bit integer e_id number(10)
E_NAME Character (VARCHAR2) 30 Characters e_name varchar2(30)
AGE Number (number) 32-bit integer age number(10)
SALARY Number (number) 32-bit integer salary number(10)
PRE QUERIES:
Create a table called customers & execute the following.
employee (E_ID,E_NAME, AGE, SALARY)
SOLUTION:
SQL> create table employee (e_id number(10), e_name varchar2(30), age number(10), salary
number(10));
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_e_id, v_e_name, v_age, v_salary;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_e_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_e_name);
DBMS_OUTPUT.PUT_LINE('Employee Age: ' || v_age);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);
END LOOP;
CLOSE emp_cursor;
END;
EXPERIMENT 6:
AIM:
Write a PL/SQL block of code using parameterized Cursor, that will merge the data availablein 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.
DESIGN:
TABLE NAME: N_RollCall
Fields/Column Domain/Data Format (SQL Size Syntax
name keyword)
ROLL Number (number) 32-bit integer roll number(10)
NAME Character (VARCHAR2) 30 Characters name varchar2(30)
PRE QUERIES:
Create a table called N_RollCall & execute the following.
N_RollCall (ROLL,NAME)
SOLUTION:
SQL> create table N_RollCall (roll number(10), name varchar2(30));
EXPERIMENT 7:
AIM:
Install an Open Source NoSQL Data base MangoDB & perform basic CRUD (Create, Read, Update &
Delete) operations. Execute MangoDB basic Queries using CRUD operations.
DESIGN:
TABLE NAME: N_RollCall
Fields/Column Domain/Data Format (SQL Size Syntax
name keyword)
ROLL Number (number) 32-bit integer roll number(10)
NAME Character (VARCHAR2) 30 Characters name varchar2(30)
PROBLEM 1:
Install an Open Source NoSQL Data base MangoDB.
SOLUTION:
MongoDB is an open-source document-oriented database. It is categorized under the NoSQL(Not only
SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables.
Requirements to Install MongoDB on Windows
MongoDB 4.4 and later only support 64-bit versions of Windows.
MongoDB 7.0 Community Edition supports the following 64-bit versions of Windows on x86_64
architecture:
Windows Server 2022
Windows Server 2019
Windows 11
Ensure that the user is running mongod and mongos has the necessary permissions from the following
groups:
Performance Monitor Users
Step 3: Now accept the End-User License Agreement and click the next button:
Step 4: Now select the complete option to install all the program features.
Step 5: Select “Run service as Network Service user” and copy the path of the data directory. Click
Next:
Step 6: Click the Install button to start the MongoDB installation process:
Step 8: Now click the Finish button to complete the MongoDB installation process:
Step 9: Now we go to the location where MongoDB installed in step 5 in your system and copy the bin
path:
Step 10: Now, to create an environment variable open system properties << Environment Variable <<
System variable << path << Edit Environment variable and paste the copied link to your environment
system and click Ok:
Step 11: After setting the environment variable, we will run the MongoDB server, i.e. mongod. So,
open the command prompt and run the following command:
mongod
When you run this command you will get an error i.e. C:/data/db/ not found.
Step 12: Now, Open C drive and create a folder named “data” inside this folder create another folder
named “db”. After creating these folders. Again open the command prompt and run the following
command:
mongod
Now, this time the MongoDB server(i.e., mongod) will run successfully.
PROBLEM 1:
Install an Open Source NoSQL Data base MangoDB.
SOLUTION:
SQL>
perform basic CRUD (Create, Read, Update & Delete) operations. Execute MangoDB basic Queries
using CRUD operations.
P1: CREATE OPERATION
SOLUTION:
Create a New Database Using Mongo Shell
use college
Show List of Databases
show dbs
Check Current Database
db
Create Operations
Method Description
db.collection.insertOne() It is used to insert a single document in the collection.
db.collection.insertMany() It is used to insert multiple documents in the collection.
db.createCollection() It is used to create an empty collection.
Exercise1:
Insert Single Document
db.student.insert({Name: "Akshay", Marks: 500})
db.student.find().pretty()
db.student.insertOne({Name: "Akshay", Marks: 500})
db.student.insertOne({_id: "Stu102", Name: "Vishal", Marks: 230})
db.student.insertMany([{name:"Ajay",age:20},
{name:"Bina",age:24},
{name:"Ram",age:23}])
db.student.insertMany([{_id:"stu200", name:"Ammu", age:18},
{_id:"stu201", name:"Priya", age:29}])
db.student.findOne({language:"c++"})
db.student.findOne({name: "Avinash"}, {_id: 0, language:1})
BASIC OPERATION1:
SOLUTION:
We will drop the student collection in the gfg database. It drops the student collection and all the indexes
associated with the collection:
db.student.drop()
BASIC OPERATION2:
SOLUTION:
MongoDB distinct() method finds the distinct values for a given field across a single collection and returns
the results in an array. It takes three parameters, first one is the field for which to return distinct values
and the others are optional.
db.student.distinct("name")
db.student.distinct("detail.age")
db.student.distinct("marks")
BASIC OPERATION3:
SOLUTION:
In MongoDB, the limit() method limits the number of records or documents that you want. It basically
defines the max limit of records/documents that you want. Or in other words, this method uses on cursor
to specify the maximum number of documents/ records the cursor will return.
db.gfg.find().limit(2)
db.gfg.find({"content":/c/i}).limit(2)
Here, content is key were we will check whether it contains ‘c’ character in the string or not. /c/ denotes
that we are looking for strings that contain this ‘c’ character and in the end of /c/i, i denotes that it is case-
insensitive.
db.gfg.find({"content":/c/i}).limit(3)
BASIC OPERATION4:
SOLUTION:
MongoDB provides a createIndex() method to create one or more indexes on collections. Using this
method we can create different types of indexes like text index, 2dsphere index, 2d index, etc.
db.student.createIndex({name:1})
db.student.createIndex({language:-1})
db.student.createIndex({name:1,language:-1})
db.student.createIndex({name:1},{unique:true})
db.student.createIndex({"branch.$**":1})
BASIC OPERATION5:
SOLUTION:
MongoDB – Comparison Query Operators
Operators Description
$eq Matches the values of the fields that are equal to a specified value.
$ne Matches all values of the field that are not equal to a specified value.
$gt Matches values of the fields that are greater than a specified value.
$gte Matches values of the fields that are greater than equal to the specified value.
$lt Matches values of the fields that are less than a specified value
$lte Matches values of the fields that are less than equal to the specified value
$in Matches any of the values specified in an array.
$nin Matches none of the values specified in an array.