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?


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01