tags:

views:

53

answers:

3

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?

+1  A: 

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

hongliang
no, the openId spec is that the same 'user' can have many URLs associated with them.
Maslow
+1  A: 

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.

OMG Ponies
+1  A: 

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.

Unreason
convenience, less typing, less work. instead of having to type `begin tran` (statement) `rollback commit`. highlighting the `begin tran` through the end of my statement and seeing if it works, then hitting `commit` or `rollback` highlighted selectively, additionally adding something to a table after the create is generally a lot more verbose.
Maslow
ok, I though the functionality was your primary goal... in that case - no AFAIK. still if we are talking about convenience - one could make an argument that indexes do now fall into the same category as create table statements. for certain activities you might want to drop all your indexes, change them or test different indexes - for this purpose verbosity and having the indexes separated from table definitions is convenient. maintaining database create script is never that much work (compared to maintaining the whole system) unless you have hundreds of tables in which case you are...
Unreason
..better off scripting the script itself (from some data dictionary) where you will not have to type anything anyway (verbosity is not important). Having a verbose (and well commented) database create script is a plus, IMO.
Unreason