MySQL 索引中的 'name' 和 'symbol' 是什么?
What are 'name' and 'symbol' in MySQL indexes?
主键定义可以有一个可选的符号值,而唯一键可以同时有符号和名称。这些是什么,它们的区别是什么?使用它们的最佳做法是什么?
symbol
是约束的名称。如果您以后需要删除、禁用或重新启用约束,您可以使用此符号向系统标识它。
index_name
是使用 CREATE INDEX
创建的索引的名称,用于强制执行 UNIQUE
约束。 (索引是一个数据库对象,它与索引的 table 是分开的。)如果您 而不是 指定现有索引的名称,系统将为你,如果你稍后禁用约束,索引将自动删除,并且必须在重新启用约束时重新创建。如果您 do 指定现有索引,它将 not 被删除并使用约束重新创建,从而节省大量时间和处理能力。其他一些平台还允许您指定现有索引以强制执行主键。
希望对您有所帮助。
达尔文为您提供了完美的答案。我将添加示例来演示符号以及如何使用它。我建议在唯一键和主键上都使用符号名称。在索引上使用索引名称。更多精彩。
文档
http://dev.mysql.com/doc/refman/5.5/en/create-table.html 显示:
create_definition:
col_name column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
约束
注意 PRIMARY 和 UNIQUE 键是约束,而常规 index/key 没有关键字 CONSTRAINT
.
符号
那么什么是符号呢?它只是给主键,唯一键,索引等的名称。举个例子:
create table test1 (
id int not null auto_increment,
national_id varchar(50) not null,
firstname varchar(50) not null,
lastname varchar(50) not null,
age int not null,
constraint pk_test1_id primary key (id),
constraint uq_test1_national_id unique key (national_id),
index idx_test1_age (age)
);
主键的 symbol/name 是 pk_test1_id
。唯一键的 symbol/name 是 uq_test1_national_id
索引的 symbol/name 是 idx_test1_age
.
** 我在哪里可以看到交易品种或指数名称?**
show index from test1;
| Table | Non_unique | Key_name | ...
| test1 | 0 | PRIMARY ...
| test1 | 0 | uq_test1_national_id ...
| test1 | 1 | idx_test1_age ...
显示索引时,可以看到non_unique列表示键是唯一的还是非唯一的。唯一键的列可以包含空值并且不能包含重复信息。主键列不能包含空值,也不能包含重复信息。 Key_name 显示 PRIMARY 而不是符号。
如果需要,我在哪里可以看到所有 table 的密钥?
select column_name, column_key
from information_schema.`COLUMNS`
where table_name = 'test1'
| column_name | column_key |
| id | PRI |
| national_id | UNI |
...
| age | MUL |
注意 information_schema.columns 也显示了类似的信息,但没有描述密钥的名称。
使用交易品种和指数名称下单
在唯一键中,如果给出了符号和索引名称,则应使用索引名称来删除唯一键。
删除索引和查看 table 的元数据时,符号很有用。明智地选择约束或索引 symbol/name 对显示 show create table test1
或 show index from test1
等时的 reader 非常有帮助
-- drop unique key using its symbol
alter table test1 drop index uq_test1_national_id;
-- drop the primary key; since primary key was on auto_increment and that
-- auto_increment should be a key, I am forcing an index on id before
-- removing primary key
create index idx_test1_id on test1 (id);
alter table test1 drop primary key;
-- drop index using its symbol/name
alter table test1 drop index idx_test1_age;
符号名称一致;所以我推荐
另请注意,如果在创建 table 期间,我们指定了:
constraint uq_test1_national_id unique key uq_my_own_name (national_id),
那么show index from test1
就会使用uq_my_own_name。 alter table ... drop index
还需要 uq_my_own_name
。因此,对于唯一键,我建议只使用符号而不是 index_name.
主键定义可以有一个可选的符号值,而唯一键可以同时有符号和名称。这些是什么,它们的区别是什么?使用它们的最佳做法是什么?
symbol
是约束的名称。如果您以后需要删除、禁用或重新启用约束,您可以使用此符号向系统标识它。
index_name
是使用 CREATE INDEX
创建的索引的名称,用于强制执行 UNIQUE
约束。 (索引是一个数据库对象,它与索引的 table 是分开的。)如果您 而不是 指定现有索引的名称,系统将为你,如果你稍后禁用约束,索引将自动删除,并且必须在重新启用约束时重新创建。如果您 do 指定现有索引,它将 not 被删除并使用约束重新创建,从而节省大量时间和处理能力。其他一些平台还允许您指定现有索引以强制执行主键。
希望对您有所帮助。
达尔文为您提供了完美的答案。我将添加示例来演示符号以及如何使用它。我建议在唯一键和主键上都使用符号名称。在索引上使用索引名称。更多精彩。
文档
http://dev.mysql.com/doc/refman/5.5/en/create-table.html 显示:
create_definition:
col_name column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
约束
注意 PRIMARY 和 UNIQUE 键是约束,而常规 index/key 没有关键字 CONSTRAINT
.
符号
那么什么是符号呢?它只是给主键,唯一键,索引等的名称。举个例子:
create table test1 (
id int not null auto_increment,
national_id varchar(50) not null,
firstname varchar(50) not null,
lastname varchar(50) not null,
age int not null,
constraint pk_test1_id primary key (id),
constraint uq_test1_national_id unique key (national_id),
index idx_test1_age (age)
);
主键的 symbol/name 是 pk_test1_id
。唯一键的 symbol/name 是 uq_test1_national_id
索引的 symbol/name 是 idx_test1_age
.
** 我在哪里可以看到交易品种或指数名称?**
show index from test1;
| Table | Non_unique | Key_name | ...
| test1 | 0 | PRIMARY ...
| test1 | 0 | uq_test1_national_id ...
| test1 | 1 | idx_test1_age ...
显示索引时,可以看到non_unique列表示键是唯一的还是非唯一的。唯一键的列可以包含空值并且不能包含重复信息。主键列不能包含空值,也不能包含重复信息。 Key_name 显示 PRIMARY 而不是符号。
如果需要,我在哪里可以看到所有 table 的密钥?
select column_name, column_key
from information_schema.`COLUMNS`
where table_name = 'test1'
| column_name | column_key |
| id | PRI |
| national_id | UNI |
...
| age | MUL |
注意 information_schema.columns 也显示了类似的信息,但没有描述密钥的名称。
使用交易品种和指数名称下单
在唯一键中,如果给出了符号和索引名称,则应使用索引名称来删除唯一键。
删除索引和查看 table 的元数据时,符号很有用。明智地选择约束或索引 symbol/name 对显示 show create table test1
或 show index from test1
等时的 reader 非常有帮助
-- drop unique key using its symbol
alter table test1 drop index uq_test1_national_id;
-- drop the primary key; since primary key was on auto_increment and that
-- auto_increment should be a key, I am forcing an index on id before
-- removing primary key
create index idx_test1_id on test1 (id);
alter table test1 drop primary key;
-- drop index using its symbol/name
alter table test1 drop index idx_test1_age;
符号名称一致;所以我推荐
另请注意,如果在创建 table 期间,我们指定了:
constraint uq_test1_national_id unique key uq_my_own_name (national_id),
那么show index from test1
就会使用uq_my_own_name。 alter table ... drop index
还需要 uq_my_own_name
。因此,对于唯一键,我建议只使用符号而不是 index_name.