Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Feb 9, 2025, 10:47 AM EST
Here's a tabular comparison of Primary Key, Foreign Key, and Composite Key:
EmployeeIDin anEmployeestable.DepartmentIDin anEmployeestable referencingDepartmentIDin aDepartmentstable.OrderID,ProductID) in anOrderDetailstable.Tuhin PaulRank #7 · 71.9K reputationPosted Feb 9, 2025, 1:43 PM EST
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.CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, -- Single-column primary key FirstName VARCHAR(50), LastName VARCHAR(50) );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.CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(100) ); CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), DepartmentID INT, FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) );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:
CREATE TABLE CourseEnrollments ( StudentID INT, CourseID INT, EnrollmentDate DATE, PRIMARY KEY (StudentID, CourseID) -- Composite key );Here, the combination of
StudentIDandCourseIDuniquely identifies each enrollment. NeitherStudentIDnorCourseIDalone is sufficient to ensure uniqueness.Puneet KankarRank #446 · 5.6K reputationPosted Feb 9, 2025, 1:49 PM EST
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.