Question

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 ;

after this i did

INSERT INTO `test` (`abc`, `data`) VALUES
('hi', 'hello2'),
('hi2', 'hello2'),
('hi', 'hello2');

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

but i wanted it this way

1   hi  hello2
1   hi2     hello2
3   hi  hello2

can anybody tell me what i am doing wrong here?

Était-ce utile?

La solution

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 ;

I cite from the manual:

For MyISAM tables, you can specify AUTO_INCREMENT on a secondary column in a multiple-column index.

Working example in this fiddle

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top