1个auto_increment,带2个主键

时间:2022-12-13 09:08:13

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:

我的目的是创建一个包含2个主键的表,其中一个是自动增量,另一个是在插入时指定的,当我为此表创建一个新字段时,如果未增加的主键发生更改,则必须启动重新计数。这就是我所拥有的:

1个auto_increment,带2个主键

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.

我已经能够将表引擎更改为MyISAM。但是有些东西丢失了,auto_increment并没有像以前那样从100开始。

1个auto_increment,带2个主键

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?

这是否发生,因为当auto_increment值与默认数字不同时,引擎必须设置为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.

该表可以返回到InnoDB,这样更好,并且在CONFIGURABLES表上不需要auto_increment,因为在执行插入时这将被控制。

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;

2 个解决方案

#1


  BEGIN;
  SELECT @id := MAX(id)+1 FROM foo WHERE other = 123 FOR UPDATE;
  INSERT INTO foo
     (other, id, ...)
     VALUES
     (123, @id, ...);
  COMMIT;

#2


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

你如何插入数据?如果您将CODIEI2作为列和值给出,则会覆盖auto_increment。

Test in SQLFiddle:

在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:

现在覆盖auto_increment:

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

Run SQL:

SELECT *
FROM CONFIGURABLES;

Output:

CODIEI2 CODIEI1
1   2
100 1

#1


  BEGIN;
  SELECT @id := MAX(id)+1 FROM foo WHERE other = 123 FOR UPDATE;
  INSERT INTO foo
     (other, id, ...)
     VALUES
     (123, @id, ...);
  COMMIT;

#2


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

你如何插入数据?如果您将CODIEI2作为列和值给出,则会覆盖auto_increment。

Test in SQLFiddle:

在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:

现在覆盖auto_increment:

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

Run SQL:

SELECT *
FROM CONFIGURABLES;

Output:

CODIEI2 CODIEI1
1   2
100 1