Primary Key, Foreign Key and Unique Key
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.
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.
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.
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.
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.
Following are some of the most commonly used constraints available in SQL
NOT NULL Constraint Ensures that a column cannot have a NULL value.
UNIQUE Constraint Ensures that all the values in a column are different.
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.
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.
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.
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.