Entities and Attributes
An entity is a real-world thing that can be told apart from others, such as a student or a book. An entity type is the description of a group of similar entities (STUDENT with the attributes roll number, name and phone), and an entity set is the collection of entities of that type present in the database at a time. A weak entity has no key of its own and depends on an owner entity (a dependent of an employee).
Attributes are of several kinds:
- Simple or composite: name may be composite (first, middle, last).
- Single-valued or multivalued: a person has one date of birth but may have many phone numbers.
- Stored or derived: age is derived from the date of birth.
- Null: the value is unknown or not applicable.
Keys
A super key is a set of attributes that identifies an entity uniquely. A candidate key is a minimal super key and the primary key is the candidate key chosen by the designer. A foreign key refers to the primary key of another table.
Relationship Types
A relationship type associates entity types (STUDENT enrolls in COURSE) and a relationship instance is one actual association. The degree is the number of participating entity types (binary, ternary). The cardinality ratio is 1:1, 1:N or M:N, and participation is total (every entity must take part) or partial. A relationship may have attributes of its own, such as the grade of an enrolment.
ER Diagram and Mapping to Tables
In an ER diagram rectangles are entity types, ovals are attributes, diamonds are relationships and a double line shows total participation. Mapping rules:
- Each strong entity becomes a table whose columns are its simple attributes.
- Each multivalued attribute becomes a separate table with the owner's key.
- A 1:N relationship is stored by placing the key of the 1 side as a foreign key in the N side.
- An M:N relationship becomes a new table that holds both keys and the relationship attributes.
Definition: Cardinality ratio. The maximum number of entities of one type that can be associated with an entity of the other type in a relationship: 1:1, 1:N or M:N.
Table 1.1: ER constructs and their mapping
| Construct | Symbol | Becomes |
|---|---|---|
| Strong entity | Rectangle | A table |
| Multivalued attribute | Double oval | A separate table |
| M:N relationship | Diamond | A table with both keys |
| 1:N relationship | Diamond | Foreign key on the N side |
Example 1.1: Mapping an ER diagram to tables
Problem: An ER diagram has three strong entities STUDENT, COURSE and INSTRUCTOR, a multivalued attribute phone of STUDENT, an M:N relationship ENROLLS between STUDENT and COURSE with the attribute grade, and a 1:N relationship TEACHES between INSTRUCTOR and COURSE. How many tables are needed?
Solution:
- The three strong entities give 3 tables.
- The multivalued attribute phone gives 1 more table, STUDENT_PHONE.
- The M:N relationship ENROLLS gives 1 table with sid, cid and grade.
- TEACHES is 1:N, so the key iid is added to COURSE; no extra table.
Result: 3 + 1 + 1 = 5 tables.
import sqlite3
db = sqlite3.connect(":memory:")
db.executescript("""
CREATE TABLE student(sid INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE student_phone(sid INTEGER, phone TEXT);
CREATE TABLE instructor(iid INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE course(cid INTEGER PRIMARY KEY, title TEXT, iid INTEGER);
CREATE TABLE enrolls(sid INTEGER, cid INTEGER, grade TEXT);
INSERT INTO student VALUES (1, 'Asha'), (2, 'Bimal');
INSERT INTO instructor VALUES (10, 'Dr Rao');
INSERT INTO course VALUES (101, 'DBMS', 10), (102, 'Networks', 10);
INSERT INTO enrolls VALUES (1, 101, 'A'), (1, 102, 'B'),
(2, 101, 'B');
""")
sql = "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'"
print("Tables:", db.execute(sql).fetchone()[0])
q = """SELECT s.name, c.title, e.grade FROM enrolls e
JOIN student s USING(sid) JOIN course c USING(cid)"""
for row in db.execute(q):
print(row)
Output
Tables: 5
('Asha', 'DBMS', 'A')
('Asha', 'Networks', 'B')
('Bimal', 'DBMS', 'B')
Common mistakes
- Making a table for a 1:N relationship instead of using a foreign key.
- Putting a multivalued attribute in the entity table, which breaks first normal form.
- Confusing a super key with a candidate key; a candidate key is minimal.
- Forgetting the relationship attribute (grade) when mapping an M:N relationship.
Key takeaways
- Entity type, entity set, attributes and keys form the base of the ER model.
- Cardinality is 1:1, 1:N or M:N; participation is total or partial.
- Multivalued attributes and M:N relationships need their own tables.
- 1:N relationships use a foreign key on the N side.
Practice questions
- What is a weak entity?
- How is an M:N relationship mapped?
- What is a candidate key?
Answers to the practice questions
- An entity without a key of its own that depends on an owner entity.
- To a new table containing both primary keys and the relationship attributes.
- A minimal set of attributes that identifies a record uniquely.
Sources
- Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit I
