【问题标题】:How can this be done using an InnoDB table type?如何使用 InnoDB 表类型来做到这一点?
【发布时间】:2013-05-03 11:18:19
【问题描述】:

我有以下使用 MyIsam 表类型的代码,因为请参阅创建代码中的注释。如何使用 Innodb 做到这一点。

其实就是根据用户生成一个新的文章id,每个用户从1开始。但是,如果更改表类型,则无法执行此操作。使用 INNODB 时如何实现?

CREATE TABLE `articles` (
    `artcId` INT(10) NOT NULL AUTO_INCREMENT,
    `artcUserId` INT(10) NOT NULL DEFAULT '0',
    `artcTitle` VARCHAR(50) NOT NULL DEFAULT 'No title defined',
    `arctTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`artcUserId`, `artcId`)// See this part here and see the Image below.
)
COLLATE='utf8_general_ci'
ENGINE=myisam;

【问题讨论】:

标签: php mysql sql


【解决方案1】:

您可以使用发布在其他线程中的触发器或手动操作。

伪代码:

START TRANSACTION; -- needs to be in a transaction
...
SELECT @lastid := MAX(idItem) FROM item WHERE idArticulo = ?;
INSERT INTO item (idArticulo, idItem, texto)
VALUES (?, @lastid+1, ?);
...
COMMIT;

To handle the first-time situation:
MAX(idItem)
-->
IFNULL(MAX(idItem), 1)

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    相关资源
    最近更新 更多