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

前端 未结 10 1635
伪装坚强ぢ
伪装坚强ぢ 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:39

    A key identifies the row stored in the database. An index is a structure like the one at the one at the end of a book. At the end of a book you see several pages with words and where you can find those words. Those pages are an index and the same is the case for a database. The index contains key and their locations. It helps you finding the location of rows. In case of a book the index tells you on which page you can find the word. The database index has the same function.

    As many mention the index is implemented with b-trees. However this is only an implementation detail. There are many ways to implement an index.

提交回复
热议问题