Vikas
Difference between primary key and Foreign Key
By Vikas in .NET on Mar 23 2021
  • Kirtesh Shah
    Apr, 2021 6

    Primary Key: -

    1. The Primary key is used to identify rows or data uniquely from the table hence table cannot have duplicate data.
    2. A table can have only one primary key.
    3. Automatically apply Cluster index to the column which has a primary key.
    4. It is not accepting null values.

    Foreign Key :

    1. A table Primary key becomes a Foreign key in another table. It helps to maintain the relationship between two or more tables.
    2. A table can have multiple Foreign keys.
    3. Not apply cluster index.
    4. Accept null value

    eg.
    For example, We have two tables student and Course.

    Student table has STUID which is primary key of Student Table and Foreign key in COURSE table.

    STUDENT TABLE

    STUID (PRIMARY KEY) Name Address Phone No
    100 Kirtesh Shah Vadodara 123456789
    101 Nitya Shah Surat 123456789

    COURSE TABLE

    COURSEID (PRIMARY KEY) STUID (FORENIGN KEY) COURSE NAME
    C1 100 MCA
    C2 101 BSC

    • 1
  • Salman Beg
    Apr, 2021 3

    A primary key is used to ensure data in the specific column is unique. It is a column cannot have NULL values.
    Where as a foreign key is a column or group of columns in a relational database table that provides a link between data in two tables.
    See this below link for full difference,

    https://www.guru99.com/difference-between-primary-key-and-foreign-key.html

    • 1
  • radhika mohan
    Feb, 2022 18

    I can think of below three points:

    1. Primary key is used to uniquely identify a row.Foreign key is used to connect two tables or maintain relationship between them
    2. Primary key can not have null values. Foreign key can have multiple null values
    3. Primary key can not be deleted from table unless all the references are deleted from other tables . Foreign key can be deleted

    Reference: Scaler Topics

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS