Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Wie erstelle ich eine dynamische Sequenztabelle in MySQL?

Die MyISAM-Engine erledigt das für Sie -

Tabellendefinition:

CREATE TABLE `sequence` (
  `label` char(30) CHARACTER SET latin1 NOT NULL,
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Tabelle füllen:

INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);
INSERT INTO sequence VALUES ('a', NULL);

INSERT INTO sequence VALUES ('b', NULL); -- add another labels 'b'
INSERT INTO sequence VALUES ('b', NULL);

INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);

Ergebnis anzeigen:

SELECT * FROM sequence;
+-------+----+
| label | id |
+-------+----+
| a     |  1 |
| a     |  2 |
| a     |  3 |
| a     |  4 |
| a     |  5 |
| a     |  6 |
| b     |  1 |
| b     |  2 |
+-------+----+