Friday, February 24, 2012

How to use both auto generated id and a guid in database

I downloaded the AdventureWorks OLTP Schema and am trying to use some of the design concepts that are used in this example but I don’t quite understand some things.

First there is a column named rowguid, at the moment I assume that this is a GUID. If this is wrong most likely my next question will make no sense.

Second if that is indeed a guid all the tables have another id; example would be Employee table would have employeeid and rowguid, why do this? My best guess is that you would use the guid to easily insert (linked/sync) data into multiple tables without needing to retrieve the id from the table that was first input into. My scenario would be inserting an employee; to insert an employee you need to create a row in the employee, contact and address tables and for ease you would use the guid to link these rows. But if this is the case wouldn’t there be sync issues that would arise?

If my guess is totally off please correct me also if anyone can direct me to some good resources that cover database design.

The schema can be downloaded at http://www.microsoft.com/downloads/details.aspx?familyid=0F6E0BCF-A1B5-4760-8D79-67970F93D5FF&displaylang=en#filelist

Thanks for any help.

The rowguid is actually used here as part of a replication example:

http://msdn2.microsoft.com/en-us/library/ms124807.aspx

Replication can add a column by this name to a schema to give it a unique value across servers. Rowguid is also a property that you can add to a guid column to tell it that it is an identifier for the row. You wouldn't generally want to have both types if you were designing a table yourself. You would just use the primary key that is set up to do the relationships and joins on.

They did this because they can use one datatype, always having a unique value across servers, no matter the table structures and type of the primary key.

No comments:

Post a Comment