0% found this document useful (0 votes)
13 views11 pages

Relationship

The document explains the concept of relationships in relational databases, focusing on primary and foreign keys that establish connections between tables. It details four types of relationships: one-to-one, one-to-many, many-to-one, and many-to-many, along with their cardinality and participation constraints. Additionally, it highlights the importance of relationships in reducing data redundancy, organizing databases, and maintaining referential integrity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Relationship

The document explains the concept of relationships in relational databases, focusing on primary and foreign keys that establish connections between tables. It details four types of relationships: one-to-one, one-to-many, many-to-one, and many-to-many, along with their cardinality and participation constraints. Additionally, it highlights the importance of relationships in reducing data redundancy, organizing databases, and maintaining referential integrity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Relationship

The main feature of relational databases is relationships. Different relationship types control how
the data and tables relate to one another. Establishing links between tables through relationships
makes a database type unique and knowing how relationships work helps broaden database
modeling capabilities.
The two following elements define how a database relationship is established:
 Primary key - A column whose value uniquely identifies a table record.
 Foreign key - A column whose values reference the primary key of another table.

A connection between a parent table and the child table exists by referencing the parent's primary
key. The reference's behavior defines the relationship type between two database tables.

A database utilizes relationships between two tables through JOIN statements in a structured
query language (SQL).

Relationships in a database help create meaningful information. The association among entities
is called a relationship. For example, an employee works_at a department, a student enrolls in a
course. Here, Works_at and Enrolls are called relationships.

Relationship set: A set of relationships of similar type is called a relationship set. Like entities, a
relationship too can have attributes. These attributes are called descriptive attributes.

Degree of Relationship: The number of participating entities in a relationship defines the degree
of the relationship. Binary = degree 2, Ternary = degree 3 while n-ary = degree.

As a result, database relationships result in:

 Reduced data redundancy. Relationships help reference information stored in existing


tables, reducing repetition.
 Better organized databases. Relationships help implement database
normalization techniques. Normalization helps yield a better organized and robust
database.
 Referential integrity. As databases grow, joins, queries and sorting become expensive.
Relationships help reduce the number of transactions and improve data validity.

Established database relationships ensure an RDBMS is viable, flexible, and stable.

Relationships are represented by diamond-shaped box. Name of the relationship is written inside
the diamond-box. All the entities (rectangles) participating in a relationship are connected to it
by a line.

Binary Relationship and Cardinality: A relationship where two entities are participating is
called a binary relationship.
Cardinality is the number of instances of an entity from a relation that can be associated with
the relation. Cardinality defines the number of entities in one entity set, which can be associated
with the number of entities of other set via relationship set.

One-to-one Cardinality (1:1): One entity from entity set A can be associated with at most one entity of
entity set B and vice versa. A one-to-one (1:1) relationship means that each record in Table A
relates to one, and only one, record in Table B, and each record in Table B relates to one,
and only one, record in Table A.

One-to-many (1:n): One entity from entity set A can be associated with more than one entities
of entity set B, however an entity from entity set B can be associated with at most one entity.

Many-to-one (m:1): More than one entity from entity set A can be associated with at most one
entity of entity set B, however an entity from entity set B can be associated with more than one
entity from entity set A.
Many-to-many (m:n): One entity from A can be associated with more than one entity from B and vice
versa.

Participation Constraints
Total Participation: Each entity is involved in the relationship. Total participation is represented
by double lines.
Partial participation: Not all entities are involved in the relationship. Partial participation is
represented by single lines.

A relational database implements three different types of relationships:


1. One-to-one (1:1)
2. One-to-many (1:N)
3. Many-to-many (N:N)
A line connecting two tables represents a relationship, while the symbols on the line's end
represent the exact relationship type.
For example, in ER diagrams, "one" and "many" relationship cardinalities appear as the
following symbols:
 A perpendicular line (one).
 Crow's feet (many).
 A circle (zero).

The symbols combine to form complex relationship types.

Below are detailed explanations for each relationship type with examples.

One-to-One Relationship in a Database

A one-to-one relationship (1:1) in a database has one record on each side of the relationship.
Every primary key relates to at most one entry from another table, making the foreign key
unique.

