Dbms Manual
Dbms Manual
Manual Prepared by
Ranjith V, Assistant Professor
Veena M S, Assistant
Dr. Manjunath R, AssistantProfessor
Professor,
Tejaswini Krishnamurthy, Assistant Professor
R. R. Institute of Technology
Affiliated to VTU Belgaum and Approved by AICTE, New Delhi ,Recognized by Govt. of Karnataka
Accredited by NAAC with ‘A+’
Raja Reddy Layout, Chikkabanavara, Bengaluru – 560 090
College Vision "To be a Premier globally recognized Institute with ensuring academic excellence, Innovation
and fostering Research in the field of Engineering"
College Mission • To consistently strive for Academic Excellence
• To promote collaborative Research & Innovation
• To create holistic teaching learning environment that build ethically sound manpower
who contribute to the stake holders operating at Global environment
Department To arise as an excellent learning center in the field of Computer Science & Engineering by fostering a
Vision skilled, innovative professionals to build a strong nation.
Department • To provide a conceptual foundation that caters the career required to adopt for changing
Mission technology in computer science.
• To bridge the gap between academics and the latest tools, technologies in the area of hardware
and software.
• To set out co-curricular open doors for student’s participation in advancements and recent
trends.
• To explore the potential and excel the students towards research to attain Novelty.
Program PEO1: Proficient to recognize contemporary issues and provide solutions using broad
Educational
knowledge of computer science.
Objectives
(PEOs) PEO2: Ability to plan, analyze, design, evolve project implementing capabilities and skills in
IT industry.
PEO3: Drive to adapt new computing technologies lifelong to acquire professional greatness.
PEO4: Possess professional, ethical, social responsibilities, communicational skills and team
work needed for a successful professional carrier.
Program PSO1: Apply the software practices, principals to design and analyse the complex computer based
Specific system.
Outcomes
(PSOs) PSO2: Design, implement and validate system software and application software to the various societal
needs.
PKM Educational Trust ®
R. R. Institute of Technology
Affiliated to VTU Belgaum and Approved by AICTE, New Delhi ,Recognized by Govt. of Karnataka
Accredited by NAAC with ‘A+’
Raja Reddy Layout, Chikkabanavara, Bengaluru – 560 090
passing mark for the CIE is 40% of the maximum marks (20 marks out of 50) and for the SEE minimum passing mark is
35% of the maximum marks (18 out of 50 marks). A student shall be deemed to have satisfied the academic requirements
and earned the credits allotted to each subject/ course if the student secures a minimum of 40% (40 marks out of 100) in the
sum total of the CIE (Continuous Internal Evaluation) and SEE (Semester End Examination) taken together.
CIE for the theory component of the IPCC (maximum marks 50)
● IPCC means practical portion integrated with the theory of the course.
● CIE marks for the theory component are 25 marks and that for the practical component is 25 marks.
● 25 marks for the theory component are split into 15 marks for two Internal Assessment Tests (Two Tests, each of 15
Marks with 01-hour duration, are to be conducted) and 10 marks for other assessment methods mentioned in 22OB4.2.
The first test at the end of 40-50% coverage of the syllabus and the second test after covering 85-90% of the syllabus.
● Scaled-down marks of the sum of two tests and other assessment methods will be CIE marks for the theory
component of IPCC (that is for 25 marks).
● The student has to secure 40% of 25 marks to qualify in the CIE of the theory component of IPCC.
CIE for the practical component of the IPCC
● 15 marks for the conduction of the experiment and preparation of laboratory record, and 10 marks for the test to be
conducted after the completion of all the laboratory sessions.
● On completion of every experiment/program in the laboratory, the students shall be evaluated including viva-voce and
marks shall be awarded on the same day.
● The CIE marks awarded in the case of the Practical component shall be based on the continuous evaluation of the
laboratory report. Each experiment report can be evaluated for 10 marks. Marks of all experiments’ write-ups are added
and scaled down to 15 marks.
● The laboratory test (duration 02/03 hours) after completion of all the experiments shall be conducted for 50 marks
and scaled down to 10 marks.
● Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory component of IPCC
for 25 marks.
● The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of the IPCC.
SEE for IPCC
Theory SEE will be conducted by University as per the scheduled timetable, with common question papers for the course
(duration 03 hours)
1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a module (with a maximum of 3 sub-
questions), should have a mix of topics under that module.
3. The students have to answer 5 full questions, selecting one full question from each module.
4. Marks scoredby the student shall be proportionally scaled down to 50 Marks
The theory portion of the IPCC shall be for both CIE and SEE, whereas the practical portion will have a CIE
component only. Questions mentioned in the SEE paper may include questions from the practical component.
TABLE CREATION
TABLE DESCRIPTION
1
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
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.
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) VALUES (1, 'Kavana
Shetty', 'Manager', NULL, 50000.00, 1000.00);
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) VALUES (2, 'Ram
Charan', 'Developer', 1, 40000.00, NULL);
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) VALUES (3, 'Honey
Singh', 'Salesperson', 2, 30000.00, 500.00);
ROLLBACK;
3. Add primary key constraint and not null constraint to the employee table.
TABLE DESCRIPTION
2
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
4. Insert null values to the employee table and verify the result.
INSERT INTO Employee VALUES (NULL, NULL, NULL, NULL, NULL, NULL);
3
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
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.
TABLE CREATION
TABLE DESCRIPTIONS
INSERT INTO Employee VALUES (101, 'Radha Bai', 'Manager', NULL, 5000.00, 1000.00);
INSERT INTO Employee VALUES (102, 'Krishna Kumar', 'Developer', 101, 4000.00, NULL);
INSERT INTO Employee VALUES (103, 'Abdul Sattar', 'Salesperson', 102, 3000.00, 500.00);
INSERT INTO Employee VALUES (104, 'Bob Johnson', 'Accountant', 101, 4500.00, NULL);
INSERT INTO Employee VALUES (105, 'Amartya Sen', 'HR Manager', 101, 4800.00, 800.00);
4
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
5
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
3.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.
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (1, 'Samarth', 30, 50000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (2, 'Ramesh Kumar', 25, 45000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (3, 'Seema Banu', 35, 60000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (4, 'Dennis Anil', 28, 52000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (5, 'Rehman Khan', 32, 58000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (6, 'Pavan Gowda', 40, 70000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (7, 'Shruthi Bhat', 27, 48000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (8, 'Sandesh Yadav', 29, 52000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (9, 'Vikram Acharya', 33, 62000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (10, 'Praveen Bellad', 26, 46000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (11, 'Sophia Mary', 31, 55000.00);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (12, 'Darshan Desai', 34, 62000.00);
6
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
7
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
8
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
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)
9
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
INSERT INTO CUSTOMERS (ID,NAME, AGE, ADDRESS, SALARY) VALUES(1, 'John Doe', 30, '123 Elm
Street', 50000.00);
10
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
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)
DECLARE
v_id Employee.E_id%TYPE;
v_name Employee.E_name%TYPE;
v_age Employee.Age%TYPE;
v_salary Employee.Salary%TYPE;
CURSOR emp_cursor IS
SELECT E_id, E_name, Age, Salary FROM Employee;
BEGIN
OPEN emp_cursor;
11
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
LOOP
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_id || ', Name: ' || v_name || ', Age: ' || v_age || ', Salary: ' || v_salary);
END LOOP;
CLOSE emp_cursor;
END;
/
12
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
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.
13
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
BEGIN
-- Open the cursor
OPEN n_cursor;
14
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
done := TRUE;
END IF;
15
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
7. Install an Open Source NoSQL Data base MangoDB & perform basic CRUD(Create,
Read,Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.
Install an Open Source NoSQL Data base MangoDB & perform basic
CRUD(Create, Read,Update & Delete) operations. Execute MangoDB basic
Queries using CRUD operations.
MongoDB can be installed on Ubuntu with the use of the following commands. These commands are
easy to run on the terminal and make the installation process handy. Follow the steps given below to
install MongoDB:
Step 1: First you need to update and upgrade your system repository to install MongoDB. Type the
following command in your terminal and then press Enter.
16
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
Step 2: Now, install the MongoDB package using ‘apt ‘. Type the following command and press
Enter.
sudo apt install -y mongodb
Step 3: Check the service status for MongoDB with the help of following command:
sudo systemctl status mongodb
Step 4: Now check if the installation process is done correctly and everything is working fine. Go
through the following command:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
the value “1” in ok field indicates that the server is working properly with no errors.
Step 5: MongoDB services can be started and stopped with the use of following commands: To stop
running the MongoDB service, use command:
MongoDB service has been stopped and can be checked by using the status command:
18
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
As it can be seen that the service has stopped, to start the service we can use :
sudo systemctl start mongodb
1. Create (Insert)
To create or insert data into a MongoDB collection, you use the insertOne() or
19
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
insertMany() methods.
Insert a single document:
db.collection('yourCollection').insertOne({ key: value });
Insert multiple documents:
db.collection('yourCollection').insertMany([
{ key1: value1 },
{ key2: value2 },
// more documents
]);
2. Read (Query)
To read or retrieve data from a MongoDB collection, you use the find() method.
Find all documents:
db.collection('yourCollection').find();
Find documents with a specific condition:
db.collection('yourCollection').find({ key: value });
3. Update
To update existing documents in a MongoDB collection, you use the updateOne() or updateMany()
methods.
Update a single document:
db.collection('yourCollection').updateOne(
{ key: value }, // filter
{ $set: { newField: newValue } } // update operation
);
Update multiple documents:
db.collection('yourCollection').updateMany(
{ key: value }, // filter
{ $set: { newField: newValue } } // update operation
);
4. Delete
To delete documents from a MongoDB collection, you use the deleteOne() or deleteMany() methods.
Delete a single document:
db.collection('yourCollection').deleteOne({ key: value });
Delete multiple documents:
db.collection('yourCollection').deleteMany({ key: value });
20
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
VIVA-QUESTIONS
1. What is data?
Data is a collection of information gathered by observations, measurements,
research or analysis.
2. What is database?
A database is an electronically stored, systematic collection of data. It can contain
any type of data, including words, numbers, images, videos, and files.
3. What is DBMS?
Database Management Systems (DBMS) are software systems used to store,
retrieve, and run queries on data.
7. What is Table?
A table is an arrangement of data in rows and columns, or possibly in a more
complex structure.
8. What is a Tuple?
A tuple is an ordered sequence of values. The values can be repeated, but their
number is always finite.
9. What is Columns?
column or pillar in architecture and structural engineering is a structural element
that transmits, through compression, the weight of the structure above to other
structural elements below.
21
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
11.What is an Attribute ?
A quality, character, or characteristic ascribed to someone or something has leadership attributes.
26.What is a query?
A query with respect to DBMS relates to user commands that are used to interact with a data base.
The query language can be classified into data definition language and data manipulation language.
27. How do you perform CRUD operations create, read, update, delete MongoDB?
MongoDB CRUD Operation
The Create operation is used to insert new documents in the MongoDB database.
The Read operation is used to query a document in the database.
The Update operation is used to modify existing documents in the database.
The Delete operation is used to remove documents in the database.
23
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
28. What are the CRUD operations in NoSQL database?
CRUD is the acronym for CREATE, READ, UPDATE and DELETE. These terms describe the
four essential operations for creating and managing persistent data elements, mainly in relational and
NoSQL databases.
24
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
EXTRA QUERIES
Consider the following schema for Order Database: SALESMAN (Salesman_id, Name, City,
Commission) CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id) ORDERS
(Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id) Write SQL queries to 1. Count
the customers with grades above Bangalore’s average. 2. Find the name and numbers of all
salesmen who had more than one customer. 3. List all salesmen and indicate those who have and
don’t have customers in their cities (Use UNION operation.) 4. Create a view that finds the
salesman who has the customer with the highest order of a day. 5. Demonstrate the DELETE
operation by removing salesman with id 1000. All his orders must also be deleted.
Solution:
Entity-Relationship Diagram
Table Creation
CREATE TABLE SALESMAN (SALESMAN_ID NUMBER (4), NAME VARCHAR2 (20), CITY
VARCHAR2 (20), COMMISSION VARCHAR2 (20), PRIMARY KEY (SALESMAN_ID));
25
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
CREATE TABLE ORDERS (ORD_NO NUMBER (5), PURCHASE_AMT NUMBER (10, 2),
ORD_DATE DATE, PRIMARY KEY (ORD_NO), CUSTOMER_ID REFERENCES CUSTOMER1
(CUSTOMER_ID) ON DELETE CASCADE, SALESMAN_ID REFERENCES SALESMAN
(SALESMAN_ID) ON DELETE CASCADE);
Table Descriptions
DESC SALESMAN;
DESC CUSTOMER1;
DESC ORDERS;
27
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
Queries:
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities (Use
UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a day.
28
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
5.Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must
also be deleted. Use ON DELETE CASCADE at the end of foreign key definitions while creating
child table orders and then execute the following: Use ON DELETE SET NULL at the end of
foreign key definitions while creating child table customers and then executes the following:
29
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
The Sales table records information about product sales, including the quantity sold, sale
date, and total price for each sale. It serves as a transactional data source for analyzing sales
trends.
Query:
-- Create Sales table
Output:
Sales Table
2. Products Table
30
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
The Products table contains details about products, including their names, categories, and
unit prices. It provides reference data for linking product information to sales transactions.
Query:
-- Create Products table
Output:
Products Table
QUERIES
4. Filter the Sales table to show only sales with a total_price greater than $100.
Query:
SELECT * FROM Sales WHERE total_price > 100;
Explanation:
This SQL query selects all columns from the Sales table but only returns rows where the
total_price column is greater than 100. It filters out sales with a total_price less than or equal to
$100.
5. Filter the Products table to show only products in the 'Electronics' category.
Query:
SELECT * FROM Products WHERE category = 'Electronics';
Explanation:
This SQL query selects all columns from the Products table but only returns rows where the
category column equals 'Electronics'. It filters out products that do not belong to the
'Electronics' category.
6. Retrieve the sale_id and total_price from the Sales table for sales made on January 3,
2024.
Query:
SELECT sale_id, total_price
FROM Sales
WHERE sale_date = '2024-01-03';
32
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
7. Retrieve the product_id and product_name from the Products table for products with
a unit_price greater than $100.
Query:
SELECT product_id, product_name
FROM Products
WHERE unit_price > 100;
Explanation:
This SQL query selects the product_id and product_name columns from the Products table but
only returns rows where the unit_price is greater than $100. It filters out products with a
unit_price less than or equal to $100.
8. Calculate the total revenue generated from all sales in the Sales table.
Query:
SELECT SUM(total_price) AS total_revenue
FROM Sales;
Explanation:
This SQL query calculates the total revenue generated from all sales by summing up the
total_price column in the Sales table using the SUM() function.
12. Retrieve product_name and unit_price from the Products table with the Highest Unit
Price
Query:
SELECT product_name, unit_price
FROM Products
ORDER BY unit_price DESC
LIMIT 1;
Explanation:
This query sorts the Products table by unit_price in descending order and retrieves the product
with the highest price using the LIMIT clause.
13. Retrieve the sale_id, product_id, and total_price from the Sales table for sales with a
quantity_sold greater than 4.
Query:
SELECT sale_id, product_id, total_price
FROM Sales
WHERE quantity_sold > 4;
Explanation:
This SQL query selects the sale_id, product_id, and total_price columns from the Sales table
but only returns rows where the quantity_sold is greater than 4.
14. Retrieve the product_name and unit_price from the Products table, ordering the
results by unit_price in descending order.
Query:
SELECT product_name, unit_price
FROM Products
ORDER BY unit_price DESC;
Explanation:
This SQL query selects the product_name and unit_price columns from the Products table and
orders the results by unit_price in descending order using the ORDER BY clause with
the DESC keyword.
15. Retrieve the total_price of all sales, rounding the values to two decimal places.
Query:
SELECT ROUND(SUM(total_price), 2) AS total_sales
FROM Sales;
Explanation:
This SQL query calculates the total sales revenu by summing up the total_price column in the
Sales table and rounds the result to two decimal places using the ROUND() function.
34
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
16. Calculate the average total_price of sales in the Sales table.
Query:
SELECT AVG(total_price) AS average_total_price
FROM Sales;
Explanation:
This SQL query calculates the average total_price of sales by averaging the values in the
total_price column in the Sales table using the AVG() function.
17. Retrieve the sale_id and sale_date from the Sales table, formatting the sale_date as
'YYYY-MM-DD'.
Query:
SELECT sale_id, DATE_FORMAT(sale_date, '%Y-%m-%d') AS formatted_date
FROM Sales;
18. Calculate the total revenue generated from sales of products in the 'Electronics'
category.
Query:
SELECT SUM(Sales.total_price) AS total_revenue
FROM Sales
JOIN Products ON Sales.product_id = Products.product_id
WHERE Products.category = 'Electronics';
Explanation:
This SQL query calculates the total revenue generated from sales of products in the
'Electronics' category by joining the Sales table with the Products table on the product_id
column and filtering sales for products in the 'Electronics' category.
19. Retrieve the product_name and unit_price from the Products table, filtering the
unit_price to show only values between $20 and $600.
Query:
SELECT product_name, unit_price
FROM Products
WHERE unit_price BETWEEN 20 AND 600;
Explanation:
This SQL query selects the product_name and unit_price columns from the Products table but
only returns rows where the unit_price falls within the range of $20 and $600 using the
BETWEEN operator.
35
DEPARTMENT OF CSE 2023-2024
BCS403 DATABASE MANAGEMENT SYSTEM
20. Retrieve the product_name and category from the Products table, ordering the
results by category in ascending order.
Query:
SELECT product_name, category
FROM Products
ORDER BY category ASC;
Explanation:
This SQL query selects the product_name and category columns from the Products table and
orders the results by category in ascending order using the ORDER BY clause with the ASC
keyword.
36
DEPARTMENT OF CSE 2023-2024