Assignment # 02
Database Systems
Submitted By: Haider Ali
Submitted To: Ms. Maria Gul
Registration No: B23F0001AI054
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
Q1: Construct an E-R diagram for a car insurance company whose customers own one
or more cars each. Each car has associated with it zero with any number of recorded
accidents. Each insurance policy covers one or more cars and has one or more premium
payments associated with it. Each payment is for a particular period, and has an
associated due date, and the date when the payment was received
Page |2
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
Q2: An E-R diagram can be viewed as a graph.What do the following mean in terms of
the structure of an enterprise schema?
a. The graph is disconnected.
b. The graph has a cycle.
(a) The graph is disconnected
A disconnected graph means that some parts of the E-R diagram are not connected to others.
Meaning in Terms of Enterprise Schema:
There exists at least one entity or group of entities that are completely isolated.
These isolated entities do not participate in any relationships with other entities.
This can indicate poor database design, as it means some data may be inaccessible or not
integrated with the rest of the system.
Example:
Suppose in a university database, we have:
o Student → Enrolls → Course
o Professor → Teaches → Course
o But Department is not linked to any entity.
Here, the Department entity is disconnected, meaning it’s not used in any relationships,
which is a flaw.
(b) The graph has a cycle
A cycle in a graph means that you can start from an entity, follow the relationships, and return to the
same entity without repeating any edges.
Meaning in Terms of Enterprise Schema:
A cycle suggests that there is a circular dependency among entities.
It may indicate redundancy in relationships, but in some cases, cycles are valid and
necessary.
Normalization is often required to avoid unnecessary cycles.
Example:
Consider a company database:
Employee → WorksFor → Department
Department → ManagedBy → Manager (who is also an Employee)
Manager → ReportsTo → Senior Manager (who is also an Employee)
Here, a cycle is formed: Employee → Department → Manager → Employee
This cycle is valid because a manager is an employee, but it might require careful design to avoid
redundant data storage.
Page |3
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
.
Page |4
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
Q3: Consider a many-to-one relationship R between entity sets Aand B. Suppose the
relation created from R is combined with the relation created from A. In SQL, attributes
participating in a foreign key constraint can be null. Explain how a constraint on total
participation of A in R can be enforced using not null constraints in SQL.
In a many-to-one (M:1) relationship R between entity sets A and B, multiple entities in A are related
to a single entity in B, but each A must be linked to exactly one B. When the relation from R is
combined with A, we must ensure that every entity in A is always associated with a B (total
participation).
To enforce total participation, the foreign key in A referencing B must be NOT NULL. This
prevents any A entity from existing without being linked to B. Additionally, a foreign key constraint
ensures referential integrity, meaning every referenced B must exist in the database.
For Example:
(b) CREATE TABLE B (
B_ID INT PRIMARY KEY
);
CREATE TABLE A (
A_ID INT PRIMARY KEY,
B_ID INT NOT NULL,
FOREIGN KEY (B_ID) REFERENCES B(B_ID)
);
Page |5
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
Q4 Design a database for an automobile company to provide to its dealers to assist them
in maintaining customer records and dealer inventory and to assist sales staff in
ordering cars. Each vehicle is identified by a vehicle identification number (VIN). Each
individual vehicle is a particular model of a particular brand offered by the company
(e.g., the XF is a model of the car brand Jaguar of Tata Motors). Each model can be
offered with a variety of options, but an individual car may have only some (or none) of
the available options. The database needs to store information about models, brands,
and options, as well as information about individual dealers, customers, and cars. Your
design should include an E-R diagram, a set of relational schemas, and a list of
constraints, including primary-key and foreign-key constraints.
. Database Design for an Automobile Company
Relational Schema
Brand Table
CREATE TABLE Brand (
Brand_ID INT PRIMARY KEY,
Page |6
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
Brand_Name VARCHAR(255) NOT NULL
);
Model Table
CREATE TABLE Model (
Model_ID INT PRIMARY KEY,
Model_Name VARCHAR(255) NOT NULL,
Brand_ID INT NOT NULL,
FOREIGN KEY (Brand_ID) REFERENCES Brand(Brand_ID)
);
Option Table
CREATE TABLE Option (
Option_ID INT PRIMARY KEY,
Option_Name VARCHAR(255) NOT NULL,
Description TEXT
);
Vehicle Table
CREATE TABLE Vehicle (
VIN VARCHAR(20) PRIMARY KEY,
Model_ID INT NOT NULL,
Dealer_ID INT NOT NULL,
Price DECIMAL(10,2),
Color VARCHAR(50),
Year INT,
FOREIGN KEY (Model_ID) REFERENCES Model(Model_ID),
FOREIGN KEY (Dealer_ID) REFERENCES Dealer(Dealer_ID)
);
Vehicle_Option (Many-to-Many Relationship Between Vehicles & Options)
CREATE TABLE Vehicle_Option (
VIN VARCHAR(20),
Option_ID INT,
PRIMARY KEY (VIN, Option_ID),
Page |7
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
FOREIGN KEY (VIN) REFERENCES Vehicle(VIN),
FOREIGN KEY (Option_ID) REFERENCES Option(Option_ID)
);
Dealer Table
CREATE TABLE Dealer (
Dealer_ID INT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Location TEXT
);
Customer Table
CREATE TABLE Customer (
Customer_ID INT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Contact VARCHAR(20)
);
Order Table
CREATE TABLE Order_Details (
Order_ID INT PRIMARY KEY,
Customer_ID INT NOT NULL,
Dealer_ID INT NOT NULL,
Order_Date DATE,
Total_Amount DECIMAL(10,2),
FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID),
FOREIGN KEY (Dealer_ID) REFERENCES Dealer(Dealer_ID)
);
Order_Vehicle (Many-to-Many Relationship Between Orders & Vehicles)
CREATE TABLE Order_Vehicle (
Order_ID INT,
VIN VARCHAR(20),
PRIMARY KEY (Order_ID, VIN),
FOREIGN KEY (Order_ID) REFERENCES Order_Details(Order_ID),
Page |8
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
FOREIGN KEY (VIN) REFERENCES Vehicle(VIN)
);
Constraints
Primary Keys
Brand_ID in Brand
Model_ID in Model
Option_ID in Option
VIN in Vehicle
Dealer_ID in Dealer
Customer_ID in Customer
Order_ID in Order_Details
Foreign Keys
Brand_ID in Model → Brand(Brand_ID)
Model_ID in Vehicle → Model(Model_ID)
Dealer_ID in Vehicle → Dealer(Dealer_ID)
VIN in Vehicle_Option → Vehicle(VIN)
Option_ID in Vehicle_Option → Option(Option_ID)
Customer_ID in Order_Details → Customer(Customer_ID)
Dealer_ID in Order_Details → Dealer(Dealer_ID)
VIN in Order_Vehicle → Vehicle(VIN)
Order_ID in Order_Vehicle → Order_Details(Order_ID)
Explanation of Relationships
Brand ↔ Model (1:M) → A brand can have multiple models.
Model ↔ Vehicle (1:M) → Each model has multiple vehicles.
Model ↔ Option (M:N) → A model has many options, and an option can be in multiple models.
Vehicle ↔ Dealer (M:1) → Each vehicle is assigned to a dealer.
Page |9
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
Dealer ↔ Customer (M:N) → A dealer can serve multiple customers.
Customer ↔ Order (1:M) → A customer can place multiple orders.
Order ↔ Vehicle (M:N) → Each order can contain multiple vehicles.
Additional Features
You can add inventory tracking by creating a table to store stock levels.
You can include sales staff and track which staff member processes which order.
Q5 Design a database for a world-wide package delivery company (e.g., DHL or FedEX).
The databasemust be able to keep track of customers (who ship items) and customers
(who receive items); some customers may do both. Each package must be identifiable
and trackable, so the database must be able to store the location of the package and its
history of locations. Locations include trucks, planes, airports, and warehouses. Your
design should include an E-R diagram, a set of relational schemas, and a list of
constraints, including primary-key and foreign-key constraints.\
Relational Schema
P a g e | 10
Instructor Name: Ms. Maria Gul Department: AI- Blue
Student Name: Ibadullah Qureshi Assignment: 02
Registration No: B23F0001AI072
Customer Table
CREATE TABLE Customer (
Customer_ID INT PRIMARY KEY,
Name VARCHAR(255),
Address TEXT,
Phone VARCHAR(15)
);
Package Table
CREATE TABLE Package (
Package_ID INT PRIMARY KEY,
Weight DECIMAL(5,2),
Dimensions VARCHAR(50),
Shipping_Date DATE,
Sender_ID INT,
Receiver_ID INT,
FOREIGN KEY (Sender_ID) REFERENCES Customer(Customer_ID),
FOREIGN KEY (Receiver_ID) REFERENCES Customer(Customer_ID)
);
Location Table
CREATE TABLE Location (
Location_ID INT PRIMARY KEY,
Type ENUM('Truck', 'Warehouse', 'Airport'),
Address TEXT
);
Tracking History Table
CREATE TABLE Tracking_History (
Tracking_ID INT PRIMARY KEY,
Package_ID INT,
Timestamp DATETIME,
Status VARCHAR(50),
Location_ID INT,
FOREIGN KEY (Package_ID) REFERENCES Package(Package_ID),
FOREIGN KEY (Location_ID) REFERENCES Location(Location_ID)
);
Constraints
Primary Keys: Customer_ID, Package_ID, Location_ID, Tracking_ID
Foreign Keys:
o Sender_ID, Receiver_ID → Customer(Customer_ID)
o Package_ID → Tracking_History(Package_ID)
o Location_ID → Tracking_History(Location_ID)
P a g e | 11