sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo generar una tabla de secuencia dinámica en MySQL?

El motor MyISAM lo hará por usted -

Definición de tabla:

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;

Rellenar tabla:

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);

Mostrar resultado:

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