参考官网 : https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2005/ms189826(v=sql.90)
在 SQL Server 表的主键有自增Id ,和 GUID。
1. 自增Id
优点:索引空间小,索引连续。在大量数据插入的时候性能有特别大的优势。
缺点:可移植性差,在数据迁移的时候。
2. GUID
优点:数据迁移的时候很容易。
缺点:索引占用空间大,因为GUID的随机性,在插入数据的时候会导致索引上的页争用。
SQL SERVER 2005新加了 NEWSEQUENTIALID(),这是个内置函数,不能用与 SELECT
-- 创建的表加约束 default newsequentialid()
create table #t
(
id uniqueidentifier not null default newsequentialid()
,name varchar(100)
)
go
--插入表100条数据,并且要指定列名
insert into #t(name) values('a')
go 100
select * from #t