Database / SQLite Interview questions
What is a composite primary key in SQLite?
A composite (or compound) primary key spans more than one column, with uniqueness enforced across the combination of those columns rather than any single one alone — useful for tables representing a many-to-many relationship, where the natural unique identifier really is a pair (or more) of values together.
CREATE TABLE enrollment ( student_id INTEGER, course_id INTEGER, PRIMARY KEY (student_id, course_id) ); -- the same student_id can appear multiple times (for different courses), -- and the same course_id can appear multiple times (for different students), -- but the (student_id, course_id) pair as a whole must be unique
Unlike a single-column INTEGER PRIMARY KEY, a composite primary key doesn't get the rowid-alias
performance shortcut — lookups against it go through a normal B-tree index traversal on the combined key,
still efficient, but not the absolute fastest single-integer lookup path.
More Related questions...