In a database, what is the difference betwen a key and an index?

前端 未结 10 1626
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 00:59

For example, in SQLServer, if I have an index with \'unique\' set on it, how is that different from a key?

How do I know when I want to use a Key vs. an Indexed field?

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-08 01:22

    A key will always have an index behind the scene If you create a Primary Key, SQL Server will create a clustered unique index to support that

    Even when you create a unique constraint SQL Server will also create an index

    And of course the difference between a unique constraint and a primary key is that the unique constraint allows at least 1 NULL value (1 on sql server more than one on Oracle) You can only have 1 primary key and many (249 on sql server) unique constraints on a table

提交回复
热议问题