Keys, ER diagrams & database design
Computer Science · Databases
Question 1 of 10
0In an ER diagram, how is a relationship usually labelled?
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Keys, ER diagrams & database design explained
A relational database stores data in tables, where each row is a record and each column is a field. A primary key is a field, or combination of fields, that uniquely identifies each record in a table. Primary keys must be unique and must never be empty. A candidate key is any field that could serve as a primary key. A foreign key is a field in one table that holds the primary key value of a record in another table, and it is used to link the tables together.
An entity relationship (ER) diagram plans a database before it is built. Entities, such as Student or Course, are drawn as boxes, and their attributes are listed. Lines between entities show relationships, labelled with their cardinality: one to one, one to many, or many to many. A many to many relationship cannot be stored directly, so it is split into a third linking table, which holds foreign keys to both original tables.
Worked example
Students can enrol on many courses, and each course has many students. Design the linking table.
- Create an Enrolment table with a StudentID column (a foreign key to Student) and a CourseID column (a foreign key to Course).
- Make the pair (StudentID, CourseID) the primary key. This is a composite key, and it stops the same student enrolling on the same course twice.
- Add any further data about the enrolment, such as the date, as extra fields.
Student 7 enrolling on Course 3 gives the row (7, 3). Student 7 enrolling on Course 5 gives a second row (7, 5), so the many to many relationship is stored without repeating any student or course details.
What you will practise
81 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.
Example questions
- What is a primary key in a relational database table?
- A foreign key is:
- In an entity-relationship (ER) diagram, an entity represents:
- In an ER diagram, an attribute is:
- In an ER diagram, a relationship is:
- A candidate key is:
- A composite key is:
- Which two properties must a primary key have?
…and 73 more.