Primary keys and foreign keys are essential for maintaining data integrity and relationships between tables in a relational database.
CREATE TABLE Students (
student_id INT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES Students(student_id)
);
| Feature | Primary Key | Foreign Key |
|---|---|---|
| Purpose | Unique identity | Relationship between tables |
| NULL Allowed? | ❌ No | ✅ Yes |
| Uniqueness | Must be unique | Can repeat |
| Count per Table | Only 1 | Many allowed |