大家帮个忙,不要复制粘贴索引的什么关系,看烦了,就想知道怎么建立聚合索引的代码,菜鸟一个,奉100分,以谢请高手们列出建立聚合索引代码,组合索引代码 两个字段的,谢谢了

解决方案 »

  1.   

    聚集索引是指索引序列与表中行的排列顺序一致,组合索引是指建立在多列上的索引.
    CREATE TABLE tb(id int NOT NULL PRIMARY KEY CLUSTERED,
    col nchar(10) NULL
    )ON [PRIMARY]
    --在建表的同时创建了作为主键的聚集索引,基于id列
    GO
    drop table tb
    go
    CREATE TABLE tb(
    id int NULL,
    col nchar(10) NULL
    ) ON [PRIMARY]
    GO
    CREATE CLUSTERED INDEX IX_tb ON dbo.tb(id)
    --单独创建聚集索引 ix_tb,基于 id 列.
    go
    drop table tb
    go
    CREATE TABLE tb(
    id1 nvarchar(50) NOT NULL,
    id2 nvarchar(50) NOT NULL,
    col nchar(10) NULL
    CONSTRAINT PK_tb PRIMARY KEY CLUSTERED(id1,id2)
    )  ON [PRIMARY]
    --在建表的同时创建了作为主键的聚集索引,基于id1,id2两列
    GO
    drop table tb
    go
    CREATE TABLE tb(
    id1 varchar(10) NOT NULL,
    id2 varchar(10) NOT NULL,
    col nchar(10) NULL
    ) ON [PRIMARY]
    GO
    CREATE CLUSTERED INDEX IX_tb ON dbo.tb(id1,id2)
    --单独创建聚集索引 ix_tb,基于 id1,id2 两列.
    go
    drop table tb
    --如果不想作为聚集索引,把上面的 CLUSTERED 拿掉就行了
      

  2.   

    create table test_index 
    (
     stuID int primary key,
     stuName char(10),
     stuClass  tinyint,
     stuGender bit,
     stuAddress varchar(100)
    )
    --组合索引
    Create index student on test_index (stuName,stuClass)
    --聚集索引
    CREATE CLUSTERED INDEX ClusteredIndex ON TableName (stuname);
      

  3.   


    --聚集索引
    CREATE CLUSTERED INDEX idx_name ON tb(col)
    --组合索引
    CREATE INDEX idx_name ON tb(col1,col2)