views:

58

answers:

2

Hi,

I would like create a table and add to it a Primary Key.

As for my understanding MS SQL add a clustered Index on the Primary Key and will name it with a default name.

I would like to know if is possible create a table and ASSIGN a custom name for the index created by default or how can i change the default name after the table as been created.

Thanks!

A: 

When you create a Clustered Primary Key, you do´nt create a index, but a Table organized as index.

Clustered Primary key is default option when you create a table with primary key on SqlServer.

http://msdn.microsoft.com/en-us/library/aa933131(SQL.80).aspx

x77
Oh yes, you **DO** create an index! It's the clustered index - and yes, the table is arranged by that index - but it's still an index no less.
marc_s
I use oracle. On oracle IOT Tables (Organization Index) are similar to SqlServer Clustered Tables. The segment for the index on a IOT Table is named as TableName (is same segment).
x77
+1  A: 

Sure - you can define the PRIMARY KEY constraint in your CREATE TABLE statement.

This will generate the default PRIMARY KEY

CREATE TABLE dbo.Table
  (ID INT IDENTITY PRIMARY KEY,
    .......)

but you can totally define the name of the constraint, too:

CREATE TABLE dbo.Table2
  (ID INT IDENTITY CONSTRAINT PK_Table2 PRIMARY KEY,
    ......)
marc_s
ok great, but how can i choose the name of index instead of using the default one?
GIbboK
@GlbboK: see my second example - you can choose the name for the CONSTRAINT which is also the name for the index: `CONSTRAINT PK_Table2 PRIMARY KEY` - you get to choose whether it's called `PK_Table2` or whatever else you might want to call it....
marc_s