views:

3212

answers:

6

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.

A: 

GUID's are not intended to be orderable, that's why AUTO_INCREMENT for them does not make sense.

You may, though, use an AUTO_INCREMENT for a second column of a composite primary key in MyISAM tables. You can create a composite key over (GUID, INT) column and make the second column to be AUTO_INCREMENT.

To generate a new GUID, just call UUID() in an INSERT statement or in a trigger.

Quassnoi
+3  A: 

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.

cletus
A: 

A couple of thoughts:

  • If your GUID is auntoincremental and unique, why not let it be the actual Primary Key?

  • On the other hand, you should never take semantical decisions based on programmatic problems: you have a problem with MySQL, not with the design of your DB.

So, a couple of workarounds here:

  • Creating a trigger that would set the GUID to the proper value once it's inserted. That's a MySQL solution to a MySQL problem, without altering semantics for your schema.

  • Before inserting, start a transaction (make sure auto commit is set to false), find out the latest GUID, increment and insert with the new value. In other words, auto-increment not automatically :P

Seb
GUIDs are not autoincremental. they're like UUIDs, a semi-random very long number so it's extremely unlikely to collide anywhere.
Javier
Yeah, I know GUIDs are long numbers, but he asked for they being autoincremental, so that's what I answered :)
Seb
+6  A: 

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

jab
I think this is probably the best way to go for my situation. thanks.
Nathan Fisher
Yup ... this is it.
jalchr
+4  A: 

No, only the primary key can have auto_increment as its value.

Spikolynn
Wrong. Any key in MySQL can have auto_increment. http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
Dustin Fineout
A: 

If, for some reason, you can't change the identity column to be a primary key, what about manually generating the auto-increment via some kind of SEQUENCE table plus a trigger to query the SEQUENCE table and save the next value to use. Then assign the value to the destination table in the trigger. Same effect. The only question I would have is whether the auto-incremented value is going to make it back thru NHibernate without a re-select of the table.

Rich