Lecture Slides available: PDF PowerPoint Mapping ER Models into RelationsContents
Overview
What is a relation?A relation is a table that holds the data we are interested in. It is two-dimensional and has rows and columns. Each entity type in the ER model is mapped into a relation.
Relations can be represented textually as:
tablename(primary key, attribute 1, attribute 2, ... , foreign key)
If matric_no was the primary key, and there were no foreign keys, then the table above could be represented as:
student(matric no, name, address, date_of_birth)
When referring to relations or tables, cardinality is considered to the the number of rows in the relation or table, and arity is the number of columns in a table or attributes in a relation. Foreign keysA foreign key is an attribute (or group of attributes) that is the primary key to another relation.
Preparing to map the ER modelBefore we start the actual mapping process we need to be certain that we have simplified the ER model as much as possible. This is the ideal time to check the model, as it is really the last chance to make changes to the ER model without causing major complications. Mapping 1:1 relationshipsBefore tackling a 1:1 relationship, we need to know its optionality. There are three possibilities the relationship can be:
Mandatory at both endsIf the relationship is mandatory at both ends it is often possible to subsume one entity type into the other.
When not to combineThere are a few reason why you might not combine a 1:1 mandatory relationship.
If not combined...If the two entity types are kept separate then the association between them must be represented by a foreign key.
Example
Staff(emp_no, name, cont_no, start, end, position, salary)
Staff(emp_no, name, contract_no) Contract(cont_no, start, end, position, salary)
Staff(emp_no, name) Contract(cont_no, start, end, position, salary, emp_no) Mandatory <->OptionalThe entity type of the optional end may be subsumed into the mandatory end as in the previous example. It is better NOT to subsume the mandatory end into the optional end as this will create null entries.
If we add to the specification that each staff member may have at most one contract (thus making the relation optional at one end).
Staff(emp_no, name, contract_no) Contract(cont_no, start, end, position, salary)
Staff(emp_no, name) Contract(cont_no, start, end, position, salary, emp_no) ExampleConsider this example:
Foreign key in Staff: Contract Table:
Staff Table:
However, Foreign key in Contract: Contract Table:
Staff Table:
As you can see, both ways store the same information, but the second way has no NULLs. Mandatory <->Optional - Subsume?The reasons for not subsuming are the same as before with the following additional reason.
Lecturer(lect_no, l_name, cno, c_name, type, yr_vetted, external)
Lecturer(lect_no, l_name) Course(cno, c_name, type, yr_vetted, external,lect_no) Summary...So for 1:1 optional relationships, take the primary key from the `mandatory end' and add it to the `optional end' as a foreign key. So, given entity types A and B, where A <->B is a relationship where the A end it optional, the result would be: A (primary key,attribute,...,foreign key to B) B (primary key,attribute,...) Optional at both ends...Such examples cannot be amalgamated as you could not select a primary key. Instead, one foreign key is used as before.
Staff_car(emp_no, name, reg_no, year, make, type, colour) what would be the primary key?
Mapping 1:m relationshipsTo map 1:m relationships, the primary key on the `one side' of the relationship is added to the `many side' as a foreign key. For example, the 1:m relationship `course-student':
Course(course_no, c_name) Student(matric_no, st_name, dob)
Course(course_no, c_name) Student(matric_no, st_name, dob, course_no)
Mapping n:m relationshipsIf you have some m:n relationships in your ER model then these are mapped in the following manner.
Student(matric_no, st_name, dob) Module(module_no, m_name, level, credits)
Student(matric_no, st_name, dob) Module(module_no, m_name, level, credits) Studies(matric_no,module_no) This is equivalent to:
Student(matric_no,st_name,dob) Module(module_no,m_name,level,credits) Study() Summary
|
|