×

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst various tables. There are several types of SQL KEYS, which are:

  1. Super key
  2. Candidate key
  3. Primary key
  4. Alternate key
  5. Composite key
  6. Unique key
  7. Foreign key

1. SUPER KEY

A SUPER KEY is a combination of one or multiple columns in a table in the database, which help in the unique identification of each row in the table. It is a group of one or several keys.

Example:

EmpSSNEmpNumEmpName
1254654TAE03Harry
2165465TAE04Ron
2154864TAE05Dobby

In the above example, EmpSSN and EmpNum are SUPER KEYS because they help in identifying each row in the table uniquely.

2. CANDIDATE KEY

CANDIDATE KEY is also a set of columns or attributes that help identify each row in the table separately. A CANDIDATE KEY can be defined as a SUPER KEY having no matching attributes. It can be demonstrated as a sub-set of SUPER KEY. Several candidate keys can be there in a table.

Example:

StuIDRollFirstNameLastNameEmail
0142HarryKaneharrykane@gmail.com
0243RonWiesleyronwiesley@yahooo.co.in
0344DobbyWrightdobbywright@hotmail.com

StuID, Roll and Email are CANDIDATE KEYS in the above table because they help to identify each row uniquely.

3. PRIMARY KEY

PRIMARY KEY is an attribute or a group of attributes that help in identifying individual rows distinctly. There cannot be the exact value of the PRIMARY KEY more than once in the table. A PRIMARY KEY can be expressed as a sub-set of a CANDIDATE KEY. There cannot be Multiple PRIMARY KEYS in a table.

Properties of a PRIMARY KEY:

  • There cannot be duplicate values of PRIMARY KEY in the table.
  • PRIMARY KEY cannot contain null values.
  • The value of a PRIMARY KEY should not be changed with time.
  • Each individual row in the table should contain a PRIMARY KEY.

Example:

StuIDRollFirstNameLastNameEmail
0142HarryKaneharrykane@gmail.com
0243RonWiesleyronwiesley@yahooo.co.in
0344DobbyWrightdobbywright@hotmail.com

StuID is the primary key in the above example since it can uniquely identify each record in the table.

4. ALTERNATE KEY

ALTERNATE KEY helps in identifying the records in the table distinctly. There can be several columns in a table that can identify individual rows in the table separately. Out of those attributes, only one attribute is chosen as the PRIMARY KEY. The rest of the attributes become ALTERNATE KEYS.

Example:

StuIDRollFirstNameLastNameEmail
0142HarryKaneharrykane@gmail.com
0243RonWiesleyronwiesley@yahooo.co.in
0344DobbyWrightdobbywright@hotmail.com

In the above example, Roll and Email are ALTERNATE KEYS.

The following representation will help understand CANDIDATE KEY, PRIMARY KEY, and ALTERNATE KEY in a better way.

5. COMPOSITE KEY

COMPOSITE KEY is a merger of multiple columns that help in identifying each row distinctly. This distinctness is guaranteed only when the columns are combined. When the columns are taken individually, it does not promise distinctiveness. A PRIMARY KEY which is made of multiple attributes, is defined as a COMPOSITE KEY.

Example:

OrderNoProductIDProductNameQuantity
A0015624185LCD1
A0013216546Printer2
A0013516527Mouse3
A0029816846Keypad1
A0037160354USB5

In the above example, OrderNo and ProductID combined to form the COMPOSITE KEY. They individually cannot identify each row in the table uniquely, but when they are combined, they can identify each record in the table uniquely.

6. UNIQUE KEY

UNIQUE KEY can also identify each row in a table uniquely like a PRIMARY KEY. But, unlike a PRIMARY KEY, a UNIQUE KEY can have only a single null value. There can be several UNIQUE KEYS in a table.

Example:

Let’s consider a Student table having the following columns.

In the above columns, CityID is the UNIQUE KEY. Suppose, if a student leaves the city and goes abroad for studies, then that student’s CityID will not be there. In that case, that attribute will become null and null values are allowed in UNIQUE KEY.

7. FOREIGN KEY

A FOREIGN KEY in a table is an attribute that establishes a relationship between two tables. A FOREIGN KEY of one table references the PRIMARY KEY of another table, establishing the relation between the two tables. A FOREIGN KEY can accept multiple null and duplicate values.

Example:

Let’s consider the following two tables, the Students table, and the Order tables.

The first table is the Students table.

StuIDFNameLNameCity
1HarryKaneKolkata
2RonWiesleyNoida
3DobbyWrightMumbai

The second table is the Order table.

OrderIDOrderNoStuID
1654985453
2465468542
3216546983
4651654151

The StuID in the Students table is the PRIMARY KEY, and the StuID in the Order table is the FOREIGN KEY.

These are the essential keys in SQL that should be given importance while creating or dealing with databases.


Related Topics

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.