0% found this document useful (0 votes)
50 views4 pages

Primary Key, Foreign Key and Unique Key

The document discusses primary keys, foreign keys, and unique keys in databases. It defines primary keys as columns that uniquely identify each row, and lists conditions they must meet. Foreign keys are fields in one table that refer to a primary key in another table, to link the tables. Unique keys are like primary keys but allow one NULL value. The document also covers composite primary keys, data integrity, database normalization, and constraints.

Uploaded by

san
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Primary Key, Foreign Key and Unique Key

The document discusses primary keys, foreign keys, and unique keys in databases. It defines primary keys as columns that uniquely identify each row, and lists conditions they must meet. Foreign keys are fields in one table that refer to a primary key in another table, to link the tables. Unique keys are like primary keys but allow one NULL value. The document also covers composite primary keys, data integrity, database normalization, and constraints.

Uploaded by

san
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Primary Key, Foreign Key and Unique Key

18 Votes
Last Updated: Saturday, 12 August 2017 Hits: 16087

To understand the ordering of data inside a table, one had to be thorough about
database keys.This article will provide an insight into Primary Key, Foreign Key and
Unique Key.

SQL Primary Key


A primary key is a column whose values uniquely identify every row in a table. To define
a field as primary key, following conditions had to be met :

No two rows can have the same primary key value.


Every row must have a primary key value
Primary key field can't be null
Values in primary key columns can never be modified or updated.

Primary key values can never be reused. If a row is deleted from the table, its primary
key may not be assigned to any new rows in the future. In the examples given below,
Employee_ID field is the primary key.

SQL Composite Primary Key


A Composite primary key is a set of columns whose values uniquely identify every row
in a table. For example, in the table given above , if "Employee_ID" and "Employee
Name" together uniquely identifies a row its called a Composite Primary Key . In this
case , both the columns will be represented as primary key.

Read SQL Interview Questions for frequently asked questions on SQL.

SQL Foreign Key


When, "one" table's primary key field is added to a related "many" table in order to
create the common field which relates the two tables, it is called a foreign key in the
"many" table. In the example given below, salary of an employee is stored in salary
table. Relation is established via foreign key column Employee_ID_Ref which refers
Employee_ID field in Employee table.

For example, salary of "Jhon" is stored in "Salary" table. But his employee info is stored
in "Employee" table. To identify the salary of "Jhon", his "employee id" is stored with
each salary record.

The advantage of using foreign key is that, the data is not getting duplicated. If foreign
key concept was not there in RDBMS, entire info of an employee, such as First Name,
Last Name, Id etc. had to be stored with each and every salary entry. Another
advantage of foreign key is that the editing master entry such as designation, address,
etc. wont have any impact on the child table.

Read Advanced SQL Query Interview Questions and Answers on SQL Server, MySQL
and Oracle.

SQL Unique Key


Unique key is same as primary with difference being the existence of null. Unique key
field allows one value as NULL value. It wont allow duplicate entries.

Concept of Primary Key, Foreign Key and Unique Key is same in all the databases.
What is a NULL value?
A NULL value in a table is a value in a field that appears to be blank, which
means a field with a NULL value is a field with no value.

It is very important to understand that a NULL value is different than a zero


value or a field that contains spaces. A field with a NULL value is the one
that has been left blank during a record creation.

SQL Constraints
Constraints are the rules enforced on data columns on a table. These are
used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the database.

Constraints can either be column level or table level. Column level


constraints are applied only to one column whereas, table level constraints
are applied to the entire table.

Following are some of the most commonly used constraints available in SQL

NOT NULL Constraint Ensures that a column cannot have a NULL value.

DEFAULT Constraint Provides a default value for a column when none is


specified.

UNIQUE Constraint Ensures that all the values in a column are different.

PRIMARY Key Uniquely identifies each row/record in a database table.

FOREIGN Key Uniquely identifies a row/record in any another database table.

CHECK Constraint The CHECK constraint ensures that all values in a column
satisfy certain conditions.

INDEX Used to create and retrieve data from the database very quickly.

Data Integrity
The following categories of data integrity exist with each RDBMS
Entity Integrity There are no duplicate rows in a table.

Domain Integrity Enforces valid entries for a given column by restricting the
type, the format, or the range of values.

Referential integrity Rows cannot be deleted, which are used by other


records.

User-Defined Integrity Enforces some specific business rules that do not


fall into entity, domain or referential integrity.

Database Normalization
Database normalization is the process of efficiently organizing data in a
database. There are two reasons of this normalization process

Eliminating redundant data, for example, storing the same data in more than
one table.

Ensuring data dependencies make sense.

Both these reasons are worthy goals as they reduce the amount of space a
database consumes and ensures that data is logically stored. Normalization
consists of a series of guidelines that help guide you in creating a good
database structure.

Normalization guidelines are divided into normal forms; think of a form as


the format or the way a database structure is laid out. The aim of normal
forms is to organize the database structure, so that it complies with the
rules of first normal form, then second normal form and finally the third
normal form.

It is your choice to take it further and go to the fourth normal form, fifth
normal form and so on, but in general, the third normal form is more than
enough.

First Normal Form (1NF)

Second Normal Form (2NF)

Third Normal Form (3NF)

You might also like