简体   繁体   English

MySQL中有没有一种方法可以隐式地为表创建主键?

[英]Is there a way in MySQL to implicitly create a primary key for a table?

In MySQL, when CREATE TABLE, is there a way for MySQL to implicitly create a column (ie a column not explicitly declared in CREATE TABLE command) as the primary key of the table? 在MySQL中,当使用CREATE TABLE时,MySQL是否可以隐式创建一列(即,在CREATE TABLE命令中未明确声明的列)作为表的主键?

Thanks. 谢谢。

No, the PRIMARY KEY needs to be defined on the table. 不,需要在表上定义PRIMARY KEY。

You may be thinking about this , which applies to InnoDB engine: 您可能正在考虑适用于InnoDB引擎的this

If the table has no PRIMARY KEY or suitable UNIQUE index, InnoDB internally generates a hidden clustered index named GEN_CLUST_INDEX on a synthetic column containing row ID values. 如果表没有PRIMARY KEY或合适的UNIQUE索引,则InnoDB在包含行ID值的合成列上内部生成一个名为GEN_CLUST_INDEX的隐藏的聚集索引。 The rows are ordered by the ID that InnoDB assigns to the rows in such a table. 这些行由InnoDB分配给此类表中的行的ID排序。 The row ID is a 6-byte field that increases monotonically as new rows are inserted. 行ID是一个6字节的字段,随着插入新行而单调增加。 Thus, the rows ordered by the row ID are physically in insertion order. 因此,按行ID排序的行实际上在插入顺序上。

Below is an example that shows this index's creation for a table with no PRIMARY KEY and no UNIQUE column. 下面的示例显示为没有PRIMARY KEY和UNIQUE列的表创建索引的示例。

# Create the table
create table test.check_table (id int, description varchar(10)) ENGINE = INNODB;

# Verify that there is no primary or unique column
desc test.check_table;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | int(11)     | YES  |     | NULL    |       |
| description | varchar(10) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+


# Insert some values
insert into test.check_table values(1, 'value-1');
insert into test.check_table values(2, 'value-2');
insert into test.check_table values(null, 'value-3');
insert into test.check_table values(4, null);
insert into test.check_table values(1, 'value-1');


# Verify table
select * from test.check_table;
+------+-------------+
| id   | description |
+------+-------------+
|    1 | value-1     |
|    2 | value-2     |
| NULL | value-3     |
|    4 | NULL        |
|    1 | value-1     |
+------+-------------+


# Verify that the GEN_CLUST_INDEX index is auto-created.
select * from INFORMATION_SCHEMA.INNODB_INDEX_STATS where TABLE_SCHEMA='test' and TABLE_NAME = 'check_table';
+--------------+-------------+-----------------+--------+--------------+-------------------+------------------+
| table_schema | table_name  | index_name      | fields | rows_per_key | index_total_pages | index_leaf_pages |
+--------------+-------------+-----------------+--------+--------------+-------------------+------------------+
| test         | check_table | GEN_CLUST_INDEX |      1 | 5            |                 1 |                1 |
+--------------+-------------+-----------------+--------+--------------+-------------------+------------------+

# Duplicate rows are still allowed (Primary Key constraints not enforced)

insert into test.check_table values(1, 'value-1');

select * from test.check_table;
+------+-------------+
| id   | description |
+------+-------------+
|    1 | value-1     |
|    2 | value-2     |
| NULL | value-3     |
|    4 | NULL        |
|    1 | value-1     |
|    5 | value-5     |
|    1 | value-1     |
+------+-------------+

To contrast, a table with a PRIMARY KEY specified creates an index with name PRIMARY. 相比之下,指定了PRIMARY KEY的表将创建名称为PRIMARY的索引。

# Create another table
create table test.check_table_2 (id int, description varchar(10), PRIMARY KEY(id)) ENGINE = INNODB;

# Verify primary key column
desc check_table_2;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | int(11)     | NO   | PRI | 0       |       |
| description | varchar(10) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+

# Verify index
select * from INFORMATION_SCHEMA.INNODB_INDEX_STATS where TABLE_SCHEMA='test' and TABLE_NAME = 'check_table_2';
+--------------+---------------+------------+--------+--------------+-------------------+------------------+
| table_schema | table_name    | index_name | fields | rows_per_key | index_total_pages | index_leaf_pages |
+--------------+---------------+------------+--------+--------------+-------------------+------------------+
| test         | check_table_2 | PRIMARY    |      1 | 0            |                 1 |                1 |
+--------------+---------------+------------+--------+--------------+-------------------+------------------+

# Primary key is enforced
insert into check_table_2 values(1,'value-1');
OK

insert into check_table_2 values(1,'value-1');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM