SQL Server中的uniqueidentifier類型


uniqueidentifier類型可以配合T-SQL中的newid和newsequentialid來生成唯一標識符,具體區別如下(摘抄自微軟官方文檔)。

Nonsequential GUIDs: You can generate nonsequential global unique identifiers to be stored in an attribute of a UNIQUEIDENTIFIER type. You can use the T-SQL function NEWID to generate a new GUID, possibly invoking it with a default expression attached to the column. You can also generate one from anywhere. for example, the client by using an application programming interface (API) that generates a new GUID. The GUIDs are guaranteed to be unique across space and time.

Sequential GUIDs: You can generate sequential GUIDs within the machine by using the T-SQL function NEWSEQUENTIALID.

 

測試代碼:

create table t_1(
id uniqueidentifier ROWGUIDCOL  primary key default NEWSEQUENTIALID(),
value int
)
create table t_2(
id uniqueidentifier ROWGUIDCOL  primary key default NEWID(),
value int
)

insert into t_1(value) values (1)
insert into t_1(value) values (2)
insert into t_1(value) values (3)
insert into t_1(value) values (4)
insert into t_1(value) values (5)

insert into t_2(value) values (1)
insert into t_2(value) values (2)
insert into t_2(value) values (3)
insert into t_2(value) values (4)
insert into t_2(value) values (5)

select *,rowguidcol from t_1
select *,rowguidcol from t_2

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM