一,UniqueIdentifier 是一个数据类型,用于存储GUID的值。
uniqueidentifier data type Is a 16-byte GUID.
A column or local variable of uniqueidentifier data type can be initialized to a value in the following ways:
- By using the NEWID function.
- By converting from a string constant in the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, in which each x is a hexadecimal digit in the range 0-9 or a-f. For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value.
Comparison operators can be used with uniqueidentifier values. However, ordering is not implemented by comparing the bit patterns of the two values. The only operations that can be performed against a uniqueidentifier value are comparisons (=, <>, <, >, <=, >=) and checking for NULL (IS NULL and IS NOT NULL). No other arithmetic operators can be used. All column constraints and properties, except IDENTITY, can be used on the uniqueidentifier data type.
Merge replication and transactional replication with updating subscriptions use uniqueidentifier columns to guarantee that rows are uniquely identified across multiple copies of the table.
Converting uniqueidentifier Data
The uniqueidentifier type is considered a character type for the purposes of conversion from a character expression, and therefore is subject to the truncation rules for converting to a character type. That is, when character expressions are converted to a character data type of a different size, values that are too long for the new data type are truncated.
二,对UniqueIdentifier 变量赋值方式有两种,分别是使用GUID产生函数和字符串赋值
1,使用GUID产生函数赋值
declare @ui uniqueidentifier set @ui=newid() select @ui
2,使用字符串赋值,字符串的格式是2-1-1-1-3,即‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’
declare @ui uniqueidentifier set @ui=‘AA786048-44BB-E511-80E3-F8B156CF6E62‘ select @ui
三,UniqueIdentifier data type 用于创建table column
1,NewID()用于Default constraint,为每一个数据行生成一个unique value
CREATE TABLE dbo.myTable_Rand ( ColumnA uniqueidentifier DEFAULT NewID(), ColumnB int, columnC varchar(10) ) go
NewID()函数产生GUID是unique,缺点是value序列的大小是随机的,不是一个始终增加的值(ever-increasing value),Sql Sever 不保证产生的GUID比之前的GUID 大或小。如果将NewID()函数产生GUID作为clustered index key,那么新的数据行插入的位置是random的,导致Page split ,降低 IO 性能。
最佳的clustered index key 应该是递增的(increase),data type is narrow (narrow),值是unique(unique),不会频繁更新(static),NewID() 产生的GUID满足narrow,unique, static,但是不满足increase,因此不是理想中的clustered index key。
2,有序的GUID
NEWSEQUENTIALID()函数产生的GUID值是始终增加(ever-increasing value)的,Sql Sever 保证产生的GUID比之前的GUID 大。NewSequentialID()函数只能用于Table的Default constraint中。
如果将NewSequentialID() 产生的GUID作为clustered index key,那么Insert会将新的row 将插入到table的末尾,reduce page split,推荐使用NewSequentialID()作为clustered index key。
Creates a GUID that is greater than any GUID previously generated by this function on a specified computer since Windows was started. After restarting Windows, the GUID can start again from a lower range, but is still globally unique. When a GUID column is used as a row identifier, using NEWSEQUENTIALID can be faster than using the NEWID function. This is because the NEWID function causes random activity and uses fewer cached data pages. Using NEWSEQUENTIALID also helps to completely fill the data and index pages.
NEWSEQUENTIALID() can only be used with DEFAULT constraints on table columns of type uniqueidentifier.
You can use NEWSEQUENTIALID to generate GUIDs to reduce page splits and random IO at the leaf level of indexes.
Each GUID generated by using NEWSEQUENTIALID is unique on that computer.
CREATE TABLE dbo.myTable ( ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID(), ColumnB int, columnC varchar(10) ) go insert into dbo.myTable(ColumnB,columnC) values(1,‘a‘),(2,‘c‘) go select * from dbo.myTable go
四,UniqueIdentifier 的 ROWGUIDCOL 属性
ROWGUIDCOL 属性标记一个 UniqueIdentifier data type的column,可以使用 $ROWGUID 来引用这个column ,一个table 只能有一个uniqueidentifier column 具有RowGUIDCol属性。
CREATE TABLE dbo.myTable_RowGUIDCol ( ColumnA uniqueidentifier ROWGUIDCOL not null constraint DF__myTable_RowGUIDCol_ColumnA DEFAULT NewID(), ColumnB int, columnC varchar(10) ) go
引用$ROWGUID查看被标记为RowGUIDCol 的column
select $ROWGUID from dbo.myTable_RowGUIDCol
Indicates that the new column is a row GUID column. Only one uniqueidentifier column per table can be designated as the ROWGUIDCOL column. Applying the ROWGUIDCOL property enables the column to be referenced using $ROWGUID. The ROWGUIDCOL property can be assigned only to a uniqueidentifier column. User-defined data type columns cannot be designated with ROWGUIDCOL.
The ROWGUIDCOL property does not enforce uniqueness of the values stored in the column. ROWGUIDCOL also does not automatically generate values for new rows inserted into the table. To generate unique values for each column, either use the NEWID or NEWSEQUENTIALID function on INSERT statements or use these functions as the default for the column.
参考文档:
https://msdn.microsoft.com/en-us/library/ms187942(v=sql.110).aspx
https://msdn.microsoft.com/en-us/library/ms190348(v=sql.110).aspx
https://msdn.microsoft.com/en-us/library/ms189786(v=sql.110).aspx