Difference between Primary Key and Foreign Key in SQL

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ค. 2024
  • A primary key is a column or set of columns in a table that uniquely identifies each row in the table. It is used to enforce the uniqueness of rows and to establish a relationship with other tables.
    A foreign key is a column or set of columns in a table that references the primary key of another table. It is used to establish a relationship between two tables, such that the data in the foreign key column in the child table must exist in the primary key column of the parent table.
    For example, consider the following Customer and Orders tables: In this example, the CustomerID column in the Orders table is a foreign key that references the CustomerID column in the Customers table. The CustomerID column in the Customers table is the primary key of the Customers table.
    The primary key and foreign key have a one-to-many relationship: each row in the Customers table can be associated with multiple rows in the Orders table, but each row in the Orders table can only be associated with one row in the Customers table.

ความคิดเห็น • 4