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

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

    A key should be the business model's unique identifier for the row. A uniquely indexed column could be considered an "alternate key." Generally SQL servers will create a unique index for primary keys automatically.

    I should also mention that you can index non-unique columns; indices such as these are used for speeding up queries over the indexed columns.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-08 01:39

    A key uniquely identifies a row in a table. An index is the order of rows based a field in a table. A table can have multiple indexes, because an index can just be a certain order of a set fields the system uses to search on and then looks up the actual row. Technically, there can only be one key the system uses to identify a row. Most of the time, this is also the primary index, but it doesn't have to be.

    0 讨论(0)
  • 2021-02-08 01:39

    Key has by default a cluster index in MS SQL Server.

    Key is unique value for each row.

    Index use to maximize your database performance. You can create indexes according to your requirement. there are different kind of indexes e.g.

    • Bitmap index
    • Dense index
    • Sparse index
    • Covering Index
    0 讨论(0)
提交回复
热议问题