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列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:一组行的AUTO_INCREMENT列?


猜你喜欢
- MySql 错误 150 - 外键 2021-01-01
- MySQL(Windows10)使用 MyISAM 表进行 FULLTEXT 搜索不起作用 2022-01-01
- 在 Oracle 中创建 CTE 2022-01-01
- HEROKU - 无法运行 git push heroku master 2021-01-01
- 如何在oracle中获取字符串最右边的10个位置 2021-01-01
- 创建索引时,具有 mysql 数据库迁移的实体框架失败 2022-01-01
- 如何将uuid存储为数字? 2021-01-01
- Oracle SQL 转置 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL Server 将 Varchar 转换为日期时间 2021-01-01