Create a nonclustered non-unique index within the CREATE TABLE statement with SQL Server

前端 未结 4 2058
小鲜肉
小鲜肉 2020-12-23 13:00

It is possible to create a primary key or unique index within a SQL Server CREATE TABLE statement. Is it possible to create a non-unique index within a CREA

4条回答
  •  庸人自扰
    2020-12-23 13:40

    It's a separate statement.

    It's also not possible to insert into a table and select from it and build an index in the same statement either.

    The BOL entry contains the information you need:

    CLUSTERED | NONCLUSTERED
    Indicate that a clustered or a nonclustered index is created for the PRIMARY KEY or UNIQUE constraint. PRIMARY KEY constraints default to CLUSTERED, and UNIQUE constraints default to NONCLUSTERED.

    In a CREATE TABLE statement, CLUSTERED can be specified for only one constraint. If CLUSTERED is specified for a UNIQUE constraint and a PRIMARY KEY constraint is also specified, the PRIMARY KEY defaults to NONCLUSTERED.

    You can create an index on a PK field, but not a non-clustered index on a non-pk non-unique-constrained field.

    A NCL index is not relevant to the structure of the table, and is not a constraint on the data inside the table. It's a separate entity that supports the table but is not integral to it's functionality or design.

    That's why it's a separate statement. The NCL index is irrelevant to the table from a design perspective (query optimization notwithstanding).

提交回复
热议问题