100% found this document useful (1 vote)
257 views15 pages

DBMS - Manual (bcs403)

Dbms bcs403 notes for 2022 scheme

Uploaded by

shreyadv5
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
100% found this document useful (1 vote)
257 views15 pages

DBMS - Manual (bcs403)

Dbms bcs403 notes for 2022 scheme

Uploaded by

shreyadv5
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/ 15

KNS INSTITUTE OF TECHNOLOGY

HEGDE NAGAR, TIRUMENAHALLI, KOGILU ROAD, BENGALURU – 64

DEPARTMENT OF
ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING

DATABASE MANAGEMENT SYSTEMS LABORATORY


[BCS403]
Academic Year 2023 - 2024
Prepared by: Reviewed by:
Ms. Ayesha Noorain Dr. Aijaz Ali Khan
Assistant Professor, AI/ML Head of the Department, ECE/AIML
KNSIT KNSIT, Bengaluru

Name of the Student:

University Serial Number:

Semester: Batch:
DBMS LABORATORY(BCS403)

Program Outcomes

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change.

DEP OF AIML,KNSIT 1
DBMS LABORATORY(BCS403)

Course Overview

Subject: DBMS Laboratory Subject Code: BCS403


A database management system (DBMS) is computer application software that
provides a way to manage data. The requirement of modern days is to have an automated
system that manages, modifies, and updates data accurately. This is achieved by a DBMS
in robust, correct, and non-redundant way. Structured Database Management Systems
(DBMS) based on relational and other models have long formed the basis for suchdatabases.
Consequently, Oracle, Microsoft SQL Server, Sybase etc. have emerged as leading
commercial systems while MySQL, PostgreSQL etc. lead in open source and free domain.
The Course allows students to apply the conceptual design model to construct thereal-world
requirement. Course gives familiarity of Database Concepts were students can analyse the
various constraints to populate the database and examine different working concepts of
DBMS to infer the most suitable pattern of documentation.
DBMS lab at practicing and achieving this aim by using MySQL. While also gain
caability to design database and its hierarchical structure for given real world application

DEP OF AIML,KNSIT 2
DBMS LABORATORY(BCS403)

SYLLABUS
DATABASE MANAGEMENT SYSTEM Semester 4
Course Code BCS403 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 3:0:2:0 SEE Marks 50
Total Hours of Pedagogy 40 hours Theory + 8-10 Lab slots Total Marks 100
Credits 04 Exam Hours
Examination nature (SEE) Theory

Course objectives:
● To Provide a strong foundation in database concepts, technology, and practice.
● To Practice SQL programming through a variety of database problems.
● To Understand the relational database design principles.
● To Demonstrate the use of concurrency and transactions in database.
● To Design and build database applications for real world problems.
● To become familiar with database storage structures and access techniques.

Teaching-Learning Process
These are sample Strategies, which teachers can use to accelerate the attainment of the various course
outcomes.
1. Lecturer method (L) needs not to be only a traditional lecture method, but alternative effective
teaching methods could be adopted to attain the outcomes.
2. Use of Video/Animation to explain functioning of various concepts.
3. Encourage collaborative (Group Learning) Learning in the class.
4. Ask at least three HOT (Higher order Thinking) questions in the class, which promotes critical
thinking.
5. Adopt Problem Based Learning (PBL), which fosters students’ Analytical skills, develop design
thinking skills such as the ability to design, evaluate, generalize, and analyze information rather than
simply recall it.
6. Introduce Topics in manifold representations.
7. Show the different ways to solve the same problem with different circuits/logic and encourage the
students to come up with their own creative ways to solve them.
8. Discuss how every concept can be applied to the real world - and when that's possible, it helps
improve the students' understanding
9. Use any of these methods: Chalk and board, Active Learning, Case Studies

Sl.NO Experiments
1 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 theuser.
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.

