代码之家  ›  专栏  ›  技术社区  ›  Hacker

主键和索引概念

  •  0
  • Hacker  · 技术社区  · 15 年前

    2 回复  |  直到 15 年前
        1
  •  2
  •   etoisarobot    15 年前

    或者更具体地说 here

    注意主键约束创建 上已存在聚集索引 不支持表和非聚集索引

        2
  •  0
  •   Sid    12 年前

    这应该能排除一些空气。

    --creating a table without any primary key
    CREATE table understanding_indexes
     (P_Id int,
     LastName varchar(255),
     FirstName varchar(255),
     Address varchar(255),
     City varchar(255)
     )
     --
     --checking for indexes
     sp_helpindex understanding_indexes
    

     The object 'understanding_indexes' does not have any indexes, or you do not have permissions.
    
    --ADDING A NOT NULL CONSTRAINT
    ALTER TABLE UNDERSTANDING_INDEXES 
    ALTER COLUMN P_Id INTEGER
    NOT NULL
    
    --ADDING A PRIMARY KEY Constraint, can only be done on column which are not null.
    ALTER TABLE UNDERSTANDING_INDEXES
    ADD PRIMARY KEY (P_Id)
    
    sp_helpindex understanding_indexes
    

    输出

    PK__understa__A3420A5702084FDA  clustered, unique, primary key located on PRIMARY  P_Id
    

    此图位于SQL Server 2008 R2上。