mysql非主键自增长用法实例分析

2022-10-07,,,,

本文实例讲述了mysql非主键增长用法。分享给大家供大家参考,具体如下:

mysql并非只有主键才能自增长,而是设为键的列就可以设置自增长。   如下:

create table t1 (
  id int,
  col1 int auto_increment not null
);

结果如下:

    

如果把col1列设为键,就可以创建自增。

create table t1 (
  id int,
  col1 int auto_increment not null,
  key(col1)
);

结果如下:

 

如果我们把id设为主键,仍然可以创建成功。

create table t2 (
  id int primary key,
  col1 int auto_increment not null,
  key(col1)
);

结果如下:

 

所以自增列必须是键,但不一定非是主键。但一张表能否有多个自增列?

答:一张表只能有一个自增列。

create table t3 (
  id int primary key auto_increment,
  col1 int auto_increment not null,
  key(col1)
);

结果如下:

《mysql非主键自增长用法实例分析.doc》

下载本文的Word格式文档,以方便收藏与打印。