The document discusses primary keys and foreign keys in entity relationship diagrams (ERDs). It defines a primary key as a unique identifier for each instance of an entity. A foreign key completes a relationship by identifying the parent entity and maintains referential integrity between entities. The document then provides an example of designing an ERD for a college, identifying the entities, relationships, keys, and other attributes to draw the complete diagram.
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Primary Key & Foreign Key part10
1. Introdaction to Python
Dr. Mohammed Kassim Page 41
Primary Key & Foreign Key - ERD
The primary key
The primary key is an attribute or a set of attributes that uniquely identify a specific
instance of an entity. Every entity in the data model must have a primary key
whose values uniquely identify instances of the entity.
To qualify as a primary key for an entity, an attribute must have the following
properties:
It must have a non-null value for each instance of the entity
The value must be unique for each instance of an entity
The values must not change or become null during the life of each entity
The primary key cannot have repeating values
Foreign key
A foreign key is an attribute that completes a relationship by identifying the parent
entity. Foreign keys provide a method for maintaining integrity in the data (called
Primary key
Foreign key
2. Introdaction to Python
Dr. Mohammed Kassim Page 42
Primary Key & Foreign Key - ERD
referential integrity) and for navigating between different instances of an entity.
Every relationship in the model must be supported by a foreign key.
Primary and foreign keys are the most basic components on which relational
theory is based. Each entity must have an attribute or attributes, the primary key,
whose values uniquely identify each instance of the entity. Every child entity must
have an attribute, the foreign key ,that completes the association with the parent
entity.
Example: design Entity Relationship diagram model for a college?
We have the following statements:
1. A college contains many departments
2. Each department can offer any number of courses
3. Many instructors can work in a department
4. An instructor can work only in one department
5. For each department there is a Head
6. Each instructor can take any number of courses
3. Introdaction to Python
Dr. Mohammed Kassim Page 43
Primary Key & Foreign Key - ERD
7. A course can be taken by only one instructor
8. A student can enroll for any number of courses
9. Each course can have any number of students
Step 1: Identify the Entities
What are the entities here?
1. Department
2. Course
3. Instructor
4. Student
Stem 2: Identify the relationships
1. One department offers many courses. But one particular course can be
offered by only one department. hence the cardinality between department
and course is One to Many (1:M)
2. One department has multiple instructors . But instructor belongs to only one
department. Hence the cardinality between department and instructor is One
to Many (1:M)
3. One course can be enrolled by many students and one student can enroll for
many courses. Hence the cardinality between course and student is Many to
Many (M:M)
4. One course is taught by only one instructor. But one instructor teaches many
courses. Hence the cardinality between course and instructor is Many to One
(M :1)
4. Introdaction to Python
Dr. Mohammed Kassim Page 44
Primary Key & Foreign Key - ERD
Step 3: Identify the key attributes
"Departmen_Name" can identify a department uniquely.
Course_ID is the key attribute for "Course" Entity.
Student_ID is the key attribute for "Student" Entity.
Instructor_ID is the key attribute for "Instructor" Entity.
Step 4: Identify other relevant attributes
For the department entity, other attributes are location
For course entity, other attributes are course_name,duration
For instructor entity, other attributes are first_name, last_name, phone
For student entity, first_name, last_name, phone
Step 5: Draw complete ER diagram
By connecting all these details, we can now draw ER diagram as given below.
5. Introdaction to Python
Dr. Mohammed Kassim Page 45
Primary Key & Foreign Key - ERD
Entity Relationship (ER) model for a college
M
M
M
M