AUTO_INCREMENT on a secondary column in a multiple-column index

MySQL中可以通过AUTO_INCREMENT产生自增长序列。
但是在MyISAM和InnoDB存储引擎中使用有点区别。
在MySQL 5.5.8中测试(默认存储引擎InnoDB),例如:

mysql> create table tbl_test (id int not null auto_increment,firstname varchar(32),lastname varchar(32),primary key (firstname,id));
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

在InnoDB中AUTO_INCREMENT可以看作只有一个链。因此建表时报错, 看了MyISAM的举例就很容易理解.

mysql> create table tbl_test (id int not null auto_increment,firstname varchar(32),lastname varchar(32),primary key (firstname,id)) engine=MyISAM;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into tbl_test(firstname,lastname) values('zhou','digoal'),('Zhou','digoal'),('zhou','Digoal');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tbl_test;
+----+-----------+----------+
| id | firstname | lastname |
+----+-----------+----------+
|  1 | zhou      | digoal   |
|  1 | Zhou      | digoal   |
|  2 | zhou      | Digoal   |
+----+-----------+----------+
3 rows in set (0.00 sec)

# 从结果来看 AUTO_INCREMENT 根据firstname的值 , 被拆成了多条链 ( 或者说有几个firstname的值就有几个链 ) 。
# 换个例子可能更好说明问题:

mysql> create table tbl_test1 (id int not null auto_increment,firstname varchar(32),lastname varchar(32),primary key (firstname,lastname,id)) engine=MyISAM;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into tbl_test1(firstname,lastname) values('zhou','digoal'),('Zhou','digoal'),('zhou','Digoal');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into tbl_test1(firstname,lastname) values('zhou','digoal'),('Zhou','digoal'),('zhou','Digoal');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into tbl_test1(firstname,lastname) values('zhou','digoal'),('Zhou','digoal'),('zhou','Digoal');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into tbl_test1(firstname,lastname) values('zhou','digoal'),('Zhou','digoal'),('zhou','Digoal');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tbl_test1;
+----+-----------+----------+
| id | firstname | lastname |
+----+-----------+----------+
|  1 | Zhou      | digoal   |
|  2 | Zhou      | digoal   |
|  3 | Zhou      | digoal   |
|  4 | Zhou      | digoal   |
|  1 | zhou      | Digoal   |
|  2 | zhou      | Digoal   |
|  3 | zhou      | Digoal   |
|  4 | zhou      | Digoal   |
|  1 | zhou      | digoal   |
|  2 | zhou      | digoal   |
|  3 | zhou      | digoal   |
|  4 | zhou      | digoal   |
+----+-----------+----------+
12 rows in set (0.00 sec)

# AUTO_INCREMENT 根据firstname,lastname的值 , 被拆成了多条链 ( 或者说有几个firstname,lastname的值组合就有几个链 ) 。

# 来看看把tbl_test的引擎转换为InnoDB会怎么样?

mysql> show table status like 'tbl_test' \G
*************************** 1. row ***************************
           Name: tbl_test
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 9
 Avg_row_length: 24
    Data_length: 216
Max_data_length: 281474976710655
   Index_length: 3072
      Data_free: 0
 Auto_increment: 9
    Create_time: 2011-01-17 15:52:50
    Update_time: 2011-01-17 15:52:54
     Check_time: NULL
      Collation: utf8_bin
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

转换报错 :

mysql> alter table tbl_test engine=InnoDB;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

# 新建一个KEY就可以转换为InnoDB引擎.

