mysql

    1. sql自增

      id int(11) not null auto_increment,primary key (id)

    2. mybatis model配置,让数据库生成id

      @GeneratedValue(generator = "JDBC")

    3. 查看下一个自增值

    • 方式1:无法做筛选

      show table status where name='表名'

    • 方式2:

      select auto_increment from information_schema.tables where table_schema='数据库名' and table_name='表名'

    postgresql

    • 方式1:建表时自动建序列
    1. 建表

      "id" serial not null

    2. 设为主键

      alter table 表名 add primary key ("id");

    3. 获取id当前自增值

      select currval('表名idseq')

    4. 获取id下一个自增值

      select nextval('表名idseq')

    • 方式2:先建序列在建表
    1. 建序列

      create sequence 序列名 increment by 1 minvalue 1 no maxvalue start with 1;

    2. 建表

      "id" int4 default nextval('序列名'::regclass) not null

    3. 设为主键

      alter table 表名 add primary key ("id");

    • mybatis model配置,让数据库生成id

      @GeneratedValue(generator = "JDBC")

    oracle

    1. 建序列

      create sequence 序列名 increment by 1 minvalue 1 no maxvalue start with 1;

    2. insert时id项的值为:

      序列名.nextval

      select 序列名.nextval from dual
      dual是虚拟表

    3. mybatis model配置,获取id值再进行insert操作

      @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "select 序列名.nextval from dual")

    4. 查看所有序列

      select * from user_sequences;