一组行的AUTO_INCREMENT列?

auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
本文介绍了一组行的AUTO_INCREMENT列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试如何制作一个有3列的表:

unique_id, type, version

其中UNIQUE_ID是每条记录的AUTO_INCREMENT,版本是每种类型的AUTO_INCREMENT。

目的是,当我插入时,我只需指定‘type’,系统会自动生成唯一标识和版本标识。例如:

insert type 'a', then values are:  1 , a , 1
insert type 'a', then values are:  2 , a , 2
insert type 'a', then values are:  3 , a , 3
insert type 'b', then values are:  4 , b , 1
insert type 'b', then values are:  5 , b , 2
insert type 'a', then values are:  6 , a , 4

还可以说这样的设置没有真正正常化吗?而应该是两张表?

推荐答案

不能为每种类型自动生成序列,但可以生成您自己的序列。

insert into the_table (type, version)
select 'a', 1 + IFNULL(max(version), 0)
  from the_table
  where type = 'a';

这篇关于一组行的AUTO_INCREMENT列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)
SnowFlake Diagram and Many to Many relationship(雪花图与多对多关系)