DEP OF AIML,KNSIT 3
DBMS LABORATORY(BCS403)
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 Employeetable.
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.
3 Queries using aggregate functions(COUNT,AVG,MIN,MAX,SUM),Group by,Orderby.
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 employeetable
3. Find the Maximum age from employee table.
4. Find the Minimum age from employeetable.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
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)
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)
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.
7 Install an Open Source NoSQL Data base MangoDB & perform basic CRUD(Create, Read,
Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.
Course outcomes (Course Skill Set):
At the end of the course, the student will be able to:
● Describe the basic elements of a relational database management system
● Design entity relationship for the given scenario.
● Apply various Structured Query Language (SQL) statements for database manipulation.
● Analyse various normalization forms for the given application.
● Develop database applications for the given real world problem.
● Understand the concepts related to NoSQL databases.
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is 50%. The minimum

DEP OF AIML,KNSIT 4
DBMS LABORATORY(BCS403)

BASIC CONCEPTS OF SQL


Introduction to SQL

SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel
– (Structured English Query Language)”. It is a query language used for accessing and modifying
information in the database. IBM first developed SQL in 1970s. Also it is an ANSI/ISO standard.
It has become a Standard Universal Language used by most of the relationaldatabase management
systems (RDBMS). Some of the RDBMS systems are: Oracle, Microsoft SQL server, Sybase etc.
Most of these have provided their own implementation thus enhancing its feature and making it a
powerful tool. Few of the SQL commands used in SQL programming are SELECT Statement,
UPDATE Statement, INSERT INTO Statement, DELETE Statement, WHERE Clause, ORDER
BY Clause, GROUP BY Clause, ORDER Clause, Joins, Views,GROUP Functions, Indexes etc.
SQL Commands
SQL commands are instructions used to communicate with the database to performspecific
task that work with data. SQL commands can be used not only for searching the database but also
to perform various other functions like, for example, you can create tables, add data to tables, or
modify data, drop the table, set permissions for users.
CREATE TABLE Statement
The CREATE TABLE Statement is used to create tables to store data. Integrity Constraints like
primary key, unique key and foreign key can be defined for the columns while creating the table. The
integrity constraints can be defined at column level or table level. The implementation and the syntax
of the CREATE Statements differs for different RDBMS.

The Syntax for the CREATE TABLE Statement is:

CREATE TABLE
table_name

(column_name1 datatype constraint,

column_name2 datatype,...

column_nameNdatatype);

DEP OF AIML,KNSIT 5
DBMS LABORATORY(BCS403)

SQL Data Types:

char(size) Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.

Varchar2(siz Variable-length character string. Max size is specified in parenthesis.


e)
number(size
) Number value with a max number of column digits specified in parenthesis.

or int
Date Date value in „dd-mon-yy‟. Eg., ‟07-jul-2004‟

number(size Number value with a maximum number of digits of "size" total, with a
d) or real maximum number of "d" digits to the right of the decimal.

SQL Integrity Constraints:


Integrity Constraints are used to apply business rules for the database tables.The constraints
available in SQL are Foreign Key, Primary key, Not Null, Unique, Check.
Constraints can be defined in two ways:
1. The constraints can be specified immediately after the column definition. This is called column-level
definition.
2. The constraints can be specified after all the columns are defined. This is called table- level definition.
1) Primary key:
This constraint defines a column or combination of columns which uniquely identifies each row in
the table.
Syntax to define a Primary key at column level:

Column_namedatatype [CONSTRAINT constraint_name] PRIMARY KEY

[CONSTRAINT constraint_name] PRIMARY KEY(column_name1,

column_name2,..)

Syntax to define a Primary key at table level:


2) Foreign key or Referential Integrity:
This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes
a relationship between two columns in the same table or between different tables. For a column to be
defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One

DEP OF AIML,KNSIT 6
DBMS LABORATORY(BCS403)

or more columns can be defined as foreign key.


Syntax to define a Foreign key at column level:

[CONSTRAINT constraint_name] REFERENCES

referenced_table_name(column_name)

3) Not Null Constraint:


This constraint ensures all rows in the table contain a definite value for the column which is
specified as not null. Which means a null value is not allowed.
Syntax to define a Not Null constraint:
[CONSTRAINT constraint name] NOT NULL

4) Unique Key:
This constraint ensures that a column or a group of columns in each row have a distinct value.
A column(s) can have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE

Syntax to define a Unique key at table level:


[CONSTRAINT constraint_name] UNIQUE(column_name)

5) Check Constraint:
This constraint defines a business rule on a column. All the rows must satisfy this rule. The
constraint can be applied for a single column or a group of columns.
Syntax to define a Check constraint:

[CONSTRAINT constraint_name] CHECK (condition)

ALTER TABLE Statement


The SQL ALTER TABLE command is used to modify the definition structure) of a table by
modifying the definition of its columns. The ALTER command is used to perform the following
functions.
1) Add, drop, modify table columns

DEP OF AIML,KNSIT 7
DBMS LABORATORY(BCS403)

2) Add and drop constraints


