关于mysql中的key和index
2013-05-22 12:27
211 查看
1. 官方文档:http://dev.mysql.com/doc/refman/5.5/en/create-table.html
The key attribute
be specified as just
a column definition. This was implemented for compatibility with other database systems.
A
that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a
permits multiple
that can contain
A
always
as the name for any other kind of index.
If you do not have a
application asks for the
your tables, MySQL returns the first
that has no
In
“
In the created table, a
placed first, followed by all
and then the nonunique indexes. This helps the MySQL optimizer to prioritize which index to use and also more quickly to detect duplicated
A
index. However, you cannot create a multiple-column index using the
...) clause.
If a
only one column that has an integer type, you can also refer to the column as
In MySQL, the name of a
For other indexes, if you do not assign a name, the index is assigned the same name as the first indexed column, with an optional suffix (
to make it unique. You can see index names for a table using
See Section 13.7.5.23,
“
2. http://forums.macrumors.com/showthread.php?t=334697
Note that "primary" is called PRIMARY KEY not INDEX.
KEY is something on the logical level, describes your table and database design (i.e. enforces referential integrity
...)
INDEX is something on the physical level, helps improve access time for table operations.
Behind every PK there is (usually) unique index created (automatically).
3. http://wenku.baidu.com/view/ff96febb1a37f111f1855bf8.html
在创建innoDB表时,在每张表中都只能有一个主键,如果在创建表时没有明确地定义主键(Primary Key),则innoDB引擎会自动按照如下方式选择或创建主键:首先看表中是否有非空唯一索引(Unique NOT NULL)如果有,则该列即为主键。 不符合上述条件,则自动创建一个6字节大小的RowID。
KEYis normally a synonym for
INDEX.
The key attribute
PRIMARY KEYcan also
be specified as just
KEYwhen given in
a column definition. This was implemented for compatibility with other database systems.
A
UNIQUEindex creates a constraint such
that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a
UNIQUEindex
permits multiple
NULLvalues for columns
that can contain
NULL.
A
PRIMARY KEYis a unique index where all key columns must be defined as
NOT NULL. If they are not explicitly declared as
NOT NULL, MySQL declares them so implicitly (and silently). A table can have only one
PRIMARY KEY. The name of a
PRIMARY KEYis
always
PRIMARY, which thus cannot be used
as the name for any other kind of index.
If you do not have a
PRIMARY KEYand an
application asks for the
PRIMARY KEYin
your tables, MySQL returns the first
UNIQUEindex
that has no
NULLcolumns as the
PRIMARY KEY.
In
InnoDBtables, keep the
PRIMARY KEYshort to minimize storage overhead for secondary indexes. Each secondary index entry contains a copy of the primary key columns for the corresponding row. (SeeSection 14.3.11,
“
InnoDBTable and Index Structures”.)
In the created table, a
PRIMARY KEYis
placed first, followed by all
UNIQUEindexes,
and then the nonunique indexes. This helps the MySQL optimizer to prioritize which index to use and also more quickly to detect duplicated
UNIQUEkeys.
A
PRIMARY KEYcan be a multiple-column
index. However, you cannot create a multiple-column index using the
PRIMARY KEYkey attribute in a column specification. Doing so only marks that single column as primary. You must use a separate
PRIMARY
KEY(index_col_name
,...) clause.
If a
PRIMARY KEYor
UNIQUEindex consists of
only one column that has an integer type, you can also refer to the column as
_rowidin
SELECTstatements.
In MySQL, the name of a
PRIMARY KEYis
PRIMARY.
For other indexes, if you do not assign a name, the index is assigned the same name as the first indexed column, with an optional suffix (
_2,
_3,
...)
to make it unique. You can see index names for a table using
SHOW
INDEX FROM tbl_name
.See Section 13.7.5.23,
“
SHOW INDEXSyntax”.
2. http://forums.macrumors.com/showthread.php?t=334697
Note that "primary" is called PRIMARY KEY not INDEX.
KEY is something on the logical level, describes your table and database design (i.e. enforces referential integrity
...)
INDEX is something on the physical level, helps improve access time for table operations.
Behind every PK there is (usually) unique index created (automatically).
3. http://wenku.baidu.com/view/ff96febb1a37f111f1855bf8.html
在创建innoDB表时,在每张表中都只能有一个主键,如果在创建表时没有明确地定义主键(Primary Key),则innoDB引擎会自动按照如下方式选择或创建主键:首先看表中是否有非空唯一索引(Unique NOT NULL)如果有,则该列即为主键。 不符合上述条件,则自动创建一个6字节大小的RowID。
相关文章推荐
- mysql中key 、primary key 、unique key 与index区别
- mysql中key 、primary key 、unique key 与index区别
- mysql中key 、primary key 、unique key 与index区别
- mysql index key
- mysql中key 、primary key 、unique key 与index区别
- 关于mysql中[Err] 1451 -Cannot delete or update a parent row: a foreign key constraint fails
- mysql中key 、primary key 、unique key 与index区别
- 一点关于MySQL参数delay_key_write、myisam_recover_options的使用经验
- 【MySQL】关于Repair by keyCache
- MySql key and index
- 关于MySQL字符集问题:Specified key was too long; max key length is 767 bytes
- mysql中key 、primary key 、unique key 与index区别
- 【mysql】关于Index Condition Pushdown特性
- mysql中key 、primary key 、unique key 与index区别
- Mysql索引详解及优化(key和index区别)
- 转-MySQL中Index与Key的区别
- mysql的key和index
- mysql index and key
- MySQL中index和key的关系
- 关于"xampp中mysql中配置问题:Unknown initial character set index '48' received from server错误解决方法"