Uploaded by mahir All zebare

mahir tahir

advertisement
Duhok polytechnic university
Akre technical college
Department:IT
2020-2021
Semmenar about:Relationship within the relational database.
Prepared by:mahir tahir hassan
What is relationship ?
relationship, in the context of
databases, is a situation that exists
between two relational database
tables when one table has a foreign
key that references the primary key
of the other table. Relationships
allow relational databases to split
and store data in different tables,
while linking disparate data items.
Type of relationship in database________________________
There are 3 different types of relations
in the database:
•one-to-one
•one-to-many, and
•many-to-many
One-to-one relation
In the database, we want to store employees, but also their valid identity
cards. We’re not interested in storing any other types of documents or
identity cards that were previously valid, so we need exactly 1 (or none)
identity card for 1 employee.
Let’s check this truly is a one-to-one relation. We’ve been given these
rules: One employee could have only one valid identity card in our
system. One identity card could belong to only one employee. We haven’t
used the word “many”, so this can’t be any type of relation including the
word “many”.
One-to-many______________________
we’ll start with is one-to-many. The reason for that is that it’s the most
commonly used and the remaining two are “subtypes” of this one. Let’s
start with a real-life problem.
Imagine that we want to store a list of all our customers in the database.
For each customer, we also want to store the city where this customer is
located, and we know that the customer will be in exactly one city.
We simply established a relation from the city.id to customer.city_id. And
this works, because the customer can be only in one city and the city could
have many different customers located in it.
When you want to determine the nature of the relation you need to
establish between two tables just do this. In our example – For one city, we
could have many different customers located in it. And the other way
around – For one customer, we can have only one city it’s located in.
For example, if you have a list of works of art and a list of museums,
each work of art can only be in one museum at a time, but each
museum can have many works of art. In a base, splitting these two lists
of entities (museums and works of art) into two tables allows you to
store information relevant to each entity.
Many-to-many relationships___________
A many-to-many relationship occurs when multiple records in
a table are associated with multiple records in another table. For
example, a many-to-many relationship exists between customers
and products: customers can purchase various products, and
products can be purchased by many customers.
Real Life Example: A user can belong to multiple community, and a
community can have multiple users.
A typical example of a many-to many relationship is one between
students and classes. A student can register for many classes, and a
class can include many students.
The following example includes a Students table, which contains a
record for each student, and a Classes table, which contains a
record for each class. A join table, Enrollments, creates two one-tomany relationships—one between each of the two tables.
Download