Under what condition we need to use composite keys in database

前端 未结 4 1675
慢半拍i
慢半拍i 2021-02-09 19:17

i have seen that we can have composite keys where the primary key is made up from combined primary keys of two tables.

Like persons and Books

person_id a         


        
4条回答
  •  庸人自扰
    2021-02-09 20:03

    If you are storing a relation between person and books that is one-to-one (for example, perhaps you are running a website where users can rate books they've read on a scale of 1-5), then a composite primary key on the votes table of person_id and book_id makes just as much sense, if not more, as having a generated ID and a unique index on the combination of (person_id, book_id). The combination of the person and the book defines the vote record.

提交回复
热议问题