Maslow
Maslow

Reputation: 18746

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

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),
)

...with an index on UserGuid.

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

Upvotes: 2

Views: 2550

Answers (3)

Unreason
Unreason

Reputation: 12704

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.

Upvotes: 1

OMG Ponies
OMG Ponies

Reputation: 332571

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.

Upvotes: 1

hongliang
hongliang

Reputation: 625

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

Upvotes: 2

Related Questions