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

    • 0

Most Popular Job Functions


MOST LIKED QUESTIONS