开发者

Can you create a table with indexes at the same time?

开发者 https://www.devze.com 2022-12-26 09:51 出处:网络
I\'d like to create a table: CREATE TABLE sfc.OpenId ( Url VARCHAR(255) PRIMARY KEY, UserGuid uniqueidentifier NOT NULL references dbo.aspnet_users(userId),

I'd like to create a table:

CREATE TABLE sfc.OpenId (
  Url VARCHAR(255) PRIMARY KEY,
  UserGuid uniqueidentifier NOT NULL references dbo.aspnet_users(userId),
)

...w开发者_如何学Cith an index on UserGuid.

Is it possible to create that index in the create table statement?


You can do that if the index on UserGuid is a unique index, via UNIQUE constraint. Otherwise, no.


Is it possible to create that index in the create table statement?

No, only constraints can be created within the CREATE TABLE syntax.

f not defined otherwise, the primary key will automatically be a CLUSTERED index - but that doesn't cover the userguid column. The CREATE INDEX syntax needs to be a separate statement otherwise.


Can you clarify why?

You can use transactions with DDL in SQL server and for most purposes this is equivalent to doing it at the same time.

0

精彩评论

暂无评论...
验证码 换一张
取 消