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列?


基础教程推荐
猜你喜欢
- 与不使用派生表的查询相比,使用派生表的查询 2021-01-01
- 替换存储在 SQL Server 数据库列中的 XML 中的节点名 2021-01-01
- 如何将参数传递给使用 sqlcmd 调用的 SQL Server 脚本? 2021-01-01
- my.cnf 文件在 macOS 上的位置 2021-01-01
- 哪个是 Rails 应用程序的最佳数据库? 2022-01-01
- SSIS:在数据流中使用 System::TaskName 2021-01-01
- 如何在没有密码提示的情况下执行 mysqldump? 2021-01-01
- Sql*plus 总是返回退出代码 0? 2021-01-01
- 为 SQL Server 设置 Maven 依赖项 2021-01-01
- (+) 在 Oracle SQL 中有什么作用? 2021-01-01