Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving from ints to GUIDs as primary keys

I use several referenced tables with integer primary keys. Now I want to change ints to GUIDs leaving all references intact. What is the easiest way to do it?

Thank you!

Addition

I do understand the process in general, so I need more detailed advices, for example, how to fill new GUID column. Using default value newid() is correct, but what for already existing rows?

like image 300
Alexander Prokofyev Avatar asked Sep 26 '08 07:09

Alexander Prokofyev


People also ask

Is it good to use GUID as primary key?

GUIDs may seem to be a natural choice for your primary key - and if you really must, you could probably argue to use it for the PRIMARY KEY of the table. What I'd strongly recommend not to do is use the GUID column as the clustering key, which SQL Server does by default, unless you specifically tell it not to.

Should I use int or GUID as primary key?

int is smaller, faster, easy to remember, keeps a chronological sequence. And as for Guid, the only advantage I found is that it is unique. In which case using sql server guid would be better than and int and why? From what I've seen, int has no flaws except by the number limit, which in many cases are irrelevant.

Why an integer is better as a primary key?

The most frequently-used data types for primary keys are: Numeric (integer). This is the simplest to use and to auto-populate. It uses less space than other data types (usually 1-8 bytes), thus both saving storage and enhancing JOIN and LOOKUP.

Should I use GUIDs?

GUIDs are seldom needed for database replication/import/export. So, instead of having the GUID on the main table, where it takes up an extra 8-bytes per row, and where a GUID index will be (by default) stored on the same volume; a separate table (aka normalization) comes to the rescue.


2 Answers

  • Create a new column for the guid value in the master table. Use the uniqueidentifier data type, make it not null with a newid() default so all existing rows will be populated.
  • Create new uniqueidentifier columns in the child tables.
  • Run update statements to build the guild relationships using the exisitng int relationships to reference the entities.
  • Drop the original int columns.

In addition, leave some space in your data/index pages (specify fillfactor < 100) as guids are not sequential like int identity columns are. This means inserts can be anywhere in the data range and will cause page splits if your pages are 100% full.

like image 120
Andy Jones Avatar answered Oct 11 '22 04:10

Andy Jones


Firstly: Dear God why?!?!?

Secondly, you're going to have to add the GUID column to all your tables first, then populate them based on the int value. Once done you can set the GUIDs to primary/foreign keys then drop the int columns.

To update the value you'd do something like

  1. Set the new GUIDs in the primary key table
  2. Run this:

.

UPDATE foreignTable f
SET f.guidCol = p.guidCol
FROM primaryTable p
WHERE p.intCol = f.intCol
like image 29
Glenn Slaven Avatar answered Oct 11 '22 05:10

Glenn Slaven