Two column as a primary keys in mysql?

前端 未结 8 1719
孤独总比滥情好
孤独总比滥情好 2020-12-15 06:34

Today I found out you can have a primary key using two columns (tsql). The PK must be unique but both columns do not (the combo must be unique).

I thought that was v

8条回答
  •  清歌不尽
    2020-12-15 06:53

    Your thinking is good. I use multi-field primary keys frequently, simply because it makes my database design more logical, managable and readable. You can think of multi-field primary keys like having a unique name. For example:

    Multi-Field Primary Keys:

    (First ,Middle, Last)
    

    Example Values:

    ('Michael', 'A.', 'Kline')
    

    There can be many people with the 'First' name 'Michael' and/or the 'Middle' name 'A.' and/or the'Last' name 'Kline', but as far as your database is concerned, there can only be ONE 'Michael A. Kline'.

    Usually, a multi-field primary key is a combination of other primary keys from other tables and the record contents describe content relavant to the specific key values. For example:

    Table #1: Student Records (KEY: student_id)
    Table #2: Course Records (KEY: course_id)
    Table #3: Student Grades (KEY: student_id, course_id)
    

    Hope this helps.

提交回复
热议问题