如何在MySql中创建上下文AUTO_INCREMENT? [英] 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文