可以在SQL Server CREATE TABLE语句内创建主键或唯一索引。是否可以在CREATE TABLE语句内创建一个非唯一索引?使用SQL Server在CREATE TABLE语句内创建非集群非唯一索引
CREATE TABLE MyTable(
a int NOT NULL
,b smallint NOT NULL
,c smallint NOT NULL
,d smallint NOT NULL
,e smallint NOT NULL
-- This creates a primary key
,CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED (a)
-- This creates a unique nonclustered index on columns b and c
,CONSTRAINT IX_MyTable1 UNIQUE (b, c)
-- Is it possible to create a non-unique index on columns d and e here?
-- Note: these variations would not work if attempted:
-- ,CONSTRAINT IX_MyTable2 INDEX (d, e)
-- ,CONSTRAINT IX_MyTable3 NONCLUSTERED INDEX (d, e)
);
GO
-- The proposed non-unique index should behave identically to
-- an index created after the CREATE TABLE statement. Example:
CREATE NONCLUSTERED INDEX IX_MyTable4 ON MY_TABLE (d, e);
GO
此外,目标是在CREATE TABLE语句中创建非唯一索引,而不是在它之后创建。
对于它的价值,我没有找到[SQL Server Books Online entry for CREATE TABLE]有帮助。
另外,[This Question]几乎相同,但接受的答案不适用。
感谢您的好解释!为什么?纯粹是出于美学原因。我认为如果所有的约束/索引都包含在同一个语句中,那么读取脚本可能会很方便。就我个人而言,我想知道属于外键的列是否也有索引,并且这可能是将这些信息在同一语句中进行逻辑分组的不错方法。 – Mike 2011-05-31 21:12:22
我得到了'错误:(1146)表'tablename'不存在',哈哈哈,讽刺的 – 2016-11-03 14:23:03