如何在 MySql 中创建上下文 AUTO_INCREMENT?

2022-01-05 00:00:00 mysql phpmyadmin

我想创建一个上下文增量,不确定我使用的术语是否正确.这是我的桌子

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.

此小提琴中的工作示例

相关文章