mysql中一个表最多能有几个auto_mysql--一个表上可以指定几个auto_increment

auto_increment测试:

一个表是否可以有多个列使用auto_increment?

只有一个列使用auto_increment:

mysql> create table tab_auto_incr(a int not null auto_increment,b int not null,primary key (a));

Query OK, 0 rows affected (0.17 sec)

mysql> drop table tab_auto_incr;

Query OK, 0 rows affected (0.03 sec)

没问题,在b字段做动作

mysql> create table tab_auto_incr(a int not null,b int not null auto_increment,unique key (b));

Query OK, 0 rows affected (0.16 sec)

mysql> drop table tab_auto_incr;

Query OK, 0 rows affected (0.04 sec)

如果a是主键,b是唯一键,那么

mysql> create table tab_auto_incr(a int not null,b int not null,primary key (a),unique key (b));

Query OK, 0 rows affected (0.23 sec)

mysql> drop table tab_auto_incr;

Query OK, 0 rows affected (0.03 sec)

也没有问题,那么如果我在一个表上两个字段指定auto_increment呢?

mysql> create table tab_auto_incr(a int not null auto_increment,b int not null auto_increment,primary key (a),unique key (b));

ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

不行!说明一个表只能有一个auto_increment,且该列必须是primary key或者unique key。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值