One-to-one relationships appear from enforced business rules and are not typical. Combining
two tables with a one-to-one relationship does not break normalization rules.

One-to-One Relationship Example

A simple example to demonstrate a one-to-one relationship is with capital cities. One country (or
state) has only one capital city, and one capital city belongs to only one country (or state).
Two tables with information about countries and capital cities connect in a database using
a primary key. For example, when added to the country table, the unique ID of a capital city (its
primary key) becomes a foreign key, creating a relationship.

In this case, the one-to-one relationship is mandatory. Every country must have a unique capital
city, and the foreign key should be unique to ensure the connection is 1:1.

Look at the following example of tables from a company's Employees database:

PERSONAL
EmployeeI FirstNam LastNam Stat
Address City Zip
D e e e
2306
0708
EN1-10 Carol Schaaf Palisade Union City NJ
7
Ave.
1855 1239
EN1-12 Gayle Murray New York NY
Broadway 0
742
0703
EN1-15 Steve Baranco Forrest Kearny NJ
2
St.
416
0703
EN1-16 Kristine Racich Bloomfiel Hoboken NJ
0
d St.
24
Ritchfield 0766
EN1-19 Barbara Zumbo Central NJ
Park 0
Ave.
2
Weehawke 0708
EN1-20 Daniel Gordon Angelique NJ
n 7
St.
3600
0708
EN1-22 Jacqueline Rivet Bergeline Union City NJ
7
Ave.
1800
Weehawke 0708
EN1-23 Betsy Rosyln Boulevard NJ
n 6
East
2100 North 0704
EN1-25 Will Strick NJ
91st St. Bergen 7
240 Fifth 1001
EN1-26 Susan Shipe New York NY
Ave. 8
PAYROLL
EmployeeID PayRate
EN1-10 $25.00
EN1-12 $27.50
EN1-15 $20.00
EN1-16 $19.00
EN1-19 $22.75
EN1-20 $23.00
EN1-22 $22.50
EN1-23 $19.50
EN1-25 $12.50
EN1-26 $14.00

Above, tables with a one-to-one relationship from a database of information about employees

Each record in the Personal table is about one employee. That record relates to one, and only
one, record in the Payroll table. Each record in the Payroll table relates to one, and only one,
record in the Personal table. (This is what looking at it from both directions means).

In a one-to-one relationship, either table can be considered to be the primary or parent table.
One-to-Many Relationship in a Database

A one-to-many (1:N) relationship in a database has a single entry on one side and multiple
entries on the other end. Every primary key corresponds to one or more records from another
table. In this case, the foreign key is not unique.

A one-to-many (1:N) relationship means a record in Table A can relate to zero, one, or many
records in Table B. Many records in Table B can relate to one record in Table A.
The potential relationship is what's important; for a single record in Table A, there might be
no related records in Table B, or there might be only one related record, but there could be
many.

One-to-many relationships are natural and often appear as a logical connection in database
modeling.

One-to-Many Relationship Example

An example of a one-to-many relationship is the connection between a mother and children. A


mother can have many kids, but every child belongs to one mother only. One entity from entity set
A can be associated with more than one entities of entity set B, however an entity from entity set B can
be associated with at most one entity.

A database containing two tables with information about mother and children connects using
a primary key. When added to the child table, the unique ID from a mother becomes a foreign
key. Different children can have the same mother.

