Primary Key vs. Foreign Key vs. Unique Key

I know this is a very basic concept and I predominantly understand the purpose of a Primary Key and a Foreign Key. But as I've been learning the concept of a Unique Key came up and I was unable to find a resource that gives me a clear understanding of the purpose of a Unique Key. I would love it if someone co explain the difference while comparing it to Primary and Foreign Keys potentially with some examples if applicable.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Primary Key :

    • The primary key uniquely identifies each record in the table.
    • The primary key cannot have a NULL value. If the column(s) contain NULL values, the system will not add the primary key constraint.
    • The primary key cannot have duplicate values.
    • A table can have only one Primary Key.

    Example : 

    In the below STUDENT table "Roll_Number" is the Primary Key of the table. This Roll_Number can be used to uniquely identify the rows of a STUDENT table.

    Roll_Number Name Class Age Phone_Number Reg_Number
    101 Mark 7 14 1023456789 301
    102 Flex 7 14 1123456789 302
    103 Scott 7 13   303

    Unique Key : 

    • Unique Key is a key which has a unique value and is used to prevent duplicate values in a column.
    • Primary key also has a unique value for each column.
    • A unique key can have a NULL value which is not allowed in a primary key .
    • If we have a value present then either it would be unique or no value would be present at all.
    • Main motive of a unique key in a table is to prevent duplicate values.
    • A table can have multiple Unique Key.

    Example : In the above table, we have Phone_Number as the Unique Key and Roll_Number as the Primary Key. If any student has a Phone_Number then it should be unique. Also, there might be some student who doesn't have any Phone_Number. In the table the Student Scott do not have a Phone Number. 

    Note : 

    • Primary Key is used to uniquely identify a row but a Unique Key is used to prevent duplicate values in a column.
    • A Table can have only one primary key but it can have multiple unique keys. In the above Student Table, we have only one Primary Key i.e Roll_Number but we have two Unique Key i.e Phone_Number and Reg_Number. 

    Foreign Key : 

    • A foreign key establishes a relationship between tables by referencing the primary key of another table. 
    • A table may have multiple foreign keys
    • Foreign keys establish relationships between tables, ensuring that data matches up correctly and maintains order.
Reply
  • 0
    Certified Lead Developer

    Primary Key :

    • The primary key uniquely identifies each record in the table.
    • The primary key cannot have a NULL value. If the column(s) contain NULL values, the system will not add the primary key constraint.
    • The primary key cannot have duplicate values.
    • A table can have only one Primary Key.

    Example : 

    In the below STUDENT table "Roll_Number" is the Primary Key of the table. This Roll_Number can be used to uniquely identify the rows of a STUDENT table.

    Roll_Number Name Class Age Phone_Number Reg_Number
    101 Mark 7 14 1023456789 301
    102 Flex 7 14 1123456789 302
    103 Scott 7 13   303

    Unique Key : 

    • Unique Key is a key which has a unique value and is used to prevent duplicate values in a column.
    • Primary key also has a unique value for each column.
    • A unique key can have a NULL value which is not allowed in a primary key .
    • If we have a value present then either it would be unique or no value would be present at all.
    • Main motive of a unique key in a table is to prevent duplicate values.
    • A table can have multiple Unique Key.

    Example : In the above table, we have Phone_Number as the Unique Key and Roll_Number as the Primary Key. If any student has a Phone_Number then it should be unique. Also, there might be some student who doesn't have any Phone_Number. In the table the Student Scott do not have a Phone Number. 

    Note : 

    • Primary Key is used to uniquely identify a row but a Unique Key is used to prevent duplicate values in a column.
    • A Table can have only one primary key but it can have multiple unique keys. In the above Student Table, we have only one Primary Key i.e Roll_Number but we have two Unique Key i.e Phone_Number and Reg_Number. 

    Foreign Key : 

    • A foreign key establishes a relationship between tables by referencing the primary key of another table. 
    • A table may have multiple foreign keys
    • Foreign keys establish relationships between tables, ensuring that data matches up correctly and maintains order.
Children
No Data