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

y1aodyip  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(699)

在mysql中,create table时,mysql有没有办法隐式地创建一列(即create table命令中没有显式声明的列)作为表的主键?
谢谢。

i2byvkas

i2byvkas1#

不,需要在表上定义主键。
您可能会想到这一点,这适用于innodb引擎:
如果表没有主键或合适的唯一索引,innodb会在包含行id值的合成列上内部生成一个名为gen\u clust\u index的隐藏聚集索引。这些行是按innodb分配给这样一个表中的行的id排序的。行id是一个6字节的字段,随着新行的插入而单调增加。因此,按行id排序的行在物理上是按插入顺序排列的。
下面的示例显示了为没有主键和唯一列的表创建索引的过程。


# 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     |
+------+-------------+

相反,指定了主键的表会创建一个名为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'

相关问题