Look at the following tables about a company's Customers and Orders. The relationship is
mandatory on both ends, and at least one entry must exist in both tables to establish a connection.
CUSTOMERS
CustomerID CustomerName Address City State Zip
19
10273-
20151 Engel's Books International Ryebrook NY
9764
Dr
396 Apache Fountain 92708-
20493 Jamison Books CA
Ave Valley 4982
79 Gessner 77024-
20512 Gardening Galore Houston TX
Pk 6261
80237-
20688 Books Abound 51 Ulster St Denver CO
3386
687 Mountain 08276-
20784 Book World Stowe VT
Rd 3196
The Corner 36 N.Miller 13206-
20926 Syracuse NY
Booksotre Ave 4976
512 08876-
20932 Allendale Books Someville NJ
Columbia Rd 2987
In Between the 2008 Delta 45208-
21570 Cincinnati OH
Covers Ave 4468
Books and 51 Windsor 02139-
21587 Cambridge MA
Beyond St 2123
04982-
21965 Cover to Cover 12 Harbor St Burlington VT
2977
ORDERS
OrderNum CustomerID OrderDate ShipDate Shipper
76654 20151 2/1/00 2/6/00 USPS
74432 20151 6/30/99 7/2/99 Federal Express
75987 20151 11/10/99 11/12/99 UPS
62922 20493 9/5/99 9/6/99 UPS
65745 20493 10/1/99 10/3/99 USPS
72212 20493 4/22/00 4/25/00 UPS
73547 20493 8/17/99 8/20/99 UPS
69211 21570 5/12/99 5/12/99 Federal Express
70343 21587 10/2/00 10/4/00 UPS
72833 21587 12/14/99 12/17/99 UPS

Above, tables with data about customers and orders that have a one-to-many relationship

The Customers table holds a unique record for each customer. Each customer can (and, we hope,
does) place many orders. Many records in the Orders table can relate to only one record in the
Customers table. This is a one-to-many relationship (1:N) between the Customers table and the
Orders table.

In a one-to-many relationship, the table on the one side of the relationship is the primary table and
the table on the many side is the related table.

A one-to-many relationship is the most common relationship found between tables in a relational
database. The following is an example of a form used in a database to display the data from two
tables with a one-to-many relationship.

A form displaying data from tables with a one-to-many relationship

Many-to-Many Relationship in a Database

Many-to-many (N:N) relationships in a database have multiple entries on both ends of the
relationship. Since numerous entries may exist on both ends, a standard solution is to create
an association (junction, join) table with foreign keys from both tables.

Many-to-many relationships are a common practice with relational databases implemented in


web technologies, such as ecommerce websites.

Many-to-Many Relationship Example


A many-to-many relationship exists between books and authors. For example, a single book can
have multiple authors. Likewise, a single author can have numerous books.

If there is a table containing books and another with authors, the best way to establish the
relationship between the two is through a new table. The new table has foreign keys from both
parent tables, creating a many-to-many relationship.

Performing different types of JOIN queries fetches data from both tables efficiently while
protecting the original tables from redundancies.

Examine the sample data below. These tables hold data about employees and the projects to
which they are assigned. Each project can involve more than one employee and each
employee can be working on more than one project (the "do more with less" thing). This
constitutes a many-to-many (N:N) relationship.

EMPLOYEES
EmployeeID Last Name First Name ProjectNum
EN1-26 O'Brien Sean 30-452-T3
EN1-26 O'Brien Sean 30-457-T3
EN1-26 O'Brien Sean 31-124-T3
EN1-33 Guya Amy 30-452-T3
EN1-33 Guya Amy 30-482-TC
EN1-33 Guya Amy 31-124-T3
EN1-35 Baranco Steven 30-452-T3
EN1-35 Baranco Steven 31-238-TC
EN1-36 Roslyn Elizabeth 35-152-TC
EN1-38 Schaaf Carol 36-272-TC
EN1-40 Wing Alexandra 31-238-TC
EN1-40 Wing Alexandra 31-241-TC
PROJECTS
ProjectNum ProjectTitle EmployeeID
30-452-T3 Woodworking Around The House EN1-26
30-452-T3 Woodworking Around The House EN1-33
30-452-T3 Woodworking Around The House EN1-35
30-457-T3 Basic Home Electronics EN1-26
30-482-TC The Complete American Auto Repair Guide EN1-33
31-124-T3 The Sport Of Hang Gliding EN1-26
31-124-T3 The Sport Of Hang Gliding EN1-33
31-238-TC The Complete Baseball Reference EN1-35
31-238-TC The Complete Baseball Reference EN1-35
31-241-TC Improving Your Tennis Game EN1-40
35-152-TC Managing Your Personal Finances EN1-36
36-272-TC Using Electronic Mail Effectively EN1-38

Above, tables with a many-to-many relationship

You might also like