Moving from ints to GUIDs as primary keys

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

  • 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.