🔑 What is a Primary Key?
A Primary Key (PK) uniquely identifies each row in a table. It must be unique and NOT NULL. Databases typically index PKs for both integrity and performance.
Table of Contents
ToggleClear definitions, side-by-side comparison, practical SQL examples, and answers to common questions like can a primary key be a foreign key or can a foreign key be NULL. Hybrid English + Hindi guide — 2025 update.
Primary Keys (PK) uniquely identify rows; Foreign Keys (FK) link tables and enforce referential integrity. This section answers high-volume queries such as primary key and foreign key, difference between primary key and foreign key, and primary key vs foreign key in SQL.
A Primary Key (PK) uniquely identifies each row in a table. It must be unique and NOT NULL. Databases typically index PKs for both integrity and performance.
A Foreign Key (FK) is a column in one table that references the Primary Key (or a UNIQUE key) of another table to enforce relationships and referential integrity.
Side-by-side comparison for quick answers to search queries.
Aspect | Primary Key | Foreign Key |
---|---|---|
Uniqueness | Unique & NOT NULL | Nullable & may repeat |
Purpose | Identify rows | Link tables |
Count per table | One (composite allowed) | Multiple allowed |
Enforces | Entity integrity | Referential integrity |
Yes — in some designs (1:1 relationships or identifying/weak-entity models) a primary key can also serve as a foreign key.
NULL: Yes, unless NOT NULL is specified.
Duplicate: Yes — multiple child rows can reference the same parent row (answers queries like “is foreign key unique?”).
Primary Key = unique identifier (NOT NULL). Foreign Key = reference to parent (nullable/duplicates allowed). Design PK/FK carefully for integrity and joins.
© Vista Academy • Content updated 2025 • Built with Vista Theme — dark, golden & elegant UX for learners.
This comparison answers common queries like difference between primary key and foreign key,
is foreign key unique, and primary key vs foreign key in SQL. Use this as a quick reference
when designing database tables and ER diagrams.
Feature | Primary Key (PK) | Foreign Key (FK) |
---|---|---|
Definition | Uniquely identifies each row within the same table | References the PK (or UNIQUE) of another table to create relationships |
NULL Allowed? | No — PK must be NOT NULL | Yes — unless explicitly constrained as NOT NULL |
Duplicates? | No — values are unique | Allowed — many child rows can reference the same parent |
Count per table | One PK per table (composite keys possible) | Multiple FKs allowed (to different parent tables) |
Purpose | Row identity, indexing, fast lookups | Enforce referential integrity and support joins |
Example | Students.student_id |
Marks.student_id → Students.student_id |
This table targets queries like primary key vs foreign key, difference between primary and foreign key,
primary key foreign key example, and is foreign key unique. For practical SQL examples and ER-diagram tips, refer to the next section of the guide.
Many learners ask: can a primary key be a foreign key? The answer is Yes. In SQL database design, a primary key (PK) can also serve as a foreign key (FK), especially in 1-to-1 relationships or self-referencing hierarchies. Below are two practical examples with SQL code.
CREATE TABLE Employees ( emp_id INT PRIMARY KEY, name VARCHAR(100), manager_id INT, FOREIGN KEY (manager_id) REFERENCES Employees(emp_id) );
Here emp_id
is the table’s Primary Key.
The manager_id
column is a Foreign Key pointing back to the same PK, creating a hierarchical relationship.
CREATE TABLE Users ( user_id INT PRIMARY KEY, email VARCHAR(100) UNIQUE ); CREATE TABLE UserProfiles ( user_id INT PRIMARY KEY, -- also FK bio VARCHAR(200), FOREIGN KEY (user_id) REFERENCES Users(user_id) );
In this case UserProfiles.user_id
is both Primary Key and Foreign Key, ensuring a strict one-to-one mapping with Users
.
Keywords covered: primary key as foreign key, can primary key be foreign key, primary key foreign key example in SQL.
CREATE TABLE Customers ( customer_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL ); CREATE TABLE Orders ( order_id INT PRIMARY KEY, customer_id INT, order_date DATE NOT NULL, CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES Customers(customer_id) ON UPDATE CASCADE ON DELETE SET NULL );
Here, Orders.customer_id
is a Foreign Key referencing Customers.customer_id
.
ON DELETE SET NULL means the FK becomes NULL when the parent row is removed.
INSERT INTO Customers(customer_id, name) VALUES (1, 'Asha'), (2, 'Ravi'); INSERT INTO Orders(order_id, customer_id, order_date) VALUES (101, 1, '2025-08-01'), (102, 2, '2025-08-05');
Always insert parent rows (Customers
) before child rows (Orders
) to satisfy FK constraints.
SELECT o.order_id, c.name, o.order_date FROM Orders o JOIN Customers c ON o.customer_id = c.customer_id;
This SQL join retrieves order details along with customer names by using the Primary–Foreign Key relationship.
Keywords covered: primary key and foreign key in SQL with examples, sql primary key foreign key example, how to use primary key and foreign key in SQL, difference between primary and foreign key in SQL.
See how primary keys and foreign keys are used in production systems — from retail to healthcare. These practical examples clarify why primary key vs foreign key design matters for data integrity, reporting, and performance.
Use Customers.customer_id (PK) and Orders.customer_id (FK) to link purchases to buyers. FK cascade rules help keep order history consistent when customer records change.
Accounts use a PK (account_id) while Transactions reference it via an FK. Referential integrity ensures transactions cannot reference deleted accounts.
Model many-to-many relationships (Students ↔ Courses) with a join table using a composite PK (student_id, course_id) while each column references the parent PKs as FKs.
Patient records use a stable primary key (e.g., patient_id). Appointments, prescriptions and lab results reference this PK with FKs to maintain an auditable history.
In star schemas, dimension tables have PKs used as FKs in fact tables (e.g., dim_date.date_id → fact_sales.date_id). This makes aggregations and joins fast and consistent.
These use cases show why careful primary key and foreign key design is essential for data integrity, query performance, and maintainability. Apply appropriate cascade rules, indexing, and constraints based on business rules.
🎯 Learn Practical DB Design at Vista AcademyPrimary Keys (PK) and Foreign Keys (FK) are the foundation of relational databases. They ensure data integrity, prevent duplicates, and enable relationships between entities. Here are some common real-world scenarios:
Customers (PK) linked to Orders (FK). Ensures each order belongs to a valid customer and enables order history tracking.
Accounts (PK) connected to Transactions (FK). Guarantees that every transaction is tied to an existing account.
Students (PK) linked to Enrollments and Courses (FK). Maintains consistency in academic records and course registrations.
Shipments (PK) linked to Delivery Tracking (FK). Keeps data consistent across warehouses, routes, and delivery status updates.
Keywords: why do relational databases use primary keys and foreign keys, primary key foreign key examples, real-world use of primary and foreign key
No. A foreign key can repeat because multiple child rows often reference the same parent row (1-to-many). Use UNIQUE if you need uniqueness on the FK column.
हिंदी: Foreign Key आमतौर पर दोहराई जा सकती है — केवल तभी यूनिक होगा जब UNIQUE constraint लगाया गया हो।
Yes — unless the column is explicitly declared NOT NULL by schema or business rules. NULL FKs mean “no relationship” for that row.
हिंदी: हाँ, जब तक NOT NULL constraint न लगाया गया हो; NULL का मतलब है उस रो का कोई parent नहीं।
Typically FK references a candidate key — most often the Primary Key. Many DBMS also allow referencing a UNIQUE NOT NULL key instead of the PK.
हिंदी: आमतौर पर FK, PK को संदर्भित करता है; लेकिन यह किसी UNIQUE NOT NULL कॉलम को भी रेफर कर सकता है।
Yes — in certain designs (identifying relationships or strict 1:1 mappings) a PK in a child table may also be an FK to the parent table.
हिंदी: हाँ — जैसे identifying relationship में child की PK, parent की PK को रेफर कर सकती है।
Yes. FK constraints prevent child rows from referencing non-existent parent rows and allow DB engines to enforce ON DELETE / ON UPDATE behaviors.
हिंदी: हाँ — FK यह सुनिश्चित करता है कि child, मौजूद parent को ही रेफर करे और cascade नियम लागू होते हैं।
Yes. Duplicates are common for 1-to-many relationships. If you want uniqueness, add a UNIQUE constraint to the FK column (but that changes relationship cardinality).
हिंदी: हाँ — कई child रो एक ही parent का reference कर सकती हैं; UNIQUE लगाने से यह व्यवहार बदल जाता है।
Behavior depends on FK actions: CASCADE deletes child rows; SET NULL sets FK to NULL; RESTRICT/NO ACTION prevents the delete.
हिंदी: parent हटाने पर CASCADE child हटाएगा, SET NULL child के FK को NULL करेगा, अन्यथा delete block होगा।
Yes. You can define composite foreign keys that reference composite primary keys — both sides must match column order and types.
हिंदी: हाँ — multi-column FK, multi-column PK को रेफर कर सकती है; कॉलम क्रम और टाइप मेल खाने चाहिए।
The Primary Key. It’s the canonical identifier for rows in a relation.
हिंदी: किसी तालिका में हर रिकॉर्ड की अनन्य पहचान Primary Key होती है।
Answer all questions — your results and correct answers will appear when you finish.
Vista Academy’s Master Program in Data Analytics equips you with advanced skills in Data Analysis, Machine Learning, SQL, Python, Tableau, and Power BI. With hands-on projects and expert mentoring, this program prepares you for high-demand roles in Data Science and Analytics.
📍 Address: Vista Academy, 316/336, Park Rd, Laxman Chowk, Dehradun, Uttarakhand 248001
Now that you’ve mastered Primary & Foreign Keys, it’s time to practice advanced SQL concepts. Explore the following tutorials and apply them on real datasets to level up your skills:
🚀 Keep practicing on real-world datasets and refining your skills. The more hands-on experience you gain, the stronger your SQL foundation will become!