What GUID can I use as a placeholder
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 row开发者_如何学JAVAs are inserted in the future? Does SQL Server enforce uniqueness on this column?
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.
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.
精彩评论