1 auto_increment 有 2 个主键
1 auto_increment with 2 primary keys
我的目的是创建一个 table,其中有 2 个主键,其中一个是自动递增的,另一个是在插入时指定的,当我为此 table 创建一个新字段时,它必须开始重新计数如果未递增的主键更改。这是我的:
我已经能够将 table 引擎更改为 MyISAM。但是缺少一些东西,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;
发生这种情况是因为当 auto_increment
值不同于默认值时引擎必须设置为 InnoDB 吗?
有什么方法可以随心所欲地得到吗?
解决方案:
table 可以返回到 InnoDB,这要好得多并且不需要 auto_increment
on CONFIGURABLES
table 因为这将在执行插入。
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);
并且在插入时这样做:
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;
如何插入数据?如果您将 CODIEI2 作为列和值提供,这将覆盖 auto_increment。
在 SQLFiddle 中测试:
构建架构:
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 );
现在覆盖 auto_increment:
INSERT INTO CONFIGURABLES ( CODIEI2, CODIEI1 )
VALUES ( 1, 2 );
运行 SQL:
SELECT *
FROM CONFIGURABLES;
输出:
CODIEI2 CODIEI1
1 2
100 1
BEGIN;
SELECT @id := MAX(id)+1 FROM foo WHERE other = 123 FOR UPDATE;
INSERT INTO foo
(other, id, ...)
VALUES
(123, @id, ...);
COMMIT;
我的目的是创建一个 table,其中有 2 个主键,其中一个是自动递增的,另一个是在插入时指定的,当我为此 table 创建一个新字段时,它必须开始重新计数如果未递增的主键更改。这是我的:
我已经能够将 table 引擎更改为 MyISAM。但是缺少一些东西,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;
发生这种情况是因为当 auto_increment
值不同于默认值时引擎必须设置为 InnoDB 吗?
有什么方法可以随心所欲地得到吗?
解决方案:
table 可以返回到 InnoDB,这要好得多并且不需要 auto_increment
on CONFIGURABLES
table 因为这将在执行插入。
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);
并且在插入时这样做:
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;
如何插入数据?如果您将 CODIEI2 作为列和值提供,这将覆盖 auto_increment。
在 SQLFiddle 中测试:
构建架构:
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 );
现在覆盖 auto_increment:
INSERT INTO CONFIGURABLES ( CODIEI2, CODIEI1 )
VALUES ( 1, 2 );
运行 SQL:
SELECT *
FROM CONFIGURABLES;
输出:
CODIEI2 CODIEI1
1 2
100 1
BEGIN;
SELECT @id := MAX(id)+1 FROM foo WHERE other = 123 FOR UPDATE;
INSERT INTO foo
(other, id, ...)
VALUES
(123, @id, ...);
COMMIT;