database: primary key, Clustered or NonClustered

前端 未结 1 670
被撕碎了的回忆
被撕碎了的回忆 2020-12-28 12:56

I am creating a database in SQL server 2008,

CREATE TABLE Users
(
    U_Id INT NOT NULL
    FirstName VARCHAR(50) NOT NULL,
    LastName VARCHAR(50) NOT NULL         


        
相关标签:
1条回答
  • 2020-12-28 13:29

    The following statement:

    CONSTRAINT pk_UserID PRIMARY KEY (U_Id)
    

    Is the same as this one:

    CONSTRAINT pk_UserID PRIMARY KEY CLUSTERED (U_Id)
    

    You can only have the table data physicality ordered by one of the indexes, and by default that index is the one used for the primary key (the primary key unique constraint is always supported by an index).

    If you want to leave the order of the table data to be stored according to some other index then you should create the primary key with:

    CONSTRAINT pk_UserID PRIMARY KEY NONCLUSTERED (U_Id)
    

    And then create the clustered index with:

    CREATE CLUSTERED INDEX ix_Email ON Users (Email); 
    
    0 讨论(0)
提交回复
热议问题