0% found this document useful (0 votes)
16 views

Chapter 7 - Table Relationships

The document discusses different types of relationships between database tables: primary key, foreign key, one-to-one relationship, one-to-many relationship, and many-to-many relationship. Examples are given for each type of relationship.

Uploaded by

Derp Derpington
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)
16 views

Chapter 7 - Table Relationships

The document discusses different types of relationships between database tables: primary key, foreign key, one-to-one relationship, one-to-many relationship, and many-to-many relationship. Examples are given for each type of relationship.

Uploaded by

Derp Derpington
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/ 12

CHAPTER 7

Table Relationships

Copyright © 2021 TalentLabs Limited


AGENDA
• Primary Key
• Foreign Key
• Relationships

Copyright © 2021 TalentLabs Limited


Primary Key

Copyright © 2021 TalentLabs Limited


Primary Key (PK)
• An unique identifier of the records, kind of like an ID
• e.g. For a city, citizensʼ names can be repeated. We add an id column to
distinguish each citizen
• e.g. For an eCommerce Platform, purchases can be repeated. (same
user purchases the same drink twice). So weʼll add a transaction_id to
each purchase record

Citizen Transactions
ID First_name Last_Name Transaction_ID User Product

1 Darren Chiu 1 Darren Coke X 2

2 Peter Chow 2 Peter Burger X 2

3 Michelle Ling 3 Anthony Chips

4 Anthony Chiu 4 Darren Coke X 2

Copyright © 2021 TalentLabs Limited


Foreign Key

Copyright © 2021 TalentLabs Limited


Foreign Key (FK)

• Foreign key is used to establish relationship between two tables


• e.g. For a movie ratings table, the movie_id column is created for
identifying which movie the ratings is for. The movie_id column
is “foreign key” column.
movies ratings

id movie_id

title rating

year

students exam_scores

id student_id

name score

class
Copyright © 2021 TalentLabs Limited
Relationships

Copyright © 2021 TalentLabs Limited


What is Table Relationships

• We categorize data and organize the data into different tables


• For example, we put “movie details” in movies table, and the
“movies ratings” into ratings table
• Different tables have relationships between them (e.g. each
ratings is attached to a movie)

movies ratings

id movie_id

title rating

year

Copyright © 2021 TalentLabs Limited


Three types of relationships

One-to-One relationship

One-to-Many relationship

Many-to-Many relationship

Copyright © 2021 TalentLabs Limited


One-to-One Relationship
• One-to-One relationship are usually used to separate a big table into two,
or attaching additional data to a record.
• E.g.
• users and users_profile (separate a big users table to users and
users_profile)
• citizens and passport_info (attaching passport_info to citizen)

users users_profile
ID First_name Last_Name User_id Gender Country Last_Online Number of Posts

1 Darren Chiu 1 M Hong Kong 2019-12-06 3

2 Peter Chow 2 M Japan 2019-12-05 2

3 Michelle Ling 3 F Malaysia 2019-12-07 6

4 Anthony Chiu 4 M Hong Kong 2019-12-06 5

Copyright © 2021 TalentLabs Limited


One-to-Many relationship
• One-to-Many relationship usually refers to ownership relationships
• E.g.
• users and users_uploads (users owning multiple file uploaded)
• country and states (country owning multiple states)

users_uploads
users User_id Post_ID Image Text

ID First_name Last_Name 1 1 img1.jpg This is my first post.

1 Darren Chiu 2 2 img2.jpg Hi Everyone!

2 Peter Chow 3 3 img3.jpg Please follow my page!

3 Michelle Ling 1 4 img4.jpg Another post!

4 Anthony Chiu 2 5 img5.jpg I love posting!

Copyright © 2021 TalentLabs Limited


Many-to-Many relationship
• Many-to-Many relationship usually refers to a membership
between two categories of data
• E.g.
• movies and actors (each actor is a “member” of the
Movies_Actors
multiple movies)
movie_id actor_id
• desserts and ingredients (each ingredient is a “member
1 1
of multiple dessert recipes)
1 3

Movies Actors 1 4

id movie_name id name 2 1

1 Toy Story 1 Darren 2 2

2 Star Wars 2 Anthony 3 1

3 Harry Potter 3 Peter 3 4

4 Karl 4 1

Copyright © 2021 TalentLabs Limited

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy