How do I create contextual AUTO_INCREMENT in MySql?(如何在 MySql 中创建上下文 AUTO_INCREMENT?)
问题描述
我想创建一个上下文增量,不确定我使用的术语是否正确.这是我的桌子
I want to Create a Contextual increment, not sure of term I am using is correct. here is my table
CREATE TABLE IF NOT EXISTS `test` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`abc` varchar(50) NOT NULL,
`data` varchar(100) NOT NULL,
PRIMARY KEY (`id`,`abc`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
在这之后我做了
INSERT INTO `test` (`abc`, `data`) VALUES
('hi', 'hello2'),
('hi2', 'hello2'),
('hi', 'hello2');
MyISAM 注释 他们给出了一个使用枚举的例子,这里我没有使用枚举,但他们没有提到它只适用于枚举.但我得到的输出是
No in example given on page MyISAM Notes they have given an example, which is using enum, here I am not using enum, but they have not mentioned that it works for enum only. but the output i get is
1 hi hello2
2 hi2 hello2
3 hi hello2
但我想要这样
1 hi hello2
1 hi2 hello2
3 hi hello2
谁能告诉我我在这里做错了什么?
can anybody tell me what i am doing wrong here?
推荐答案
你必须交换索引的顺序:
You've got to swap the order of your indexes:
CREATE TABLE IF NOT EXISTS `test` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`abc` varchar(50) NOT NULL,
`data` varchar(100) NOT NULL,
PRIMARY KEY (`abc`, `id`) -- id got to be second
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
我从手册中引用:
对于 MyISAM 表,您可以在 次要 上指定 AUTO_INCREMENT多列索引中的列.
For MyISAM tables, you can specify AUTO_INCREMENT on a secondary column in a multiple-column index.
此小提琴中的工作示例
这篇关于如何在 MySql 中创建上下文 AUTO_INCREMENT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MySql 中创建上下文 AUTO_INCREMENT?


基础教程推荐
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01