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

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

    0 讨论(0)
  • A key (declaration) is a constraint. It prevents rows from being entered into the table if any key data is missing, or if the key data duplicates a row that's already in the table.

    An index is a data structure that allows rapid lookup of a row or rows given values for the columns (fields) used in the index. Indexes can be used to speed up certain other kinds of queries as well. For example, a merge join makes use of indexes on the two columns that make up the join condition, if those indexes are both there. Whether your DBMS will do a merge join depends on the query optimizer, the size of the tables, and the presence of the needed indexes.

    To confuse the issue a little, some of the literature refers to the columns that an index uses as "index keys". The same literature usually refers to primary keys and foreign keys as "logical keys". That same literature will often refer to keys as logical features of a table, while indexes are called physical features of the table.

    Most DBMSes will create an index for you when you declare a primary key. There are two reasons for this behavior. The first is that detecting duplicates without an index takes a long time on a big table. The second is that you will presumably be doing lots of lookups based on the primary key, and those lookups run much faster with an index.

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

    An field which has unique values is, essentially, a key. However, a key is used to uniquely identify a row in a table, while an index is used to sort, or group, the rows in the table. A key should not change once it has been initially set, as it might be referenced to elsewhere in your database. An indexed field, however, can change freely.

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

    Keys are used to maintain data integrity, indexes are used to maintain database performance. Think about whatever problem you're trying to solve and that will lead you in the right direction.

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

    Unique is a constraint forced on your data, whereas an index will help in ordering your rows physically (clustered index) or logically (non-clustered index) in the database for faster retrieval.

    0 讨论(0)
  • A key is a field or set of fields that uniquely identifies each row. Typically, there will be one key that is designated as the primary key, and nowadays this seems to be one field that is frequently meaningless by itself (much like a page number, which is important only in identifying what's on the page).

    It's possible to have a table with a key for another table, which is called a foreign key. For example, in an online store's database, there may be a table for orders, and each order will have an associated customer, so each row in the orders table has a foreign key for the customer table.

    An index is a data structure based on one or more fields in a table, which allows rapid access to the table based on those fields. The index may identify individual rows (for example, an index of the customer email address), or may indicate groups (for example, tax-exempt status of customers). It is used much like the index of a book, or like a reverse directory (such as a phone book ordered by telephone number).

    A key is mostly a way to think about the database (although it's usually possible to tell the database about keys, so the database can reject rows with duplicate keys, for example). An index is a way to make the database work faster.

    0 讨论(0)
提交回复
热议问题