Keys in the ER Model

A key attribute distinguishes one entity from another. Understanding super keys, candidate keys, primary keys and partial keys at design time prevents most identity problems later.

Concept

Every entity in an entity set must be distinguishable from every other. A key is the attribute or set of attributes that makes that possible. Phase 5 treats keys formally in the relational model; this note covers what you decide while still drawing the diagram.

The key types

KeyDefinitionExample
Super keyAny attribute set that uniquely identifies an entity. May contain extra attributes.{roll_no}, {roll_no, name}, {roll_no, name, dob}
Candidate keyA minimal super key — remove any attribute and uniqueness is lost.{roll_no}, {email}
Primary keyThe candidate key chosen to identify entities. Underlined in the diagram.{roll_no}
Alternate keyA candidate key not chosen as primary.{email}
Composite keyA key made of more than one attribute.{roll_no, course_code}
Partial keyIdentifies a weak entity only within its owner. Drawn with a dashed underline.{dependant_name} within one employee
   EVERY candidate key is a super key.
   NOT every super key is a candidate key.
   EXACTLY ONE candidate key becomes the primary key.
   The rest become alternate keys.

   super keys
   +--------------------------------------+
   |  {roll_no, name, dob}                |
   |  {roll_no, name}          +--------+ |
   |  {email, dob}             |candidate| |
   |                           | {roll_no}| |  <- minimal
   |                           | {email}  | |
   |                           +--------+ |
   +--------------------------------------+

Choosing a primary key

Judge every candidate against these five tests.

  1. Unique — no two entities may share the value, ever.
  2. Never null — every entity must have one, from the moment it exists.
  3. Stable — it must not change. Changing a primary key means changing every reference to it.
  4. Minimal — as few attributes as will do the job.
  5. Simple — short and easy to compare and index.

Natural and surrogate keys

Natural keySurrogate key
What it isAn attribute with real world meaningA value generated only to identify the row
Exampleroll number, vehicle registrationan auto generated id
AdvantagesMeaningful, already known to users, avoids an extra columnNever changes, never null, always short
DisadvantagesReal world values do change, and get reissuedMeaningless to users, needs a unique constraint on the natural key anyway
Whichever you choose, still declare a uniqueness constraint on the natural key. A surrogate key alone will happily allow the same student to be inserted twice under two different generated ids.

Example

   STUDENT ( roll_no, email, aadhaar_style_id, name, dob )

   candidate keys   {roll_no}, {email}, {aadhaar_style_id}
   primary key      {roll_no}   stable, never null, short,
                                already used by everyone
   alternate keys   {email}, {aadhaar_style_id}

   Why not email as primary?
     students change their email address, and a primary key
     must be stable. Keep it as an alternate key with a
     uniqueness constraint.

   Why not a national identifier?
     it is sensitive personal data, it may be absent for some
     students, and it should not be scattered across every
     table that references a student.

That last point is a genuine design and privacy rule: do not use sensitive personal identifiers as primary keys, because a primary key is copied into every table that refers to it.

Common mistakes

  • Saying a candidate key is any unique attribute set. Minimality is what separates candidate from super.
  • Choosing a changeable attribute. Phone numbers, names and email addresses all change.
  • Assuming a composite key is bad design. For a relationship such as enrolment, a composite key is exactly right.
  • Forgetting the partial key on weak entities. It is drawn with a dashed underline and it is examined.
  • Using a name as a key. Names are not unique and they change.

Exam and interview questions

  1. Define super key, candidate key, primary key, alternate key and composite key with examples.
  2. Why is every candidate key a super key but not the reverse?
  3. State five criteria for choosing a primary key.
  4. Compare natural and surrogate keys and say when you would choose each.
  5. What is a partial key and which kind of entity has one?

Practice

  1. For BOOK ( isbn, title, author, accession_no, publisher ), list the super keys, candidate keys and choose a primary key with reasons.
  2. Explain in three sentences why a mobile number is a poor primary key.
  3. Give one case where a composite key is the correct choice and say why a surrogate would be worse.

Conclusion

Super keys identify, candidate keys identify minimally, and one candidate becomes the primary key. Choose it to be unique, never null, stable, minimal and simple — and never choose sensitive personal data.

Useful resources

Hand picked references for this topic
Written by Lorens Mishra

Software Engineer Notes Management System Administrator

Continue reading

All DBMS notes →
DBMS

Strong and Weak Entities

A weak entity has no key of its own and can only be identified through an owner entity. It is drawn with a double rectangle, joined by an identifying...

Read more

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.