RDBMS Practical File
RDBMS Practical File
Practical File
On
RDBMS LAB
Bachelor of Technology
In
Computer Science & Engineering
Session 2023-2027 program under the Maharishi University of Information
Technology, Noida
Submitted to : Ms Cheshta
Objective
The ER diagram is a conceptual tool used in database design to visually map out entities,
relationships, and attributes within a database. It helps in understanding the structure and
relationships within the system, laying the foundation for creating an efficient, normalized
database schema.
Example Scenario
Let’s consider a simple Library Management System. In this system, we need to track Books,
Authors, Members, and Loans.
Key Entities:
Relationships:
Writes: A relationship between Author and Book (an author can write multiple books,
and a book can have multiple authors).
Borrows: A relationship between Member and Loan (a member can borrow multiple
books).
Contains: A relationship between Loan and Book (a loan can contain one or more
books).
Entities:
Relationships:
Cardinalities:
An Author can write multiple Books (1), and each Book can have multiple Authors
(M:1).
A Member can have multiple Loans (1), but each Loan is associated with a single
Member (M:1).
Each Loan can contain multiple Books (1), and each Book can be part of multiple Loans
(M:1).
1. Creating Tables
Publishers Table
PublisherID Name
1 O'Reilly Media
2 Packt Publishing
3 McGraw-Hill
Books Table
Members Table
Loans Table
Objective: Retrieve data from a single table using the SELECT statement.
Query 2: Retrieve the Title and Price columns from the Books table.
Query 3: Retrieve all columns where the Category is "Database" from the Books table.
Objective: Use WHERE to filter data and ORDER BY to sort the results.
Query 1: Retrieve books with price above 500 and sort by Title in descending order.
SELECT Title, Price FROM Books WHERE Price > 500 ORDER BY Title DESC;
Query 2: Retrieve members who joined after 2023-01-01 and sort by JoinDate in ascending
order.
SELECT FirstName, LastName, JoinDate FROM Members WHERE JoinDate > '2023-01-01' ORDER BY JoinDate
ASC;
Query 3: Retrieve books with price greater than 400 and sort them by Price in descending
order.
SELECT Title, Price FROM Books WHERE Price > 400 ORDER BY Price DESC;
Query 1: Retrieve LoanID, MemberName, and BookTitle from Loans, Members, and Books using
INNER JOIN.
Query 2: Retrieve BookTitle, PublisherName from Books and Publishers using LEFT JOIN.
Query 3: Retrieve LoanID, MemberName, and BookTitle with members who haven’t borrowed
any books using LEFT JOIN.
Objective: Use aggregation functions (COUNT, SUM, AVG, MAX, MIN) to perform calculations.
5. Manipulating Data
UPDATE Books
SET Price = 350
WHERE Title = 'Learning SQL';
Query 3: Delete the member with MemberID = 4 from the Members table.
Objective: Create and manage tables, including adding, modifying, and deleting columns.
Query 1: Create a new table Authors with AuthorID, FirstName, LastName, and Country columns.
III. Normalization
1NF Table:
Courses Table:
CourseID | CourseName | Instructor
---------|------------|-----------
1 | Math | Dr. Smith
2 | Science | Dr. Jones
Enrollments Table:
StudentID | CourseID
----------|---------
101 |1
101 |2
Department Table:
DeptID | DeptName | Manager
-------|----------|--------
10 | Sales | John
20 | HR | Mary
Objective: A cursor is used to retrieve and process individual rows returned by a query in a
sequential manner.
Syntax:
Example steps:
OPEN cursor_name;
FETCH cursor_name INTO variable;
CLOSE cursor_name;
Example:
Objective: Stored procedures and functions encapsulate SQL code for reusability and better
performance.
Differences between Procedures and Functions
Procedures: Perform actions, can return multiple values, and use OUT parameters.
Functions: Return a single value and can be used in SQL expressions.
Stored Procedure:
Function:
Use Case:
Use a procedure for complex business logic that may affect multiple tables.
Use a function when a single value needs to be returned (e.g., computing a total or
retrieving a specific attribute).
Objective: Packages group multiple procedures, functions, and other database elements, while
triggers execute actions in response to specific events.
Example Package
Creating a Package:
Package Body:
Example Trigger
Objective: Create a trigger that runs before inserting a record to enforce specific business rules.
Explanation: