There are no items in your cart
Add More
Add More
| Item Details | Price | ||
|---|---|---|---|
Understand how Primary Keys and Foreign Keys work in SQL databases, how they create relationships between tables, maintain data integrity, and help developers design reliable and scalable relational databases.
Relational databases store information in multiple tables. As the database grows, these tables need a reliable way to identify individual records and establish relationships between related data. Primary Keys and Foreign Keys are two of the most important concepts used for this purpose.
A Primary Key uniquely identifies a record inside its own table, while a Foreign Key connects one table with another by referencing a key from the related table. Together, they help maintain data integrity, consistency, and relationships within a relational database.
Uniquely identifies every row in a table. A table normally has one Primary Key definition, which may consist of one column or multiple columns.
Creates a relationship between tables by storing values that reference a candidate key, commonly the Primary Key, in another table.
Keys help databases prevent invalid relationships and maintain consistent data across related tables.
A Primary Key is a column or combination of columns that uniquely identifies each record in a relational database table. Because every row needs to be distinguishable, the values of the Primary Key must be unique.
A Primary Key cannot contain duplicate values and normally cannot contain NULL values because it must reliably identify a record.
CREATE TABLE Customers ( customer_id INT PRIMARY KEY, customer_name VARCHAR(100), email VARCHAR(150));
Here, customer_id uniquely identifies every customer. Two customers cannot have the same customer_id.
A Foreign Key is a column or set of columns in one table that references a candidate key, commonly the Primary Key, in another table. It establishes a relationship between records stored in different tables.
For example, an Orders table may contain a customer_id column. That value can reference customer_id in the Customers table. This tells the database which customer owns a particular order.
Primary Key = Who is this record?
Foreign Key = Which related record does this belong to?
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
order_date DATE,
customer_id INT,
FOREIGN KEY (customer_id)
REFERENCES Customers(customer_id)
);
The customer_id column in Orders references customer_id in Customers. This creates a relationship between the two tables.
Customers.customer_id is the referenced key, while Orders.customer_id stores the relationship.
| Feature | Primary Key | Foreign Key |
|---|---|---|
| Purpose | Uniquely identifies a row | Creates a relationship between tables |
| Duplicates | Not allowed | Can be repeated |
| NULL | Not allowed | Can be allowed depending on design |
| Reference | Referenced by other tables | References another table |
| Number per table | One Primary Key constraint | Can have multiple Foreign Keys |
| Example | customer_id | Orders.customer_id |
Imagine an online shopping application. The system has thousands of customers and millions of orders. Instead of storing the customer's name and email repeatedly in every order record, the database can store customer information once and use a key to connect orders to that customer.
customer_id = 101
customer_name = Rahul
email = rahul@example.com
order_id = 5001
order_date = 2026-08-19
customer_id = 101
Because both records contain the value 101, the database knows that Order 5001 belongs to Customer 101.
INSERT INTO Customers
(customer_id, customer_name, email)
VALUES
(101, 'Rahul', 'rahul@example.com');
INSERT INTO Orders
(order_id, order_date, customer_id)
VALUES
(5001, '2026-08-19', 101);
Once tables are connected through keys, SQL JOIN operations can combine related information from multiple tables.
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.order_date
FROM Customers c
INNER JOIN Orders o
ON c.customer_id = o.customer_id;
The JOIN uses the relationship between Customers.customer_id and Orders.customer_id to retrieve combined information.
Trying to insert the same Primary Key value into two rows violates the uniqueness requirement.
Inserting a Foreign Key value that does not satisfy the referenced key constraint can violate referential integrity.
Related key columns should use compatible data types and compatible definitions.
Design relationships before creating tables so that the database structure remains maintainable.
Primary Keys and Foreign Keys are fundamental building blocks of relational database design. A Primary Key identifies a record, while a Foreign Key establishes a relationship with another table. Understanding the difference between them is essential for anyone working with SQL, backend development, data analytics, data science, or data engineering.
Uniquely identifies a row.
Connects related tables.
They support relational data integrity.