What is a Primary Key, Foreign Key, and Composite Key?
Loading
What is a Primary Key, Foreign Key, and Composite Key?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Puneet KankarPosted Feb 9, 2025, 6:49 PM
Primary Key
A primary key uniquely identifies each record in a database table. Any individual key that does this can be called a candidate key, but only one can be chosen by database engineers as a primary key.
Composite Key
The composite key, which is composed of two or more attributes that collectively uniquely identify each record.
An example would be a list of homes on a real estate market. In a well-ordered database, there should be a primary key that uniquely identifies each record.
Foreign Key
if there is a key in a linked table, such as a buyer’s table that references the primary key, that will be a foreign key.
While a primary key and a composite key might do the same things, the primary key will consist of one column, where the composite key will consist of two or more columns.
The relationship between a primary key and a foreign key is quite different. The key thing to understand here is that the primary key in one database table becomes a foreign key in another database table, and vice versa.
A foreign key in a database table is taken from some other table and applied in order to link database records back to that foreign table.
Tuhin PaulPosted Feb 9, 2025, 6:43 PM
1. Primary Key
A Primary Key is a column (or a set of columns) in a database table that uniquely identifies each row in the table. It ensures that no two rows have the same value in the primary key column(s), and it cannot contain
NULLvalues.Key Characteristics:
NULLvalues.Here,
EmployeeIDis the primary key, ensuring that each employee has a unique identifier.2. Foreign Key
A Foreign Key is a column (or a set of columns) in one table that refers to the Primary Key of another table. It establishes a relationship between two tables, enforcing referential integrity.
Key Characteristics:
NULLvalues unless explicitly restricted.Here,
DepartmentIDin theEmployeestable is a foreign key that references theDepartmentIDprimary key in theDepartmentstable.3. Composite Key
A Composite Key is a combination of two or more columns in a table that together uniquely identify each row. It is used when no single column can uniquely identify a record.
Key Characteristics:
Here, the combination of
StudentIDandCourseIDuniquely identifies each enrollment. NeitherStudentIDnorCourseIDalone is sufficient to ensure uniqueness.Jaish MathewsPosted Feb 9, 2025, 3:47 PM
Here's a tabular comparison of Primary Key, Foreign Key, and Composite Key:
EmployeeIDin anEmployeestable.DepartmentIDin anEmployeestable referencingDepartmentIDin aDepartmentstable.OrderID,ProductID) in anOrderDetailstable.