• mysql auto_increment 不同引擎区别


        最近为了性能问题,有时候用myisam引擎建表,发现在auto_increment上面两者区别还是有点大,下面说明一下。

    1. InnoDB:

       表里面只能有一个列是auto_increment 并且必须是主键,如果用下面的语句建表会报错:   

    create table dd(grp varchar(10) not null, id int not null auto_increment, primary key(grp,id)) engine=innodb;

       下面的是OK的,因为id是主键。

    create table dd(grp varchar(10) not null, id int not null auto_increment, primary key(id)) engine=innodb;

       下面是个测试例子:

    sample
    drop table if exists dd;
    create table dd(grp varchar(10) not null, id int not null auto_increment, primary key(id)) engine=innodb;
    insert into dd values('a',null),('a',null),('b',null),('b',null),('b',null);
    select * from dd;

    2. MyISAM:
       相对InnoDB比较宽松,如果主键是多列,某一列可以是auto_increment,在插入新列时自增列的计算方式是以主键的其他列的值分组,然后自增列在同组内递增,用公式说话就是newValue= (max(value) in the same group) + 1;

       下面是个例子:   

    sample
    drop table if exists dd;
    create table dd(grp varchar(10) not null, id int not null auto_increment, primary key(grp,id)) engine=myisam;
    insert into dd values('a',null),('a',null),('b',null),('b',null),('b',null);
    select * from dd;


     



     

  • 相关阅读:
    设计模式入门
    Spring Boot 日志
    Spring Boot入门
    Vue--过滤器、指令、插件
    CentOS7更换yum源
    CentOS7中修改运行级别
    Xshell进行远程登录
    Linux的目录结构详情
    通过VMware Tools配置Centos7与本地主机的共享文件夹(亲测)
    eclipse中的Git操作
  • 原文地址:https://www.cnblogs.com/alala666888/p/2404110.html
Copyright © 2020-2023  润新知