2009-10-30 69 views
0

當我將用戶添加到我的數據庫時,我使用Guid作爲主鍵。但是它只是一串固定的0。我應該在哪裏設置它?我將它設置爲 「ROWGUID()」 中的MS-SQL服務器...未創建GUID主鍵

回答

9

嘗試設置一欄,NEWID缺省值()

3

你不想ROWGUID()。你想要NewID()。

 
CREATE TABLE myTable(GuidCol uniqueidentifier, NumCol int) 
INSERT INTO myTable Values(NEWID(), 4) 
SELECT * FROM myTable 
0

低於用戶表可以如何與GUID作爲PrimaryKey的列看一個小例子:

CREATE TABLE [dbo].[Users](
    [userId] [uniqueidentifier] NOT NULL, 
    [Name] [nvarchar](255) NOT NULL, 
CONSTRAINT [PK_Users_1] PRIMARY KEY CLUSTERED 
(
    [userId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_userId] DEFAULT (newid()) FOR [userId]