views:

56

answers:

2

I have a database table that has a non null column of type uniqueidentifier. This was put in place for use in the near future. But for now, I need to use some placeholder. Can I simply use:

00000000-0000-0000-0000-000000000000

for all the rows until a real guid is used when new rows are inserted in the future? Does SQL Server enforce uniqueness on this column?

+6  A: 

SQL Server will enforce uniqueness, IF and only if you put a unique constraint or unique index on that field. Otherwise, SQL Server will only enforce that the value must be NOT NULL.

marc_s
very true, it's totally up to you
Pieter888
+2  A: 

As marc_s, says, you can do that because uniqueness is not enforced for uniqueidentifiers even in the same column of a table without an explicit declared unique index/constraint (after all, two rows can legitimately have the same foreign key).

IF this is just a temporary bootstrap, and in the future only real GUIDs (NOT NULL) are going to be allowed, I think this is an OK workaround to avoid generating GUIDs which just need to be replaced later and so not needing to keep a separate partially-initialized flag column or table of the temporary rows so you can fill in the appropriate GUIDs later.

However, from a design point of view, I'm more concerned about the semantics of what this special reserved GUID is, and why allowing a special reserved value is OK, but NULLs are not. Like I said, if it's just temporary and in the steady state, you don't want to ever allow NULLs OR this special reserved 0, that's fine, but if you are going to continue to allow this special reserved GUID in steady state operations, I think that raises design questions.

Is it meant to be a foreign key? If so, NULLs can be used (but a reserved key value like 0 which is not in the referenced table cannot). If it's a loose association, storing the GUID in this table might not be a great design.

Cade Roux
+1. The very meaning of `NULL` is "unknown" or "undefined" - if the column can have unknown values then it should be nullable! An empty uuid is no better than an empty string.
Aaronaught
@Aaronaught, NULL interpretation is a can of worms indeed. NOT NULL should be considered first in any column design, but usage of a NULL with a single interpretation should be preferred before single reserved values. Then if multiple reserved values are needed (DateOfDeath: 1-not dead, 2-unknown date), then the design needs to think how to handle this reliably and ensure integrity and consistency and perimeter defense against both insertion and misinterpretation.
Cade Roux
Cade, while I agree with you in general, in this case, it seems like NULL would be a viable choice over a "magic" / dummy value / placeholder, at least until the rest of the design comes about.
Aaron Bertrand
@Aaron - right - I question why the NOT NULL constraint on the table and unless that's a long-term design intention and this workaround is just temporary, I think it's fine, but if this behavior is intended long term, I would recommend using NULL. If there are multiple special "No-GUID" cases, then either NULL with a flag/type column or some kind of associative link table is a better design.
Cade Roux