MySQL中INDEX、PRIMARY、UNIQUE、FULLTEXT之间的区别?

2022-01-30 00:00:00 indexing key schema mysql

在创建 MySQL 表时,PRIMARY、UNIQUE、INDEX 和 FULLTEXT 有什么区别?

What are the differences between PRIMARY, UNIQUE, INDEX and FULLTEXT when creating MySQL tables?

我将如何使用它们?

推荐答案

区别

  • KEY 或 INDEX 指的是普通的非唯一索引.允许索引的非不同值,因此索引可能包含在索引的所有列中具有相同值的行.这些索引不会对您的数据施加任何限制,因此它们仅用于访问 - 用于快速到达特定范围的记录,而无需扫描所有记录.

    Differences

    • KEY or INDEX refers to a normal non-unique index. Non-distinct values for the index are allowed, so the index may contain rows with identical values in all columns of the index. These indexes don't enforce any restraints on your data so they are used only for access - for quickly reaching certain ranges of records without scanning all records.

      UNIQUE 是指索引的所有行都必须唯一的索引.也就是说,对于该索引中的所有列,同一行可能不会与另一行具有相同的非 NULL 值.除了用于快速达到某些记录范围外,UNIQUE 索引还可用于对数据实施约束,因为数据库系统不允许在插入或更新数据时破坏不同值规则.

      UNIQUE refers to an index where all rows of the index must be unique. That is, the same row may not have identical non-NULL values for all columns in this index as another row. As well as being used to quickly reach certain record ranges, UNIQUE indexes can be used to enforce restraints on data, because the database system does not allow the distinct values rule to be broken when inserting or updating data.

      您的数据库系统可能允许将 UNIQUE 索引应用于允许 NULL 值的列,在这种情况下,如果两行都包含 NULL 值,则允许它们相同(这里的基本原理是 NULL 被认为不等于本身).但是,根据您的应用程序,您可能会发现这种情况不受欢迎:如果您希望防止这种情况发生,您应该在相关列中禁止 NULL 值.

      Your database system may allow a UNIQUE index to be applied to columns which allow NULL values, in which case two rows are allowed to be identical if they both contain a NULL value (the rationale here is that NULL is considered not equal to itself). Depending on your application, however, you may find this undesirable: if you wish to prevent this, you should disallow NULL values in the relevant columns.

      PRIMARY 的作用与 UNIQUE 索引完全相同,只是它始终命名为PRIMARY",并且表上可能只有一个(并且应该 始终为一;尽管某些数据库系统不强制执行此操作).PRIMARY 索引旨在作为唯一标识表中任何行的主要手段,因此与 UNIQUE 不同,它不应用于任何允许 NULL 值的列.您的 PRIMARY 索引应位于足以唯一标识行的最少列数上.通常,这只是包含唯一自动递增数字的一列,但如果有其他任何东西可以唯一标识一行,例如countrycode",则为countrycode".在国家/地区列表中,您可以改用它.

      PRIMARY acts exactly like a UNIQUE index, except that it is always named 'PRIMARY', and there may be only one on a table (and there should always be one; though some database systems don't enforce this). A PRIMARY index is intended as a primary means to uniquely identify any row in the table, so unlike UNIQUE it should not be used on any columns which allow NULL values. Your PRIMARY index should be on the smallest number of columns that are sufficient to uniquely identify a row. Often, this is just one column containing a unique auto-incremented number, but if there is anything else that can uniquely identify a row, such as "countrycode" in a list of countries, you can use that instead.

      一些数据库系统(例如 MySQL 的 InnoDB)会按照它们在 PRIMARY 索引中出现的顺序将表的记录存储在磁盘上.

      Some database systems (such as MySQL's InnoDB) will store a table's records on disk in the order in which they appear in the PRIMARY index.

      FULLTEXT 索引与上述所有索引都不同,它们的行为在数据库系统之间也存在显着差异.FULLTEXT 索引仅对使用 MATCH()/AGAINST() 子句完成的全文搜索有用,这与上述三个不同 - 通常在内部使用 b-trees 实现(允许从最左边的列开始选择、排序或范围)或哈希表(允许从最左边的列开始选择).

      FULLTEXT indexes are different from all of the above, and their behaviour differs significantly between database systems. FULLTEXT indexes are only useful for full text searches done with the MATCH() / AGAINST() clause, unlike the above three - which are typically implemented internally using b-trees (allowing for selecting, sorting or ranges starting from left most column) or hash tables (allowing for selection starting from left most column).

      如果其他索引类型是通用的,那么 FULLTEXT 索引是专门的,因为它的用途很窄:它只用于全文搜索".功能.

      Where the other index types are general-purpose, a FULLTEXT index is specialised, in that it serves a narrow purpose: it's only used for a "full text search" feature.

      • 所有这些索引中可能包含多于一列.

      • All of these indexes may have more than one column in them.

      除了 FULLTEXT,列顺序很重要:要使索引在查询中有用,查询必须使用索引中从左侧开始的列 - 它不能只使用第二个,索引的第三或第四部分,除非它还使用索引中的先前列来匹配静态值.(要使 FULLTEXT 索引对查询有用,查询必须使用索引的 所有 列.)

      With the exception of FULLTEXT, the column order is significant: for the index to be useful in a query, the query must use columns from the index starting from the left - it can't use just the second, third or fourth part of an index, unless it is also using the previous columns in the index to match static values. (For a FULLTEXT index to be useful to a query, the query must use all columns of the index.)

相关文章