×

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

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 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.

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 IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

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...

3 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 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.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.