mysql> create index idx_id on tbl_test(id);
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> alter table tbl_test engine=innodb;
Query OK, 9 rows affected (0.08 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> show table status like 'tbl_test' \G
*************************** 1. row ***************************
           Name: tbl_test
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 9
 Avg_row_length: 1820
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 10485760
 Auto_increment: 9
    Create_time: 2011-01-17 15:55:48
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_bin
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

mysql> describe tbl_test
    -> ;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(32) | NO   | PRI |         |                |
| lastname  | varchar(32) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> show create table tbl_test;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                             |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tbl_test | CREATE TABLE `tbl_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(32) COLLATE utf8_bin NOT NULL DEFAULT '',
  `lastname` varchar(32) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`firstname`,`id`),
  KEY `idx_id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> insert into tbl_test(firstname,lastname) values('zhou','digoal'),('Zhou','digoal'),('zhou','Digoal');
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tbl_test;
+----+-----------+----------+
| id | firstname | lastname |
+----+-----------+----------+
|  1 | Zhou      | digoal   |
|  4 | Zhou      | digoal   |
|  7 | Zhou      | digoal   |
| 10 | Zhou      | digoal   |
|  1 | zhou      | digoal   |
|  2 | zhou      | Digoal   |
|  3 | zhou      | digoal   |
|  5 | zhou      | Digoal   |
|  6 | zhou      | digoal   |
|  8 | zhou      | Digoal   |
|  9 | zhou      | digoal   |
| 11 | zhou      | Digoal   |
+----+-----------+----------+
12 rows in set (0.00 sec)

# 转换为 InnoDB之后,AUTO_INCREMENT 变成单链自增。

时间: 2024-10-18 22:04:22

AUTO_INCREMENT on a secondary column in a multiple-column index的相关文章

HBase在单Column和多Column情况下批量Put的性能对比分析

针对HBase在单column family单column qualifier和单column family多column qualifier两种场景下,分别批量Put写入时的性能对比情况,下面是结合HBase的源码来简单分析解释这一现象. 1. 测试结果 在客户端批量写入时,单列族单列模式和单列族多列模式的TPS和RPC次数相差很大,以客户端10个线程,开启WAL的两种模式下的测试数据为例, 单列族单列模式下TPS能够达到12403.87,实际RPC次数为53次: 单列族多列模式下,TPS只有

抛砖引玉——实现LISTVIEW点击COLUMN排序的代码,可实现按时间,字符,数字排序(控件部分)

using System;using System.ComponentModel;using System.Collections;using System.Diagnostics;using System.Windows.Forms;using System.Drawing; namespace ListViewControl{    /// <summary>    /// Summary description for UserControl1.    /// </summary&

Cassandra数据库中的column family

Cassandra中的column family,相当于关系数据库中的表,它用于存储行和字段. column family 中column数量是不固定的 在关系数据库中,每一行包含的字段个数是相同的.但是在Cassandra中,虽然column family可以定义column的元数据(metadata), 但是每一行的实际的字段数是由客户端程序决定的,所以没必要所有的字段数目都一样. column family的分类 静态column family(static column family)

[LeetCode]171.Excel Sheet Column Number

[题目] Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 [分析] 可以看成是一道26进制转换为10进制的题目. 跟 [Le

PostgreSQL 10 新特性 - identity column (serial, 自增)

标签 PostgreSQL , 10 , 特性 , identify , 自增 , 覆盖 , SQL Server IDENTITY兼容 , SQL标准 背景 自增列是数据库的一个常用功能,PostgreSQL的自增列在10的版本出来前,有两种非常简单的方法来实现: 1.serial类型,自动创建一个序列,同时将列设置为INT,默认值设置为nextval('序列'). create table test(id serial, info text); postgres=# \d+ test Tab

oracle 报错 column ambiguously defined

column ambiguously defined ORA-00918: column ambiguously defined java.sql.SQLException: ORA-00918: column ambiguously definedORA-00918: column ambiguously defined.中文名称:未明确定义的列! 可能定义了两个相同的列 你找找你后面的t里面是不是有两个相同的列,我也是这个问题,就是多了一个相同的列的查询,所以sb了.. 我出错的sql语句是

[LeetCode] Excel Sheet Column Number

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 实现代码: /*******************************

MySQL数据库-错误1166 Incorrect column name_Mysql

出现MySQL数据库-错误1166 Incorrect column name 字段名,这个是字段中有空格了,这个只要利用alert或phpmyadmin进入修改即可解决. 今天在用Navicat for MySQL给MySQL数据库修改表的字段时报如下的错误 SQL 查询: CREATE TABLE `gfan_content`.`channel_into` ( `time` INT( 11 ) NOT NULL COMMENT '产品id', `bid` INT( 11 ) NOT NULL

PostgreSQL update returning NEW|OLD column value 在对账|购票|防纂改|原子操作中的妙用

标签 PostgreSQL , update , returning , NEW , OLD 背景 在数据库中更新记录时,有时为了对账,或者防纂改的目的,需要在更新后立即返回更新前和更新后的值. 例如以set bit为例,假设使用BIT串作为火车的每个位置,每个BIT代表一张票,0表示未售卖,1表示已售卖. 购票时,使用set bit=1的操作,为了确保不出现重复售票的问题,必须确保被set的value以前的值为0,SET后的值为1. 这个动作其实也可以在function中来保证,不过你要扩展F