0% found this document useful (0 votes)
20 views5 pages

Different Types of Relationships in SQL

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)
20 views5 pages

Different Types of Relationships in SQL

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/ 5

Different types of relationships in SQL

1) One to One → each record in the first table (parent


table) is associated with exactly one record in the
second table (child table), and vice versa.
person_id | name | age
--------------------------
1 | Alice | 25
2 | Bob | 30

passport_id | passport_number | expiry_date


-------------------------------------------
101 | AB123456 | 2025-12-31
102 | CD789012 | 2024-08-15

person_id | passport_id
-------------------------
1 | 101
2 | 102

This represents a one-to-one relationship between


people and passports, where each person has exactly
one passport, and each passport is associated with
exactly one person.

2) One to Many → each record in the first table (parent


table) is associated with one or more records in the
second table (child table), and vice versa.
customer_id | name | email
----------------------------------
1 | Alice | [email protected]
2 | Bob | [email protected]

order_id | customer_id | order_date


-------------------------------------
101 | 1 | 2024-04-15
102 | 1 | 2024-04-16
103 | 2 | 2024-04-16

With the help of one customer id the user can order


multiple products.

3) Many to Many → each record in the parent table can


be associated with multiple records in the child table.
student_id | name | age
----------------------------
1 | Alice | 20
2 | Bob | 22
3 | Charlie| 21

course_id | course_name | instructor


-------------------------------------
101 | Math | Dr. Smith
102 | Science | Prof. Johnson
103 | English | Prof. Davis
student_id | course_id
------------------------
1 | 101
1 | 103
2 | 102
3 | 101
3 | 103

This represents a many-to-many relationship between


students and courses, where each student can be
enrolled in multiple courses, and each course can
have multiple students.

4)Many to One
It is a reverse process of One to Many

department_id | name | location


--------------------------------------
1 | IT | New York
2 | Marketing | London

employee_id | name | position


--------------------------------
101 | Alice | Developer
102 | Bob | Manager
103 | Charlie | Designer
employee_id | department_id
----------------------------
101 |1
102 |2
103 |1

This represents a many-to-one relationship between


employees and departments, where multiple
employees can belong to the same department.
Or we can consider multiple orders done by one
person.

Students to teachers—> one to many(One student can


have multiple teachers.)

Students to subjects → Many to many(One student can


learn multiple subjects.Each subject can be learned by
multiple students.)

Teachers to Students → one to many(One teacher can


have multiple students.)

Teachers to Subjects —> many to many (One teacher can


teach multiple subjects. Each subject can be taught by
multiple teachers.)
Subjects to students →Many to Many(One subject can
have multiple students.Each student can study multiple
subjects.)

Subjects to teachers —> One to many (One subject can be


taught by multiple teachers.)

You might also like