0% found this document useful (0 votes)
45 views3 pages

What Is Primary Key

A primary key is a unique identifier for each row in a database table, ensuring no duplicate or NULL values. In contrast, a foreign key establishes a relationship between two tables, allowing multiple foreign keys and accepting NULL values. The primary key uniquely identifies records, while the foreign key references the primary key of another table to maintain data integrity.

Uploaded by

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

What Is Primary Key

A primary key is a unique identifier for each row in a database table, ensuring no duplicate or NULL values. In contrast, a foreign key establishes a relationship between two tables, allowing multiple foreign keys and accepting NULL values. The primary key uniquely identifies records, while the foreign key references the primary key of another table to maintain data integrity.

Uploaded by

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

What Is Primary Key?

A primary key generally focuses on the uniqueness of the table. It is a column


or a set of columns that uniquely distinguishes every row in the database. It
means it should not have any duplicate value. Also, it doesn’t contain a NULL
value.

What Is Foreign Key?


A foreign key is generally used to build a relationship between the two tables.
The major purpose of the foreign key is to sustain data integrity between two
separate instances of an entity.

Difference Between Primary Key and Foreign Key


S.No. Primary Key Foreign Key

1 A primary key generally focuses on the A foreign key is generally


uniqueness of the table. It assures the used to build a relationship
value in the specific column is unique. between the two tables.

2 The table allows only one primary key. Tables can allow more than
one foreign key.

3 The primary key doesn’t allow null The foreign key accepts
values. multiple null values.

4 It can identify the record uniquely in the A foreign key is a field in the
database table. table that is the primary key
in another table.

5 In the primary key, the value cannot be In this, the value can be
removed from the parent table. deleted from the child table.
6 Its restriction can be completely defined Its restriction cannot be
on the temporary tables. defined on the global or local
temporary tables.

What is a Primary Key in SQL?


A Primary key is a unique column we set in a table to easily identify and locate
data in queries. A table can have only one primary key.

The primary key column has a unique value and doesn’t store repeating
values. A Primary key can never take NULL values.

For example, in the case of a student when identification needs to be done in


the class, the roll number of the student plays the role of Primary key.
Similarly, when we talk about employees in a company, the employee ID is
functioning as the Primary key for identification.

Let us now understand the Syntax of creating the table with the Primary key
specified.

Syntax:

What is a Foreign key in SQL?


A Foreign key is beneficial when we connect two or more tables so that data
from both can be put to use parallelly.

A foreign key is a field or collection of fields in a table that refers to the Primary
key of the other table. It is responsible for managing the relationship between
the tables.
The table which contains the foreign key is often called the child table, and the
table whose primary key is being referred by the foreign key is called the Parent
Table.

For example: When we talk about students and the courses they have enrolled
in, now if we try to store all the data in a single table, the problem of redundancy
arises.
To solve this table, we make two tables, one the student detail table and the
other department table. In the student table, we store the details of students
and the courses they have enrolled in.

And in the department table, we store all the details of the department. Here
the courseId acts as the Primary key for the department table whereas it acts as
the Foreign key in the student table.

Let us now look at the syntax of creating a table with a foreign key.

You might also like