---
title: "Keys in SQL"  
description: "In this blog, I’m explaining the keys in sql and how to create it.Primary KeyA Primary Key is a column which has unique values in the column. A primar"  
author: "Sumit Kesarwani"  
published: 2013-05-18  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/512/keys-in-sql  
category: "database"  
tags: ["database"]  
reading_time: 3 minutes  

---

# Keys in SQL

In this blog, I’m explaining the [keys in sql](https://www.mindstick.com/articles/1433/keys-in-sql) and how to create it.

##### Primary Key

A Primary Key is a column which has [unique values](https://answers.mindstick.com/qa/93939/how-to-define-hashset-t-collection-in-c-sharp-which-collection-is-used-to-store-unique-values) in the column. A primary key uniquely identifies the each record in the table. A Primary key column cannot have null values. A primary key can be made up of [single column](https://www.mindstick.com/forum/159327/how-to-set-all-values-in-a-single-column-mysql-query) or [multiple columns](https://www.mindstick.com/forum/157767/how-to-apply-group-on-multiple-columns-in-sql).

##### Syntax:

[CREATE TABLE](https://www.mindstick.com/articles/443/how-to-create-table-in-sql-server) tablename(column name datatype(size) primary key,column name datatype(size))

CREATE TABLE tablename(column name datatype(size) ,column name datatype(size),primary key(column name))

##### Example

```
CREATE TABLE student(student_id int primary key,student_name varchar(30),student_address varchar(30),student_mobile int,student_branch varchar(10),student_date_of_birth date,);CREATE TABLE student(student_id int,student_name varchar(30),student_address varchar(30),student_mobile int,student_branch varchar(10),student_date_of_birth date,primary key(student_id));
```

##### Composite Key

When we have a Primary Key of a table defined using more than one columns then it is known as a Composite Key, each columns data can be duplicated, but combined values cannot be. The columns which are participating in a composite primary key are not simple keys.

##### Syntax:

CREATE TABLE tablename(column name datatype(size) ,column name datatype(size),column name datatype(size),primary key(column name, column name))

##### Example

```
CREATE TABLE book(name varchar(30),author varchar(30),dept varchar(30),primary key(name,author));
```

##### Foreign Key

Foreign Key is a field 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). A column of one table points to the Primary Key column of another table to implement referential [data integrity](https://www.mindstick.com/forum/160197/what-is-the-normalization-in-sql-server-explain-its-impact-on-data-integrity-and-performance). [Foreign keys](https://www.mindstick.com/forum/159031/how-can-i-list-all-foreign-keys-referencing-a-table-in-sql-server) define a relationship between two tables. A foreign key identifies a column or group of columns in one (referencing) table that refers to a column or group of columns in another (referenced) table.

##### Syntax:

CREATE TABLE tablename(column name datatype(size), column name datatype(size),column name datatype(size), column name datatype references tablename);

##### Example

```
CREATE TABLE book(name varchar(30),author varchar(30),dept varchar(30),student_id int references student);
```

##### Candidate Key

A Candidate Key is a set of one or more fields/columns that can identify a record uniquely in a table. There can be multiple Candidate Keys in one table. Each Candidate Key can work as Primary Key. A Column (or) Combination of columns which can help uniquely identify a record in a table without the need of any external data is called a Candidate Key. Depending on the need and situation a Table may have one or more candidate keys and [one of them](https://answers.mindstick.com/qa/47593/what-are-the-prevalent-water-proofing-systems-write-a-short-notes-on-any-one-of-them) can be used as a Primary Key of the table.

##### Example

```
CREATE TABLE student(student_id int primary key,student_roll_number int,student_enrollment_number int,student_name varchar(30),student_address varchar(30),student_mobile int,student_branch varchar(10),student_date_of_birth date,);
```

In this query, student_roll_number and student_enrollment_number can be used as candidate key.

---

Original Source: https://www.mindstick.com/blog/512/keys-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
