Relationships

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

Relationships

CMPSC140
Relationships
• Definition
– An association between common fields (columns) in two tables
– One-to-One (1:1)
– One-to-Many (1:N)
– Many-to-Many (M:N)
• Advantages
– You can join and view information together from several tables
• How to define relationship
– By matching common fields – usually a field with the same name in
both tables ( one for primary, the other for foreign key)
Relationships
 Keys
– Primary key: A unique value for each record
– Foreign key: A copy of a primary key which can appear in other table
– Composite Primary Key: A primary key that made up of more than
one attribute

Student Table Enrolment Table Course Table Teacher Table


Field Name Field Name Field Name Field Name
StudentID (PK) StudentID (CPK) CourseID (PK) TeacherID (PK)
StudentName CourseID (CPK) CourseName TeacherName
TeacherID
(FK)
One-To-One (1:1)
• Each record in Table A can have only one matching record in
Table B, and each record in Table B can have only one matching
record in Table A
• Any time two tables have the same primary key and are joined
by a foreign key, a one-to-one relationship exists
• Example
One-To-Many (1:N)
• A record in Table A can have many matching records in Table
B, but a record in Table B has only one matching record in
Table A
• Use a foreign key on the many side of the relationship linking
back to the "one" side
• One side table is a primary table
• Example
Teacher Course
teacherID courseID
1 N
teacherName courseName
teacherID (FK)
Referential Integrity
• Referential integrity allows you to maintain the integrity
between related tables
• Referential integrity specifies that when you add a record to a
related table, there must be a matching record in the primary
table
• If you choose to enforce referential integrity, you can ensure
that you will not have orphaned records (records that have no
matching record in the primary table)
• Example:
– Teacher and Course tables, In order to add one new course, its
teacherID already exists in the Teacher table.
Referential Integrity
Cascade Update

You must enforce


the Referential
Integrity rules first then
you can put a
checkmark in Cascade
Update Related Fields.
Cascade Update
• Once we have Enforced Referential Integrity and
have put a checkmark in the
• Cascade
• Update
• We have now made making changes easier!
• How do we do this?
– Once you make a change in the Primary Table (teachers)
and save that record, the changes will be made
automatically in the Related Table (students).
Cascade update: Example
• I am going to make the field of tid change from T2 to T22.
• I open the teachers (the Primary table) and make the change
on that record and then save the table.
Many-To-Many (M:N)
• A record in Table A can have many matching records in Table
B, and a record in Table B can have many matching records in
Table A.
• This type of relationship is only possible by defining a third
table whose primary key consists of two fields: the primary
keys from both Table A and B
• A many-many relationship between two tables must be
resolved into two one-many relationships that is done by
taking the primary key of each table and combining them into
a composite primary key for a new table
Many-To-Many (M:N)
• One student can take many courses Student Course
• One course can have many students studentID courseID
N M
studentName courseName

Student Course
studentID courseID
studentName courseName
1 1

Enrollment
M StudentID (CPK) M
CourseID (CPK)
Relationships

Course
Student
CourseID (PK)
StudentID (PK) 1 M
1 CourseName
StudentName
teacherID(FK)

Enrollment Teacher
StudentID (CPK) TeacherID (PK)
M 1
CourseID (CPK) M TeacherName

You might also like