简体   繁体   中英

1 auto_increment with 2 primary keys

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;

How do you insert your data? If you give CODIEI2 as a column and a value as well, this will overwrite the auto_increment.

Test in SQLFiddle:

Build schema:

CREATE TABLE CONFIGURABLES(
CODIEI2 INTEGER AUTO_INCREMENT,
CODIEI1 INTEGER,
CONSTRAINT PK_CODIEI PRIMARY KEY(CODIEI2));

ALTER TABLE CONFIGURABLES AUTO_INCREMENT = 100;

INSERT INTO CONFIGURABLES ( CODIEI1 ) 
VALUES ( 1 );

Now overwriting the auto_increment:

INSERT INTO CONFIGURABLES ( CODIEI2, CODIEI1 ) 
VALUES ( 1, 2 );

Run SQL:

SELECT *
FROM CONFIGURABLES;

Output:

CODIEI2 CODIEI1
1   2
100 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM