Can you use auto-increment in MySql with out it being the primary Key

16,440

Solution 1

A GUID value is intended to be unique across tables and even databases so, make the auto_increment column primary index and make a UNIQUE index for the GUID

Solution 2

I would lean the other way.

Why? Because creating a composite key gives the impression to the next guy who comes along that it's OK to have the same GUID in the table twice but with different sequence numbers.

Share:
16,440
Sughiy
Author by

Sughiy

Twitter @GoneFishern

Updated on June 04, 2022

Comments

  • Sughiy
    Sughiy almost 2 years

    I am using GUIDs as my primary key for all my other tables, but I have a requirement that needs to have an incrementing number. I tried to create a field in the table with the auto increment but MySql complained that it needed to be the primary key.

    My application uses MySql 5, nhibernate as the ORM.

    Possible solutions I have thought of are:

    • change the primary key to the auto-increment field but still have the Id as a GUID so the rest of my app is consistent.

    • create a composite key with both the GUID and the auto-increment field.

    My thoughts at the moment are leaning towards the composite key idea.

    EDIT: The Row ID (Primary Key) is the GUID currently. I would like to add an an INT Field that is Auto Incremented so that it is human readable. I just didn't want to move away from current standard in the app of having GUID's as primary-keys.