Saturday, February 25, 2012

Create INDEX within CREATE TABLE DDL

Hi

Minor and inconsequential but sometimes you just gotta know:

Is it possible to define a non-primary key index within a Create Table statement? I can create a constraint and a PK. I can create the table and then add the index. I just wondered if you can do it in one statement.

e.g. I have:

CREATE TABLE MyT
(MyT_ID INT Identity(1, 1) CONSTRAINT MyT_PK PRIMARY KEY Clustered,
MyT_Desc Char(40) NOT NULL CONSTRAINT MyT_idx1 UNIQUE NONCLUSTERED ON [DEFAULT])
which creates a table with a PK and unique constraint.
I would like (pseudo SQL):
CREATE TABLE MyT
(MyT_ID INT Identity(1, 1) CONSTRAINT MyT_PK PRIMARY KEY Clustered,
MyT_Desc Char(40) NOT NULL CONSTRAINT MyT_idx1 UNIQUE INDEX NONCLUSTERED ON [DEFAULT])

No big deal - just curious :D Once I know I can stop scouring BOL for clues.

Tks in advanceI don't think so. I don't recall seeing any syntax that allows this. Non-clustered indexes are separate objects from the table, and that is probably why they need to be created separately, and can be dropped separately as well.|||I don't think so. I don't recall seeing any syntax that allows this. Non-clustered indexes are separate objects from the table, and that is probably why they need to be created separately, and can be dropped separately as well.
Cheers BM - didn't think of it like that - that does kind of make sense - you can't create an object dependent on another object before the first object exists. Or something similar but more felicitous ;)

No comments:

Post a Comment