Composite Key
A composite key is a candidate or primary or super key that compose of two or more columns which together uniquely identify an entity occurrence.
author_id | post_id | category_id |
101 | 45 | 3 |
200 | 67 | 5 |
289 | 89 | 7 |
101 | 45 | 3 |
As we can’t just rely on any of these individual author_id, post_id or category_id to become primary key. Because:
- There can be multiple author_id for the post_id. As same author may post multiple articles or posts.
- A post may have multiple categories.
So to uniquely identify the entity or row it is recommended to add composite key on the table.
In the given table composite key will be author_id and post_id.
Syntax:
CREATE TABLE SAMPLE_TABLE (COL1 integer, COL2 integer,COL3 integer, PRIMARY KEY (COL1, COL2));