Two column as a primary keys in mysql?

前端 未结 8 1703
孤独总比滥情好
孤独总比滥情好 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:36

    Also one thing to note: Primary keys are automatically indexed in MySQL. And order of columns you mention in primary key does matter for performance as mentioned here

    0 讨论(0)
  • 2020-12-15 06:41

    Yes, this is a normal thing to do in SQL and it works (having a composite primary key, in which multiple fields together constitute a unique value).

    Two notes:

    1. Make sure it's necessary. It often is and then it's fine. But sometimes it's a sign that you further need to normalize your data model.

    2. I'm thinking that you don't want to make and a b be foreign keys from another table and then make them the composite primary key of your table. What happens if you set up a cascading delete, in which one user id, but not the other is deleted? So the composite primary key is fine, but then you don't want come from "unrelated" foreign keys.

    0 讨论(0)
  • 2020-12-15 06:41

    ALTER TABLE TableName DROP PRIMARY KEY, ADD PRIMARY KEY( column1, column2); if you have set primary key previously, then try this.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 06:55

    To make it easier to explain, I will only use one table. Create a table with 2 int columns, and a PK on both of them together. As in the question.

    create table test(
      a  INT  NOT NULL ,
      b  INT  NOT NULL ,
      PRIMARY KEY(a,b));
    

    Now we can add rows, until we get an error

    insert into test values(1,1); 
    Query OK, 1 row affected (0,00 sec)
    
    insert into test values(1,2); 
    Query OK, 1 row affected (0,00 sec)
    
    insert into test values(1,1); 
    ERROR 1062 (23000): Duplicate entry '1-1' for key 'PRIMARY'
    

    Which is logical because the combined values of the 2 columns which make up the PK are not unique anymore when this last statement would be executed.

    It is allowed to store 2x the value 1 in a, because that is not the PK. The PK is the combined value of columns a and b.

    0 讨论(0)
  • 2020-12-15 06:58

    I believe what is happening is the paired columns together are a primary. For example you know you cannot have a duplicate primary column Ex: if col "a" is primary you cannot have two rows that have the same value for a.

    In this example you have two primaries; which means you can only have one unique value for each col pair. For example if col 'a' and 'b' are primary and 'c' is not: a|b|c 1,2,3 works 1,4,5 works and 5,1,6 works 9,1,10 works

    but you cannot have: 9,8,10 9,8,6 because for that (9,8) pair you can only have one unique value...

    Does that make sense or would you like me to further elaborate?

    0 讨论(0)
提交回复
热议问题