Journal logo

What are Keys in Database and Types of Keys With Examples Tables

What are the keys we use in the database? Different types of keys with examples.

By Intelligent CreatorPublished 3 years ago 6 min read
What are Keys in Database and Types of Keys With Examples Tables
Photo by Jan Antonin Kolar on Unsplash

What are Keys in Database and Types of Keys With Examples Tables

This article is about keys in Database and its type With Examples. Keys in the database are essentials topics of database management courses. So you must have a very clear concept because keys are used very frequently in the database management system. In these articles, the outlines are only ‘keys and its different types.

What is key in the database?

A Key in the database is a single attribute or set of attributes that can be used for a purpose to access or identify a specific entity instance uniquely. The database store hundred, thousand, and even more. And every entity type has different and many instances even in thousands or more. Now Occasionally If we want to pick or identify a single or particular instance then the key is the ultimate solution do it simply.

Example

In Wapda there are thousands of records of one city. They need to access all the records in the database to print the bills. Sometimes there may be some mistakes in the bill to rectify that mistake they must access your record out of these thousands. Would they do manually be searching at each register?

The answer is No. They would certainly use the computers to access your records but here the second problem is there may be many ‘Names’ similar to yours. Here to resolve these problems and access simply to specific records Key is the final solution HOW.

Identify and fix an attribute known as (key attributes) in the database, particular entity type add many instances then the values of these entity instances cannot be duplicated for that attribute. E.g. Bill Number, ID card Number.

If you think about the national ID card number and set it as key, then no matter how much the records is you can access to particular record precisely. This is the job of the designer to identify these attributes and it works as a KEY.

Key attributes serve two purposes. First, it expresses the information that is stored in its value. Second, it’s being used to identify the records from the instances of that entity type.

Before going to types of you must know the difference between Simple Key and Composite Key.

Simple Key in the database

A key that has only one attribute is known as the simple key, e.g. student ID, item No. but this is not necessary that the key must have a single attribute it can be multiple.

Composite Key in database

Sometimes the situation is that we cannot identify the single key. Then the possibilities are to combine two attributes and let’s have Key. Then entering a lot of instances of entity types two records values are not the same. So a key consisting of more than one attribute is known as a composite key e.g. program-code, course-code.

Example of Composite Key

If you want to store data about college in which program which courses are offering and you declare a program code as a KEY. Then it’s not possible because there are multiple courses in the program.

Also affirming course code as a key is not possible because there may be the same code in multiple programs see in the table below. To resolve these problems combine course code and program code, it makes a composite Key.

Program Code Course Code Credit Hours

MBA DBS 3

BCS NW 3

MCS DBS 3

MCS DS 3

There are different types of Keys in the database.

  • Super Key
  • Candidate Key
  • Primary Key
  • Alternate Key
  • Secondary Key
  • Foreign Key

Super Key

Super Key in the database is a single attribute or set of attributes that can be used for a purpose to access or identify a specific entity instance uniquely. (Same as Key above) e.g. for Student entity type student ID is Key. This Key works as Super Key.

The composite key can also be a super key. Because the composite key can also be used to identify the entity instance uniquely. If you add more attribute with composite key and it works is to identify particular records it is considered as super key.

Candidate Key

A candidate keys in the database is a super key that does not contain extra attributes. Extra attributes mean if you eliminate it, can access particular records it might have more than one attributes to uniquely identify an entity. E.g. Name, Address.

A superkey which have a single attribute or composite but for the candidates key it must be minimum for uniqueness identification.

Program Code Course Code Credit Hours

MBA DBS 3

BCS NW 3

MCS DBS 3

MCS DS 3

In this table you declare that Program code and course Code are the Key (composite Key) then this is the super key as well as the candidate’s key because it is a minimum number of the attribute to identify the uniqueness.

Also if you declare the credit hours as the key then its super key but not candidate kay, credit hours is the same for all, you can say it’s additional.

Primary Key

A primary key in the database is the main candidate key from the possible set of candidate’s keys chosen by the database designer that is most suitable for entity identification.

An entity type might have more than one candidates key not frequently, in that case, the database designer declares one key to be used for that entity type, called a primary key. If there is only one candidate key by default it would be considered as the primary key.

NOTE: Whenever we use the term key we mean by it the Primary Key. Its value can’t be duplicated.

Major characteristics of the primary key that it can’t have NULL values, mean ‘’not given’’ of ‘’not definite’’. Also, its values can’t be duplicated.

NOTE: Zero does not mean NULL.

Which one to consider a primary key? There are certain things that are generally considered while making this decision. Like, select the candidate key which is small in size mean that has less number of attributes.

For example, if you have two candidates' keys. One has single attributes and others have three. Then use the single one as a primary key. You will identify easily entering the values of one attribute rather than entering the values of two attributes.

The Second criteria are if both attributes are single then chose the one which is easy to remember for that specific organization.

NOTE: Every candidate key is super key. And every primary key is a candidate’s key. But one key from the set of candidate’s keys would be declared as the primary key.

Alternate key

Candidate’s keys in the database which are not selected as primary key are called an alternate key. For example, you have two candidates’ keys to a student the registration number and Student Id. If you chose both as primary key surely it would be used for uniqueness but quite difficult to remember and type.

In such case you select the registration number as the Primary key then student ID will be the alternate key.

Secondary key

The secondary keys in the database is an attribute or set of attributes on which you need to access the instances of an entity type that may not necessarily return unique.

For example, if you want to see how many of your students belong to Model Town, in that case, you will access those instances of student entity types that contain ‘Model Town’ in their address. In this case, the address is called a secondary key.

If you still have a question in your mind after reading this ‘Keys in Database and it’s Types With Examples’ feel free to give us a comment. Do share this with others.

Read More Engineering related Articles On Vocal:

Differences between CNC, DNC, and VMC

Types of Management information system

What is the Difference Between NC and CNC Machines? Full Explained

how tohow to

About the Creator

Intelligent Creator

My posts are informational & something I do great research for SEO before writing it. Writing posts to make you more intelligent.

Work with OFAgency: Join OFAgency.co as a Creator here

Sell Feet Pics on FeetFinder here.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.