---
title: "SQL Keys"  
description: "A key is a single or combination of multiple fields in a table. It is used to fetch or retrieve records/data-rows from data table according to the con"  
author: "AVADHESH PATEL"  
published: 2012-09-28  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/371/sql-keys  
category: "database"  
tags: ["database"]  
reading_time: 2 minutes  

---

# SQL Keys

A **key** is a single or combination of [multiple fields](https://www.mindstick.com/forum/160754/how-orderby-multiple-fields-in-angular) in a table. It is used to fetch or retrieve records/data-rows from [data table](https://www.mindstick.com/forum/12752/how-to-select-specific-columns-from-a-data-table-in-vb-dot-net) according to the condition/requirement. Keys are also used to create relationship among different database tables or views. [SQL keys](https://www.mindstick.com/articles/553/important-sql-keys) describe below.

Super Key: - Supper key is a set of one or more than one keys that can be used to identify a record uniquely in a table.

Example: -[Primary key](https://www.mindstick.com/blog/214/primary-key), [Unique key](https://www.mindstick.com/interview/768/what-s-the-dot-net-datatype-that-allows-the-retrieval-of-data-by-a-unique-key), Alternate key is subset of Super Keys.

Primary Key: - The attribute or combination of attributes that uniquely identifies a row or record.

##### Syntax:-

```
-- Syntax of Primary KeyCREATE TABLE <TABLE_NMAE>(<COLUMN_NAME> <DATATYPE> PRIMARY KEY)
```

[Foreign Key](https://www.mindstick.com/blog/174/foreign-key-self-reference-constraint): - Foreign Key is an attribute in [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table) that is Primary key in another table. It can accept multiple null, [duplicate values](https://www.mindstick.com/forum/157764/how-to-find-duplicate-values-in-a-sql-table).

##### Syntax:-

```
--Syntax of Foreign KeyCREATE TABLE <TABLE_NAME>(<COLUMN_NAME> <DATATAYPE> FOREIGN KEY REFERENCES <TABLE_NAME>(<COLUMN_NAME>))
```

\

Unique Key: - Unique key is a set of one or more attribute of a table that uniquely identify a record in database table. It is like Primary key but it can accept only one [null value](https://www.mindstick.com/forum/159364/difference-between-two-expression-results-with-a-null-value) and it cannot have duplicate values.

##### Syntax:-

```
-- Syntax of UniqueCREATE TABLE <TABLE_NAME>(<COLUMN_NAME> <DATATYPE> UNIQUE)
```

Composite Key: - Consists of two or more attributes is [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) composite key. It can be a Candidate key or Primary key.\
\
Candidate Key: - A column in a table which has the ability to become a primary key is called candidate key. \
\
Alternate Key: - Any of the candidate keys that are not part of the primary key is called an alternate key. \
\
Secondary Key: - Alternate of primary key.

##### Example of Primary key, Foreign Key and Unique:-

```
-- CREATED FIRST TABLECREATE TABLE Department(ID INT PRIMARY KEY,Name VARCHAR (50) NOT NULL,Address VARCHAR (200) NOT NULL)  --CREATED SECOND TABLECREATE TABLE Student(ID INT IDENTITY(1,1) PRIMARY KEY, -- PRIMARY KEY WITH IDENTITY PROPERTIESRollNo VARCHAR(10) NOT NULL,Name VARCHAR(50) NOT NULL,EnrollNo VARCHAR(50) UNIQUE, -- UNIQUE KEY Address VARCHAR(200) NOT NULL,DeptID INT FOREIGN KEY REFERENCES Department(ID) -- FOREIGN KEY )
```

---

Original Source: https://www.mindstick.com/blog/371/sql-keys

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
