Let's Learn The Database And It's Types...

Let's Learn The Database And It's Types...

In this article, we will learn about types of keys in database like primary, super, alternate, composite, candidate, foreign key.

ยท

4 min read

Let's get started..

First, I will explain what are keys in DBMS?

  • A key in an attribute or set of attributes uniquely identifies any record from the table.

  • It is also used to establish and identify relationships between tables.

  • The key is used to uniquely identify any record or row of data from the table.

  • example:

In the above-given example, Employee ID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID.

I hope my lovely learner must have understood what is a database ๐Ÿค— ๐Ÿค—.

Now let us discuss the next topic which is the types of database keys.

Types of keys

  1. Primary key

  2. Candidate Key

  3. Alternate Key

  4. Foreign key

  5. Super Key

  6. Composite key

1. Super key

A super key is a combination of all possible attributes that can uniquely identify the row in the given relation.

  • The super key is a superset of a candidate key

  • A table can have many super keys.

  • A super key may have additional attributes that are not needed for a unique identity.

  • Example:

2. Candidate Key

  • A candidate key is an attribute or set of an attribute that can uniquely identify a tuple.

  • A candidate key is a minimal super key or a super key with no redundant attribute.

  • It is called a minimum super key because we select a candidate key from a set of super key such that the selected candidate key is the minimum attribute required to uniquely identify the table.

  • Candidate key are defined as a distinct set of attributes from which the primary key can be selected.

  • candidate key are not allowed to null value.

  • Example:

3. Primary Key

  • a primary key is one of the candidate key chosen by the database designer to uniquely identify the tuple in the relation.

  • The value of a primary key can never be Null

  • The value of the primary key must always be unique (not duplicate).

  • The value of the primary key can never be changed i.e no updation is possible the value of the primary key must be assigned when inserting a record.

  • a relation is allowed to have only one primary key.

  • Example:

Here we have learned 3 types of database, take a break and revise the topic for 2 minutes.

I hope you have revised the topic, let's start with the another 3 topics

4. Alternate Key

Out of all candidate keys, only one gets selected as a primary key, the remaining key is known as an alternate key.

  • Example:

    In the employee table

  • employee_ID is the best suited for the primary key.

  • The rest of the attributes like Aadhar_number and Email_id are considered as an alternate key.

5. Foreign key

  • a key used to link two tables together.

  • an attribute Or a set of attributes is one table that refers to the primary key in another table.

  • the purpose of the foreign key is to ensure or maintain the referential integrity of the data.

Example:

  • foreign key reference the primary key of the table.

  • a foreign key can take only those values which are present in the primary key of the reference relation.

  • the foreign key may have a name other than that of a primary key.

  • a foreign key can take the null values there is no restriction on a foreign key to be unique foreign key is not unique most of the time.

  • referenced relation me also called a master table or a primary table.

  • reference in a relationship may also be called the foreign table.

6. composite key

A that has more than one attribute is known as a composite key it is also known as a compound key.

Example:

Here we complete the topic database and its types of keys, I hope you have enjoyed reading this article โค๏ธ, if you want me to give any suggestions please comment in the comment section ๐Ÿ‘‡๐Ÿ‘‡ .

ย