Sure. Here are the differences between primary key, foreign key, and composite key in SQL:
Primary key: A primary key is a unique identifier for each row in a table. It cannot have any duplicate values, and it cannot be NULL. A primary key can be a single column or a combination of columns.
Foreign key: A foreign key is a column or combination of columns in one table that references the primary key of another table. The foreign key must have the same values as the primary key in the other table. Foreign keys are used to establish relationships between tables.
Composite key: A composite key is a primary key that consists of two or more columns. Composite keys are used when no single column is sufficient to uniquely identify a row in a table.
Here is a table summarizing the differences between primary key, foreign key, and composite key:
Feature
Primary key
Foreign key
Composite key
Uniqueness
Yes
Yes
Yes
Nullability
No
No
Can be null
Number of columns
One or more
One
Two or more
Purpose
Uniquely identifies each row in a table
Establishes a relationship between tables
Uniquely identifies each row in a table
Here are some additional details about the differences between primary key, foreign key, and composite key:
A primary key can be used to uniquely identify a row in a table, even if other columns in the table have duplicate values. For example, a table of students might have a primary key of
student_id, even if there are multiple students with the same name.
A foreign key is used to establish a relationship between two tables. The foreign key in one table must reference the primary key in another table. For example, a table of orders might have a foreign key of
customer_id that references the primary key in a table of customers.
A composite key is a primary key that consists of two or more columns. Composite keys are used when no single column is sufficient to uniquely identify a row in a table. For example, a table of books might have a composite key of
ISBN and edition.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure. Here are the differences between primary key, foreign key, and composite key in SQL:
Here is a table summarizing the differences between primary key, foreign key, and composite key:
Here are some additional details about the differences between primary key, foreign key, and composite key:
student_id, even if there are multiple students with the same name.customer_idthat references the primary key in a table of customers.ISBNandedition.