[英]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. 我的意图是创建一个带有2个主键的表,其中一个自动递增,另一个在插入时指定,当我为该表创建新字段时,如果未递增的主键发生更改,则必须开始重新计数。 This is what I had:
这就是我所拥有的:
I have been able to get this changing the table engine to MyISAM. 我已经能够将表引擎更改为MyISAM。 But there is something missing, the
auto_increment
does not start at 100 as it happened before. 但是缺少一些东西,
auto_increment
不能像以前那样从100开始。
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
值与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;
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.
如果同时将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: 运行SQL:
SELECT *
FROM CONFIGURABLES;
Output: 输出:
CODIEI2 CODIEI1
1 2
100 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.