如何在 MySql 中创建上下文 AUTO_INCREMENT? [英] How do I create contextual AUTO_INCREMENT in MySql?

查看:20
本文介绍了如何在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