3) Enable and Disable constraints
The HAVING clause
The HAVING clause can be used to restrict the display of grouped rows. The result of the grouped
query is passed on to the HAVING clause for output filtration.

The INSERT INTO Statement


The INSERT INTO statement is used to insert a new row in a table.
The UPDATE Statement

The UPDATE statement is used to update existing records in a table.

The DELETE Statement

The DELETE statement is used to delete rows in a table. SQL

DELETE

Commit command

Commit command is used to permanently save any transaaction into database

Rollback command
This command restores the database to last commited state. It is also use with savepoint command to
jump to a savepoint in a transaction.
Savepoint command

savepoint command is used to temporarily save a transaction so that you can rollback to that point
whenever necessary.

DEP OF AIML,KNSIT 8
DBMS LABORATORY(BCS403)
LAB EXPERIMENTS

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 theuser.
b. Insert the any three records in the employee table contains attributes EMPNO,ENAME JOB,
MANAGER_NO, SAL, COMMISSION 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.

◼ 1. Create a user and grant all permissions to the user.


• CREATE USER new_user IDENTIFIED BY password;
• GRANT ALL PRIVILEGES TO new_user;

◼ 2. Create the Employee table and insert three records with a rollback.
• CREATE TABLE Employee (
• EMPNO INT,
• ENAME VARCHAR(50),
• JOB VARCHAR(50),
• MANAGER_NO INT,
• SAL DECIMAL(10,2),
• COMMISSION DECIMAL(10,2)
• );

◼ Inserting three records


• INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) VALUES
• (1, 'John Doe', 'Manager', NULL, 50000.00, 1000.00),
• (2, 'Jane Smith', 'Developer', 1, 40000.00, NULL),
• (3, 'Alice Johnson', 'Salesperson', 1, 30000.00, 500.00);

◼ Rolling back the inserts


• ROLLBACK;

◼ 3. Add primary key constraint and not null constraint to the Employee table.
• ALTER TABLE Employee
• ADD CONSTRAINT PK_Employee PRIMARY KEY (EMPNO),
• MODIFY (ENAME NOT NULL),
• MODIFY (JOB NOT NULL),
• MODIFY (SAL NOT NULL);

◼ 4. Insert null values to the Employee table and verify the result.
• INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) VALUES
• (4, NULL, 'Manager', 1, 55000.00, NULL);

◼ Checking the result


• SELECT * FROM Employee;

DEP OF AIML,KNSIT 9
DBMS LABORATORY(BCS403)

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.

◼ 1. Create the Employee table with initial attributes.


• CREATE TABLE Employee (
• EMPNO INT,
• ENAME VARCHAR(50),
• JOB VARCHAR(50),
• MGR INT,
• SAL DECIMAL(10,2)
• );

◼ 2. Add a column called 'commission' to the Employee table.


• ALTER TABLE Employee
• ADD commission DECIMAL(10,2);

◼ 3. Insert five records into the table.


• INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, commission) VALUES
• (101, 'John Doe', 'Manager', NULL, 50000.00, 1000.00),
• (102, 'Jane Smith', 'Developer', 101, 40000.00, 500.00),
• (103, 'Alice Johnson', 'Salesperson', 101, 30000.00, 200.00),
• (104, 'Bob Brown', 'Analyst', 101, 45000.00, NULL),
• (105, 'Emma Davis', 'Designer', 102, 35000.00, 300.00);

◼ 4. Update the column details of JOB.


• UPDATE Employee
• SET JOB = 'Senior ' || JOB;

◼ 5. Rename the column 'Employ' to 'Employee' using ALTER TABLE.


• ALTER TABLE Employee
• RENAME COLUMN Employ TO Employee;

◼ 6. Delete the employee whose Empno is 105.


• DELETE FROM Employee WHERE EMPNO = 105;

DEP OF AIML,KNSIT 10
DBMS LABORATORY(BCS403)

3. Queries using aggregate functions(COUNT,AVG,MIN,MAX,SUM),Group by,Orderby. 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,
• E_name VARCHAR(50),
• Age INT,
• Salary DECIMAL(10, 2)
• );

• SELECT COUNT(E_name) AS TotalEmployees FROM Employee;

• SELECT MAX(Age) AS MaxAge FROM Employee;

• SELECT MIN(Age) AS MinAge FROM Employee;


• SELECT E_name, Salary FROM Employee ORDER BY Salary ASC;

