DBMS Lab 10

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

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 10
(Spring 2024)

Course: DBMS Lab Date: 08-05-2024


Course Code: CSL 220 Max Marks: 20
Faculty’s Name: Muhammad Shahid Muhammad Shahid

Name: Enroll No:

Lab 10: ER-Model to Relational-Model

OBJECTIVE:
The objective of teaching the Entity-Relationship (ER) model is to provide a foundational
understanding of data modeling concepts, enabling individuals to design efficient databases by
representing real-world entities and relationships.

Topics to Cover:
Keys:
Primary Key
Foreign Key
Composite Key
One-One Relationship
A one-to-one relationship between two entity types is established only when the foreign Key
is UNIQUE or not duplicate.
Many-to-Many Relationship
Handling a many-to-many relationship between customers and products typically involves
creating an intermediate table, often referred to as a junction table or association table.
PK is a combination of PK of both the tables.
Ternary Relations
A ternary relationship involves three entities and the relationships between them.
In a ternary relationship, you might have a table that includes foreign keys referencing the
primary keys of three related entities.

Adding Foreign Key Constraint:


a) While creating the table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

b) After creating the Table


ALTER TABLE Orders
ADD CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES
Customers(CustomerID);
Transactions:
Transaction Syntax:

BEGIN TRANSACTION Start Transaction.

SQL statement (s)

COMMIT / ROLLBACK Ends Transaction.

Scenario:

Consider the retail scenario of customer, product order and Vendor. A customer can make many
orders and each order can have many products.
Each product is shipped to the customer within due time by specific Vendor, One order will be delivered
to only one address provided by customer while placing order. Customer will also choose a Vendor for
each product within an order.
A Vendor can supply more than one products and each product can be supplied by more than one
Vendor.

TASKS:
Task 1 30 Minutes
Identify the ENTITITY TYPES and suggest their key attributes.
Manually CREATE a DB with your enrollment and also manually create tables for each Entity Type.

Customer
Attributes:
CustomerID (Primary Key)
Name
Email
Address
Phone Number

Product
Attributes:
ProductID (Primary Key)
Name
Description
Price
VendorID (Foreign Key)

Order
Attributes:
OrderID (Primary Key)
CustomerID (Foreign Key)
OrderDate
DeliveryAddress

Vendor
Attributes:
VendorID (Primary Key)
Name
Email
Phone Number

Task 2 30 Minutes
MANUALLY Set Primary Key and foreign key for each RELATION.

ALTER TABLE Product ADD CONSTRAINT fk_vendor


FOREIGN KEY (VendorID)
REFERENCES Vendor(VendorID);

ALTER TABLE Order ADD CONSTRAINT fk_customer


FOREIGN KEY (CustomerID)
REFERENCES Customer(CustomerID);

Task 3 30 Minutes
Create Database Diagram of your database and identify all RELATIONSHIPS among the entity
types and write them down in tabular form.

Entity Types Relationship Description


Customer-Order One-to-Many One customer can place many orders.
Order-Product One-to-Many One order can contain many products.
Product-Vendor Many-to-One Many products can be supplied by one vendor.

Task 4 30 Minutes
a) Find the ternary relationship and represent in RDBMS in an acceptable way.

Relationship between Customer, Order, and Product:

CREATE TABLE CustomerOrderProduct (


CustomerID INT,
OrderID INT,
ProductID INT,
PRIMARY KEY (CustomerID, OrderID, ProductID),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),
FOREIGN KEY (OrderID) REFERENCES Order(OrderID),
FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);

b) Find many-many relationships and resolve it so that database can be implemented correctly.

Many-to-many relationship between Product and Vendor:

CREATE TABLE ProductVendor (


ProductID INT,
VendorID INT,
PRIMARY KEY (ProductID, VendorID),
FOREIGN KEY (ProductID) REFERENCES Product(ProductID),
FOREIGN KEY (VendorID) REFERENCES Vendor(VendorID)
);
ERD:

You might also like