问:GUID [英] Q: GUID
问题描述
嗨
如何将主键设置为GUID?
也就是说,使用以下代码,我可以设置一个整数主键,但如何将
更改为GUID?
Dim databaseCommand As OleDbCommand = New OleDbCommand(" CREATE TABLE books
(" ID INT IDENTITY,Book VARCHAR(50),CONSTRAINT [pk_ID] PRIMARY KEY(ID))",
myDatabaseConnection)
>
非常感谢提前
Geoff
Geoff Jones写道:如何将主键设置为GUID?
即使用以下代码,我可以设置整数主键,
但是如何更改它是一个GUID?
这可能更适合SQL Server新闻组(假设它是SQL
服务器你''重新使用),但这可能会有所帮助。创建默认限制在
ID字段,它给出值NewID()。这是一个生成GUID的SQL Server
函数。因此,更改这两个
SQL语句的表创建:
CREATE TABLE书籍
(
[ID] [varchar](50)NOT NULL,
[Book] [varchar](50)NULL
CONSTRAINT [pk_ID] PRIMARY KEY([ID])
)
ALTER TABLE [dbo]。[books] ADD
CONSTRAINT [DF_books_ID] DEFAULT(newid())FOR [ID]
现在,如果您在表格中插入值并且没有为
ID字段指定任何内容,它将自动收到每行的GUID。
-
(O)enone
CREATE TABLE [dbo]。[books](
[book_id] [uniqueidentifier] NOT NULL
)ON [PRIMARY]
GO
ALTER TABLE [dbo]。[books] ADD
CONSTRAINT [DF_books_bookid_quid] DEFAULT(newid())FOR [book_id],
CONSTRAINT [PK_books] PRIMARY KEY CLUSTERED
(
[book_id]
)ON [PRIMARY]
GO
HTH,
Greg
" Geoff Jones" < GE *** @ NODAMNSPAM.com>在消息中写道
news:41 *********************** @ news.dial.pipex.com ...嗨
如何将主键设置为GUID?
即使用以下代码,我可以设置一个整数主键,但是
如何将其更改为GUID?
Dim databaseCommand As OleDbCommand = New OleDbCommand(" CREATE TABLE books
(" ID INT IDENTITY,Book VARCHAR( 50),CONSTRAINT [pk_ID] PRIMARY KEY
(ID))",myDatabaseConnection)
非常感谢提前
Geoff
2004年8月5日星期四11:59:44 +0100,Geoff Jones写道:
即使用下面的代码,我可以设置一个整数主键,但是如何将它更改为GUID?
使列类型为UNIQUEIDENITFIER类型
-
克里斯
dunawayc [AT] sbcglobal_lunchmeat_ [DOT] net
>
要给我发电子邮件,请删除[",],下划线,午餐和
替换我的电子邮件地址中的某些字词。 br />
Hi
How do I set a primary key to be a GUID?
That is, using the following code, I can set an integer primary key, but how
do I change it to a GUID?
Dim databaseCommand As OleDbCommand = New OleDbCommand("CREATE TABLE books
(" ID INT IDENTITY, Book VARCHAR(50), CONSTRAINT [pk_ID] PRIMARY KEY (ID))",
myDatabaseConnection)
Many thanks in advance
Geoff
Geoff Jones wrote:How do I set a primary key to be a GUID?
That is, using the following code, I can set an integer primary key,
but how do I change it to a GUID?
This is probably better suited to a SQL Server newsgroup (assuming it''s SQL
Server you''re using), but this might help. Create a "default" constraint on
the ID field which gives it the value "NewID()". This is a SQL Server
function that generates a GUID. So change your table creation for these two
SQL statements:
CREATE TABLE books
(
[ID] [varchar] (50) NOT NULL ,
[Book] [varchar] (50) NULL
CONSTRAINT [pk_ID] PRIMARY KEY ([ID])
)
ALTER TABLE [dbo].[books] ADD
CONSTRAINT [DF_books_ID] DEFAULT (newid()) FOR [ID]
Now if you insert values into the table and don''t specify anything for the
ID field, it''ll automatically receive a GUID for each row.
--
(O)enone
CREATE TABLE [dbo].[books] (
[book_id] [uniqueidentifier] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[books] ADD
CONSTRAINT [DF_books_bookid_quid] DEFAULT (newid()) FOR [book_id],
CONSTRAINT [PK_books] PRIMARY KEY CLUSTERED
(
[book_id]
) ON [PRIMARY]
GO
HTH,
Greg
"Geoff Jones" <ge***@NODAMNSPAM.com> wrote in message
news:41***********************@news.dial.pipex.com ...Hi
How do I set a primary key to be a GUID?
That is, using the following code, I can set an integer primary key, but how do I change it to a GUID?
Dim databaseCommand As OleDbCommand = New OleDbCommand("CREATE TABLE books
(" ID INT IDENTITY, Book VARCHAR(50), CONSTRAINT [pk_ID] PRIMARY KEY (ID))", myDatabaseConnection)
Many thanks in advance
Geoff
On Thu, 5 Aug 2004 11:59:44 +0100, Geoff Jones wrote:
That is, using the following code, I can set an integer primary key, but how
do I change it to a GUID?
Make your column type a UNIQUEIDENITFIER type
--
Chris
dunawayc[AT]sbcglobal_lunchmeat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
这篇关于问:GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!