Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Assignment # 3, 4

Database Management System


BSAI-Semester 4, Section 4A
Department of Artificial Intelligence

Date: Week 13 , 14 Dec 2023 Total marks = 30


Name: SAMRA BB Roll No: F2022376083

Assignment 3
Question 1 marks (10) clo3
Apply the concept of creating views using SQL queries. Suppose we have two tables “customers”
and “Orders”. Create view for customer orders that will show the information of customer name
with total amount of all orders placed by him. Fetch the record by applying all joins (INNER)
JOIN, LEFT (OUTER), RIGHT (OUTER), FULL (OUTER) and write the comprehensive subquery to
fetch all records.

Assignment 4
Question 2 marks (10) clo3
How SQL transaction controls statements Begin Trans, Commit, Rollback, and Save point help us
to prevent our data and secure our operations? Give example for each Begin Trans, Commit,
Rollback, and Save point
Project
Question 3 marks (10) clo4
Design the database for Hospital Management System.
Note: It should store patient records, including demographics, medical history, diagnoses,
medications, and test results. It should also manage appointments, scheduling, and billing
information.

A) Design the Data model by applying all three normal forms.


Assignment # 3, 4

Rules:
1. This is an individual assignment. Each student has to submit his/her assignment work.
2. Group discussion is allowed but DO NOT SHARE SOLUTIONS of assignment with the other
students.
3. Plagiarism is intolerable in any of its forms. Minimum penalty would be ZERO marks in the
Assignment.
4. ONLY HANDWRITTEN is acceptable. Also upload scanned (soft form) assignment on LMS.
5. First Page must contain student’s information and solution must be start from second page.
Assignment # 3, 4

Assignment 3
Question 1:
Apply the concept of creating views using SQL queries. Suppose we have two tables “customers” and “Orders”. Create
view for customer orders that will show the information of customer name with total amount of all orders placed by
him. Fetch the record by applying all joins (INNER) JOIN, LEFT (OUTER), RIGHT (OUTER), FULL (OUTER) and write the
comprehensive subquery to fetch all records.

ANSWER:
To create a view in SQL that shows the information of customer names with the total amount of all orders placed by
them using different types of joins, we have to follow the steps below. Assuming you have two tables named
"customers" and "orders," and they have a common column "customer_id" that links them:
SQL QUERY :

-- Create a view using INNER JOIN


CREATE VIEW customer_orders_inner AS
SELECT
c.customer_name,
SUM(o.order_amount) AS total_order_amount
FROM
customers c
INNER JOIN
orders o ON c.customer_id = o.customer_id
GROUP BY
c.customer_name;

-- Create a view using LEFT OUTER JOIN


CREATE VIEW customer_orders_left AS
SELECT
c.customer_name,
COALESCE(SUM(o.order_amount), 0) AS total_order_amount
FROM
customers c
LEFT JOIN
orders o ON c.customer_id = o.customer_id
GROUP BY
c.customer_name;

-- Create a view using RIGHT OUTER JOIN


CREATE VIEW customer_orders_right AS
SELECT
c.customer_name,
COALESCE(SUM(o.order_amount), 0) AS total_order_amount
FROM
customers c
RIGHT JOIN
Assignment # 3, 4
orders o ON c.customer_id = o.customer_id
GROUP BY
c.customer_name;

-- Create a view using FULL OUTER JOIN (Note: Some databases may not support FULL OUTER JOIN directly)
CREATE VIEW customer_orders_full AS
SELECT
c.customer_name,
COALESCE(SUM(o.order_amount), 0) AS total_order_amount
FROM
customers c
FULL OUTER JOIN
orders o ON c.customer_id = o.customer_id
GROUP BY
c.customer_name;

-- Create a view using a comprehensive subquery


CREATE VIEW customer_orders_subquery AS
SELECT
c.customer_name,
(SELECT COALESCE(SUM(order_amount), 0) FROM orders o WHERE o.customer_id = c.customer_id) AS
total_order_amount
FROM
customers c;

EXLANATION:
In these queries:
customer_orders_inner: Uses INNER JOIN to get only the matching records between customers and orders.
customer_orders_left: Uses LEFT OUTER JOIN to include all records from the "customers" table and matching records
from the "orders" table.
customer_orders_right: Uses RIGHT OUTER JOIN to include all records from the "orders" table and matching records
from the "customers" table.
customer_orders_full: Uses FULL OUTER JOIN to include all records when there is a match in either the "customers" or
"orders" table.
customer_orders_subquery: Uses a subquery to fetch records, where the subquery calculates the total order amount
for each customer.

Assignment 4
Assignment # 3, 4
Question 2:
How SQL transaction controls statements Begin Trans, Commit, Rollback, and Save point help us to prevent our data
and secure our operations? Give example for each Begin Trans, Commit, Rollback, and Save point.

Answer:
SQL transaction control statements, such as BEGIN TRANSACTION, COMMIT, ROLLBACK, and SAVEPOINT, are essential
for managing database transactions. These statements help ensure data integrity, consistency, and security in the database. Here's
an explanation and example for each:
BEGIN TRANSACTION:
Marks the beginning of a transaction. All the SQL statements that follow the BEGIN TRANSACTION statement are
part of the same transaction until a COMMIT or ROLLBACK statement is encountered.
Example:

BEGIN TRANSACTION;
-- SQL statements for the transaction go here

COMMIT:
Marks the end of a successful transaction. All changes made during the transaction are permanently saved to the
database.
Example:
Commit;
ROLLBACK:
Undoes the changes made during the current transaction. If an error occurs or if the transaction needs to be canceled,
the ROLLBACK statement is used to revert any modifications made so far.
Example:

BEGIN TRANSACTION;
-- SQL statements for the transaction go here
-- If an error occurs or conditions are not met
ROLLBACK;
SAVEPOINT:
Creates a point within the current transaction to which you can later roll back. This allows you to have nested levels of
transactions within a larger transaction.
Example:

BEGIN TRANSACTION;
-- SQL statements for the main transaction go here
SAVEPOINT my_savepoint;
-- Nested transaction
BEGIN
-- SQL statements for the nested transaction go here
-- If an error occurs or conditions are not met
ROLLBACK TO my_savepoint;
END;
-- Continue with the main transaction
COMMIT;

In the above examples:


BEGIN TRANSACTION: is used to mark the start of a transaction.
Assignment # 3, 4
COMMIT: is used to permanently apply the changes made during the transaction.
ROLLBACK: is used to undo the changes if an error occurs or if conditions are not met.
SAVEPOINT: is used to create a point within the transaction, allowing for partial rollbacks.

You might also like