Question :

What are keys? Discuss its types.

Subject

Database Management System

Standard

Computer Science Engineering

Views

584

Asked By

Deepak

Emily
Answer / Solution

In a database table, a key is a column or combination of columns that uniquely identify each row in the table.

There are several types of keys that can be used in database tables, including:

  • Primary Key: A primary key is a column or combination of columns that uniquely identifies each row in a table. It is typically used as the main way to identify and relate records to each other.
  • Foreign Key: A foreign key is a column or combination of columns that references the primary key of another table. It is used to establish relationships between tables.
  • Candidate Key: A candidate key is a column or combination of columns that could potentially serve as the primary key of a table, but is not currently being used as such.
  • Unique Key: A unique key is a column or combination of columns that uniquely identifies each row in a table, similar to a primary key. However, unlike a primary key, a table can have multiple unique keys.
  • Composite Key: A composite key is a combination of two or more columns that uniquely identifies each row in a table.

Keys are important in a database because they help to ensure data integrity and consistency, and they facilitate efficient querying and manipulation of data.

Angelina
Answer / Solution

Primary key

A primary key is a column or a set of columns that uniquely identifies each row in a table. It cannot have null values, and each value must be unique. A table can have only one primary key.

  • It is the first key used to identify one and only one instance of an entity uniquely. An entity can contain multiple keys, as we saw in the PERSON table. The key which is most suitable from those lists becomes a primary key.
  • In the EMPLOYEE table, ID can be the primary key since it is unique for each employee. In the EMPLOYEE table, we can even select License_Number and Passport_Number as primary keys since they are also unique.
  • For each entity, the primary key selection is based on requirements and developers.

Unique key:

A unique key is a column or a set of columns that must have unique values, but unlike a primary key, it can have null values.

Candidate key

A candidate key is a column or a set of columns that could potentially be used as a primary key. It must also be unique and cannot have null values.

  • A candidate key is an attribute or set of attributes that can uniquely identify a tuple.
  • Except for the primary key, the remaining attributes are considered a candidate key. The candidate keys are as strong as the primary key.

For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the attributes, like SSN, Passport_Number, License_Number, etc., are considered a candidate key.

Super Key

Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a candidate key.

For example: In the above EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME), the name of two employees can be the same, but their EMPLYEE_ID can't be the same. Hence, this combination can also be a key.

The super key would be EMPLOYEE-ID (EMPLOYEE_ID, EMPLOYEE-NAME), etc.

Foreign key

A foreign key is a column or a set of columns in one table that refers to the primary key of another table. It is used to establish a relationship between two tables.

  • Foreign keys are the column of the table used to point to the primary key of another table.
  • Every employee works in a specific department in a company, and employee and department are two different entities. So we can't store the department's information in the employee table. That's why we link these two tables through the primary key of one table.
  • We add the primary key of the DEPARTMENT table, Department_Id, as a new attribute in the EMPLOYEE table.
  • In the EMPLOYEE table, Department_Id is the foreign key, and both the tables are related.

Alternate key

There may be one or more attributes or a combination of attributes that uniquely identify each tuple in a relation. These attributes or combinations of the attributes are called the candidate keys. One key is chosen as the primary key from these candidate keys, and the remaining candidate key, if it exists, is termed the alternate key. 

In other words, the total number of the alternate keys is the total number of candidate keys minus the primary key. The alternate key may or may not exist. If there is only one candidate key in a relation, it does not have an alternate key.

For example, employee relation has two attributes, Employee_Id and PAN_No, that act as candidate keys. In this relation, Employee_Id is chosen as the primary key, so the other candidate key, PAN_No, acts as the Alternate key.

Composite key

Whenever a primary key consists of more than one attribute, it is known as a composite key. This key is also known as Concatenated Key. A composite key is a key that consists of two or more columns in a table that together uniquely identify each row.

For example, in employee relations, we assume that an employee may be assigned multiple roles, and an employee may work on multiple projects simultaneously. So the primary key will be composed of all three attributes, namely Emp_ID, Emp_role, and Proj_ID in combination. So these attributes act as a composite key since the primary key comprises more than one attribute.

Artificial key

The key created using arbitrarily assigned data are known as artificial keys. These keys are created when a primary key is large and complex and has no relationship with many other relations. The data values of the artificial keys are usually numbered in a serial order.


Top Trending Questions


Recent Question Update

Explain Domain Key Normal Form DKNF with example.
Explain Project Join Normal Form PJNF with example.
Explain 4nf and 5nf with example.
What is 5nf in DBMS.
What do you mean by 4NF in DBMS. Explain with example.
What is BCNF Boyce Codd Normal Form. Explain it.
What is normalization? Explain 3NF with example.
What is 2NF? How is it achieved.
What is 1NF? How do we achieve it.
What are the different normal forms in DBMS?
Brief Introduction to the Normalization.
Explain multivalued dependencies
What do you mean by lossless join? How can we test it.
What is the meaning of decomposition in DBMS? List its properties.
Explain the closure of attribute sets.
What do you mean by closure of a set of functional dependencies.
Explain the inference rules for functional dependencies in DBMS
What is the Difference between BCNF and 4NF in DBMS.
SQL Full Outer Join Using Left and Right Outer Join and Union Clause
Difference between Natural join and Inner Join in SQL
Define aggregate function in database.
Write the operations which are responsible for database modification.
Explain natural join operation.
Differentiate union, intersection and set difference operations.
Explain fundamental operations of relational algebra with example.
What do you mean by database scheme. Explain with example.
What do you mean by relational algebra?
Explain Aggregation
What do you mean by Generalization and Specialization in DBMS?
Explain superclass and subclass entity types
Describe EER Model?
What is ER Entity relationship Diagram.
Explain the concept of weak entity and strong entity.
What are the entities and attributes? Explain them.
Define relationship sets.
What do you mean by attributes? Explain its types.
What are the limitations of a database system.
Explain database administrator and its functions
Explain different database system users
What is database abstraction. Explain its levels.
Write advantages and disadvantages of Hierarchical Data Model
Write advantages and disadvantages of Network Data Model
Write advantages and disadvantages of Relational Data Model
What are data models? Explain its types.
Write different applications of a database
What are the advantages and disadvantages of a database system
Explain Database. Explain different types of elements.

Advantages Of NCERT, CBSE & State Boards Solutions For All Subjects

  • All the NCERT Solutions have been prepared by academic experts having 10+ years of teaching experience.
  • They have prepared all the solutions in simple and easy language so that each and every student can understand the concepts easily.
  • All the solutions have been explained step to step-wise in details with better explanations.
  • Students can also use these question and answers for your assignments and in homework help.
  • All the solutions have been explained in detail and the answers have been compiled in a step-wise manner.
  • All the questions and answers are commonly prepared according to the Latest Syllabus of Board Education and Guidelines.
  • Students can know about the various types of questions asked in the exams with the help of these solutions.

Top Course Categories