• SELECT Salary, COUNT(*) AS NumEmployees FROM Employee GROUP BY Salary;

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 OR REPLACE TRIGGER salary_difference_trigger


• AFTER INSERT OR UPDATE OR DELETE ON CUSTOMERS
• FOR EACH ROW
• DECLARE
• old_salary NUMBER;
• new_salary NUMBER;
• BEGIN
• IF INSERTING OR UPDATING THEN
• old_salary := NVL(:OLD.SALARY, 0);
• new_salary := NVL(:NEW.SALARY, 0);
• DBMS_OUTPUT.PUT_LINE('Salary difference for ' || :NEW.ID || ': ' || (new_salary - old_salary));
• END IF;

• IF DELETING THEN
• old_salary := NVL(:OLD.SALARY, 0);
• DBMS_OUTPUT.PUT_LINE('Deleted employee ' || :OLD.ID || ' with salary ' || old_salary);
• END IF;
• END;

DEP OF AIML,KNSIT 11
DBMS LABORATORY(BCS403)
• /

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)

• DECLARE
◼ //Declare variables to store values 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;

◼ Declare the cursor


• CURSOR emp_cursor IS
• SELECT E_id, E_name, Age, Salary
• FROM Employee;

• BEGIN
◼ Open the cursor
• OPEN emp_cursor;

◼ Fetch values from the cursor


• LOOP
• FETCH emp_cursor INTO v_E_id, v_E_name, v_Age, v_Salary;
• EXIT WHEN emp_cursor%NOTFOUND;

◼ Process the fetched values (you can perform any operations here)
• DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_E_id || ', Name: ' || v_E_name || ', Age: ' || v_Age || ', Salary: ' ||
v_Salary);
• END LOOP;

◼ Close the cursor


• CLOSE emp_cursor;
• END;
• /

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.

• DECLARE
◼ Declare variables to hold values from the cursor
• v_N_RollCall_id N_RollCall.id%TYPE;
• v_N_RollCall_data N_RollCall.data%TYPE;

◼ Declare parameterized cursor


• CURSOR n_cursor IS
• SELECT id, data
• FROM N_RollCall;

DEP OF AIML,KNSIT 12
DBMS LABORATORY(BCS403)
• BEGIN
◼ Open the cursor
• OPEN n_cursor;

◼ Loop through the data in N_RollCall table


• LOOP
◼ Fetch data from N_RollCall table
• FETCH n_cursor INTO v_N_RollCall_id, v_N_RollCall_data;
• EXIT WHEN n_cursor%NOTFOUND;

◼ Check if data already exists in O_RollCall table


• IF NOT EXISTS (SELECT 1 FROM O_RollCall WHERE id = v_N_RollCall_id) THEN
◼ Insert data into O_RollCall table if it doesn't already exist
o INSERT INTO O_RollCall (id, data)
o VALUES (v_N_RollCall_id, v_N_RollCall_data);
• END IF;
• END LOOP;

◼ Close the cursor


• CLOSE n_cursor;
• END;
• /

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

Step 1: Install MongoDB

1. Download MongoDB: Visit the MongoDB download page and select the appropriate version for your operating system.
2. Install MongoDB: Follow the installation instructions provided for your operating system.

Step 2: Start MongoDB Server

1. After installation, start the MongoDB server. The method depends on your operating system.
For example, on Linux/Mac, you can start MongoDB with the following command in your terminal:
sudo service mongod start

On Windows, you can start it from the Command Prompt or PowerShell:


net start MongoDB

Step 3: Connect to MongoDB

1. Open a terminal or command prompt and connect to the MongoDB server using the mongo shell:
mongo

Step 4: Perform Basic CRUD Operations


Now let's perform basic CRUD operations.

Create Operation
To create a new document in a collection:

DEP OF AIML,KNSIT 13
DBMS LABORATORY(BCS403)
use testDB;

db.users.insertOne({name: "John", age: 30});

Read Operation
To read documents from a collection:

db.users.find();

Update Operation
To update a document:

db.users.updateOne({name: "John"}, {$set: {age: 35}});

Delete Operation
To delete a document:

// Find all documents in the collection

db.users.find();

// Find documents with specific criteria

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

// Count documents in the collection

db.users.count();

// Sort documents

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

// Limit the number of documents returned

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

// Update multiple documents

db.users.updateMany({age: {$gt: 30}}, {$set: {status: "active"}});

// Delete all documents that match a condition

db.users.deleteMany({age: {$lt: 25}});

DEP OF AIML,KNSIT 14

You might also like