SQL Assignment 3
SQL Assignment 3
Attributes:
Attributes are the properties or characteristics that describe an entity.
For example, an "Employee" entity might have attributes like "Employee ID," "Name,"
"Address," and "Date_of_Joining."
Attributes can be of different types, such as simple, composite, derived, or multi-valued.
Relationships:
Relationships describe how entities are associated with each other.
For example, a "Student" entity may be related to a "Course" entity through an "Enrolled
in" relationship.
In ER diagrams, relationships are typically represented as diamonds, with lines
connecting them to the involved entities.
Keys:
Keys uniquely identify an entity within a database.
Primary keys
Primary keys are attributes that uniquely identify a single instance of an entity (e.g.,
"Student ID" for a "Student" entity).
2
Foreign keys
Foreign keys are attributes in one entity that reference the primary key of another entity
to establish relationships between them.
Question no 2:
Types of Relationships with Examples
1. Unary Relationship (Recursive Relationship):
A unary relationship occurs when an entity is related to itself. In this case, the same
entity participates in the relationship.
Example:
Employee supervises Employee: In a company, an "Employee" entity can have a
relationship with another "Employee" entity, where one employee supervises another.
2. Binary Relationship:
A binary relationship involves two entities and is the most common type of relationship
in an ER model. Example:
Student enrolls in Course: Here, "Student" and "Course" are two different entities, and
the relationship "enrolls in" connects them.
3. Ternary Relationship:
A ternary relationship involves three entities. It represents a relationship that connects
three different entities simultaneously.
Example:
Doctor prescribes Medicine to Patient: In this scenario, the entities "Doctor," "Medicine,"
and "Patient" are involved in the relationship, and all three participate together in the
"prescribes" relationship.
Quaternary Relationship:
A quaternary relationship involves four entities. It is less common but still used in
complex ER models to represent relationships among four entities simultaneously.
Example:
Project requires Employee with Skill in Location: This relationship connects the entities
"Project," "Employee," "Skill," and "Location," indicating that a specific project requires
an employee who possesses a certain skill and is located in a particular place.
3
Question no 3:
1. Entities
Notation: Entities are represented by rectangles.
Description: The name of the entity is written inside the rectangle. For example, an
entity "Student" would appear as a rectangle labeled "Student." Strong Entity: Has a
Weak Entity: Represented by a rectangle with a double border, indicating that its
existence depends on a strong entity.
2. Attributes
Types of Attributes:
Single Valued Attribute: A single-valued attribute holds a single value for each
occurrence of an entity type.
Multi-Valued Attribute: A multi-valued attribute holds multiple values for each
occurrence of an entity type.
Derived Attribute: A derived attribute represents a value that is derivable from the
value of a related attribute or set of attributes, not necessarily in the same entity.
Question no 4:
The multiplicity constraint, also known as cardinality constraint, in an Entity-
Relationship (ER) model specifies the number of instances of one entity that can or
must be associated with each instance of another entity in a relationship. It defines the
4
minimum and maximum number of times an entity can participate in a relationship, and
it is crucial for understanding the nature of the interaction between entities.
"Person."
Example: A "Department" has many "Employees," but each "Employee" belongs to one
"Department."
Example: "Students" can enroll in multiple "Courses," and each "Course" can have
multiple "Students" enrolled.
For example: a multiplicity of (0, N) for an "Employee" in a "Works on" relationship with
"Project" indicates that an employee may work on zero or more projects.
Question no 5:
Integrity constraints are rules or conditions that ensure the accuracy, validity, and
consistency of data within a database. They help maintain the integrity of the database
by restricting the types of data that can be entered or the relationships that can be
formed. Integrity constraints enforce certain conditions on data, making sure it adheres
to specified rules throughout the database's lifecycle.
For example: an age attribute might have a constraint that only allows values between
0 and 120.
Question no 6:
Multiplicity in an Entity-Relationship (ER) model represents both cardinality and
participation constraints for a relationship type, specifying how entities interact within a
relationship and whether their participation is mandatory or optional. It is crucial for
modeling real-world relationships accurately in a database system.
Cardinality defines the number of instances of one entity that can be associated with a
given instance of another entity in a relationship.
The maximum value in the multiplicity notation (such as 1, N, or M) represents the
cardinality constraint.
One-to-One (1:1): Each instance of Entity A is related to at most one instance of Entity
B, and vice versa.
One-to-Many (1: M): Each instance of Entity A can relate to multiple instances of
Entity B, but each instance of Entity B relates to only one instance of Entity A.
Multiplicity: (1, 1)
Example: A "Person" must have exactly one "Social Security Number" (SSN), and each
SSN is associated with exactly one "Person." This enforces a one-to-one relationship
with mandatory participation for both entities.
Question no 7:
8
A relationship type with attributes occurs when the association between entities has
properties of its own that need to be captured. In such cases, the attributes are directly
associated with the relationship rather than the individual entities.
• Entities:
o Student: Attributes include Student ID, Name, and Major.
o Course: Attributes include Course-ID, Course_Name, and Credits.
:
o Enrolls in: Represents the association between Student and Course.
• Attributes of the Relationship:
o The Enrolls_in relationship can have attributes such as:
Enrollment_Date: The date on which the student enrolled in the
course.
Grade: The grade the student received for the course.
ER Diagram Representation:
In the ER diagram:
Explanation:
• Here, Enrollment Date and Grade are specific to the association between the
student and the course, not the individual entities. Therefore, they are attributes
of the Enrolls in relationship.
• This modeling approach allows capturing additional details about the enrollment
process, making the database design more accurate and comprehensive.
Question no 8:
Entity-Relationship (ER) Model:
9
Key Differences:
Aspect ER Model ER Diagram
Conceptual framework for
Graphical representation of the ER
Definition designing
model
databases
Describes the structure and rules Provides a visual depiction of the
Purpose of the database database design
Consists of entities, attributes, Uses rectangles, ovals, and diamonds to
Components represent entities, attributes, and
relationships, and constraints relationships
Used for conceptual database Used for visualizing and communicating
Usage design database structure
10
In summary, the ER model defines the conceptual structure and rules of a database,
while the ER diagram is the graphical illustration of that model, helping to visualize and
understand the database's design.
Question no 9:
Fan traps and chasm traps are potential issues that can occur in an Entity-
Relationship (ER) model when designing a database. They arise due to improper
representation of relationships, leading to ambiguities in understanding the associations
between entities.
1. Fan Trap
• Definition: A fan trap occurs when a model represents
a relationship in such a way that it becomes unclear
which instances of one entity are associated with which
instances of another entity. It happens when a single
entity is involved in two or more one-to-many
relationships that converge at a common entity, making
it difficult to identify the correct path for data association.
• Example:
o Suppose we have three entities: Department, Manager, and Employee.
o There are two relationships:
1. Department has Manager (One-to-Many): Each department has
one manager.
2. Department employs Employee (One-to-Many): Each department
can employ multiple employees.
o The fan trap occurs because the relationships do not explicitly connect the
Manager to the Employee through the Department. We cannot directly
determine which employees a specific manager supervises.
Resolution:
o To resolve a fan trap, we need to add a new relationship or restructure the
existing relationships to correctly represent the intended association.
o In the example, we can introduce a direct relationship between Manager
and Employee (e.g., Manager supervises Employee) to make the
connections explicit.
2. Chasm Trap
• Definition: A chasm trap occurs when a model
allows for possible gaps in the relationships
between entities, leading to missing associations.
It arises when an expected relationship path
between entities is not properly represented,
11
Question no 10:
Let's create the Entity-Relationship (ER) models step by step for each description and
then combine them into a single ER model.
(a) Each company operates four departments, and each department belongs to
one company.
• Entities:
o Company o Department Relationships: o
Company operates Department Cardinality:
o One Company operates multiple Departments (1) o
Lua
Copy code
+----------+ operates +-----------+
| Company |----------------------| Department|
+----------+ (1: N) +-----------+
(b) Each department in part (a) employs one or more employees, and each
employee works for one department.
• Entities: o Department o
Employee Relationships: o
Department employs Employee
Cardinality:
o Each Department employs one or more Employees (1)
(c) Each of the employees in part (b) may or may not have one or more
dependents, and each dependent belongs to one employee.
• Entities:
o Employee o Dependent Relationships: o
Employee has Dependent Cardinality:
o Each Employee may have zero or more Dependents (0)
(d) Each employee in part (c) may or may not have an employment history.
• Entities:
o Employee o Employment_History
Relationships: o Employee has Employment_History
Cardinality:
13
Question no 11:
14
To develop a database system for the university that keeps track of student registration
and accommodation records, let's identify the main entity types, relationship types, and
draw an ER diagram based on your requirements.
Assumptions
• A Student can reside in only one Hostel at a time.
• Each Room has its own distinct set of Furniture.
• There are no limits on how many Courses a Student can enroll in.
• The University owns all the hostels, but the faculties are not responsible for
student accommodation.
+-----------+
|
| contains
|----------------
| (0:N)
|
+-----------+
| Furniture |
|------------|
| Furniture-ID|
| Type |
| Quantity |
+-----------+
+-----------+
| Faculty |
|-----------|
| Faculty-ID|
| Faculty-Name |
+-----------+
|
| offers
|----------------
| (1:N)
|
+----------+
Question no 12:
To design a database for the DVD rental company based on the provided case study, let's break
down the requirements step by step.
+----------+
| DVD |
+----------+
| Catalog-No|
| DVD-No |
| Title |
| Category |
| Daily-Rental|
| Cost |
| Status |
| Main-Actors|
| Director |
+----------+
18
+----------+
| Member |
+----------+
| Member-No |
| First-Name|
| Last-Name |
| Address |
| Register-Date|
+----------+
+----------+
| Rental | +----------+
| Rental-No |
| Member-No |
| DVD-No |
| Rental-Date|
| Return-Date|
| Title |
| Daily-Rental|
+----------+
1..N|
|
|
+----------+
| DVD |
+----------+
| Catalog-No|
| DVD-No |
| Title |
| Category |
| Daily-Rental|
| Cost |
| Status |
| Main-Actors|
| Director |
+----------+
+----------+
| Member |
+----------+
| Member-No |
| First-Name|
| Last-Name |
| Address |
| Register-Date|
+----------+
|
|1
|
|
| rents
|0..N
|
+----------+
| Rental |
+----------+
| Rental-No |
| Member-No |
| DVD-No |
| Rental-Date|
| Return-Date|
| Title |
| Daily-Rental|
+----------+
Branch:
o Attributes:
20
DVD-No(Composite Key)
Member:
o Primary Key: Member-No
Rental:
o Primary Key: Rental-No
21
| Rental-No |
| Member-No |
| DVD-No |
| Rental-Date|
| Return-Date|
| Title |
| Daily-Rental|
+----------+ Assumptions:
1. Each branch can have multiple staff members, but each staff member works for
only one branch.
2. Each DVD can have multiple copies (DVD-No) in different branches.
3. A member can rent multiple DVDs at a time, but only up to ten.
4. The Rental entity contains both a foreign key reference to the Member and the
DVD, allowing for the tracking of rentals and returns.
Question no 13:
To create an ER model for the parking lot system described in your request, we will
break down the requirements step by step, identify the main entity types, their
attributes, and the relationships among them.
3. Staff Member o
Attributes:
Staff-Number (Primary Key, unique for each staff member)
Name
Telephone-Extension
Vehicle-License-Number
(d) Assumptions
1. Each parking space is uniquely identified within its respective parking lot.
2. A staff member can request the sole use of one parking space at a time.
3. The status of a parking space can be either available or reserved for a staff
member.
4. Each parking lot can have multiple parking spaces.
| Parking Space | |
+-------------------+ |
| Space-Number | |
| Parking-Lot-ID | |
| Status | |
+-------------------+ |
|
|
+---------------------+
| Staff Member |
+---------------------+
| Staff-Number |
| Name |
| Telephone-Extension |
| Vehicle-License-Number |
Question no 14:
To create an ER model representing the data used by a library based on the provided
specifications, we will identify the main entity types, their attributes, and the relationships
among them. Here’s how we can break it down: Identify the Main Entity Types
1. Book
o Represents the collection of books in the library.
2. Copy
o Represents individual copies of each book available for loan.
3. Borrower o Represents
individuals who borrow books
from the library.
4. Loan
o Represents the transaction of borrowing books.
Title
Edition
Year-of-Publication 2. Copy o Attributes:
Copy-Number (Primary Key, unique for each book copy)
ISBN (Foreign Key referencing Book)
Status (e.g., Available, Loaned)
Loan-Period (Duration of loan period)
3. Borrower o
Attributes:
Borrower-Number (Primary Key, unique for each borrower)
Name Address 4. Loan o Attributes:
Loan-Number (Primary Key, unique for each loan)
Copy-Number (Foreign Key referencing Copy)
Borrower-Number (Foreign Key referencing Borrower)
Loan-Date Return-Date
|1
|
|
N|
|
+---------------------+
| Loan |
+---------------------+
| Loan-Number |
| Copy-Number |
| Borrower-Number |
| Loan-Date |
| Return-Date |
+---------------------+
|
|1
|
|
N|
|
+---------------------+
| Borrower |
+---------------------+
| Borrower-Number |
| Name |
| Address |
+---------------------+
• Book entity represents the collection of books in the library, uniquely identified by
ISBN.
• Copy entity represents individual copies of each book, uniquely identified by Copy
Number and linked to the Book entity through ISBN.
• Borrower entity represents individuals borrowing books, uniquely identified by
Borrower-Number.
• Loan entity records the transactions of borrowing, linking Copy and Borrower
with attributes like Loan-Date and Return-Date, and is uniquely identified by
Loan-Number.