My intention is to create a table with 2 primary keys with one of those autoincrementing and the other specified when inserting and when I create a new field for this table it must start the recount if the not incremented primary key changes. This is what I had:
I have been able to get this changing the table engine to MyISAM. But there is something missing, the auto_increment
does not start at 100 as it happened before.
CREATE TABLE CONFIGURABLES(
CODIEI2 INTEGER AUTO_INCREMENT,
CODIEI1 INTEGER,
SKU VARCHAR(30),
COLOR INTEGER,
COLOR2 INTEGER,
TALLA INTEGER,
CONSTRAINT PK_CODIEI PRIMARY KEY(CODIEI1,CODIEI2),
CONSTRAINT FK_CODIEI1 FOREIGN KEY(CODIEI1) REFERENCES PRODUCTOS(ENTITY_ID) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR FOREIGN KEY(COLOR) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR2 FOREIGN KEY(COLOR2) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CTALLA FOREIGN KEY(TALLA) REFERENCES TALLAS(CODITLL) ON DELETE CASCADE) ENGINE=MyISAM;
ALTER TABLE CONFIGURABLES AUTO_INCREMENT = 100;
Is this happening because when the auto_increment
value is different from the default number the engine must be set to InnoDB?
Is there a way to get it as I want?
SOLUTION:
The table can be back to InnoDB which is much better and there is no need for auto_increment
on CONFIGURABLES
table as this will be controlled when doing the insert.
CREATE TABLE CONFIGURABLES(
CODIEI2 INTEGER,
CODIEI1 INTEGER,
SKU VARCHAR(30),
COLOR INTEGER,
COLOR2 INTEGER,
TALLA INTEGER,
CONSTRAINT PK_CODIEI PRIMARY KEY(CODIEI1,CODIEI2),
CONSTRAINT FK_CODIEI1 FOREIGN KEY(CODIEI1) REFERENCES PRODUCTOS(ENTITY_ID) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR FOREIGN KEY(COLOR) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CCOLOR2 FOREIGN KEY(COLOR2) REFERENCES COLORES(CODICOL) ON DELETE CASCADE,
CONSTRAINT FK_CTALLA FOREIGN KEY(TALLA) REFERENCES TALLAS(CODITLL) ON DELETE CASCADE);
And when doing the insert do this:
BEGIN;
SELECT @id := IFNULL(MAX(CODIEI2)+1,100) FROM CONFIGURABLES WHERE CODIEI1 = 10001 FOR UPDATE;
INSERT INTO CONFIGURABLES
(CODIEI1,CODIEI2,SKU,COLOR,COLOR2,TALLA)
VALUES
(10001,@id,'',4,2,2);
COMMIT;
BEGIN;
SELECT @id := MAX(id)+1 FROM foo WHERE other = 123 FOR UPDATE;
INSERT INTO foo
(other, id, ...)
VALUES
(123, @id, ...);
COMMIT;