是否可以更改此触发器以使表插入sortorder
2 个列值 ( sortOrderId
, )?sortOrder
发现的价值如何sortOrder
?
如果它是已知的并且可以插入到image
表中,那么它也可以插入到sortorder
表中吗?
-- Trigger DDL Statements
DELIMITER $$
USE `nextcart`$$
CREATE
DEFINER=`root`@`localhost`
TRIGGER `nextcart`.`insert_sortorderid`
BEFORE INSERT ON `nextcart`.`image`
FOR EACH ROW
BEGIN
INSERT INTO sortorder SET sortOrderId = NULL, sortOrder = NEW.sortOrder;
SET NEW.sortOrderId = (SELECT LAST_INSERT_ID());
END;
$$
创建表排序:
delimiter $$
CREATE TABLE `sortorder` (
`sortOrderId` int(11) NOT NULL AUTO_INCREMENT,
`sortOrder` tinyint(4) NOT NULL,
PRIMARY KEY (`sortOrderId`),
KEY `sort_order` (`sortOrderId`,`sortOrder`),
CONSTRAINT `fk_sortOrderId` FOREIGN KEY (`sortOrderId`) REFERENCES `image` (`imageId`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8$$
创建表图像:
delimiter $$
CREATE TABLE `image` (
`imageId` int(11) NOT NULL AUTO_INCREMENT,
`imageFileName` varchar(45) DEFAULT NULL,
`imagePath` varchar(255) DEFAULT NULL,
`imageTitle` varchar(100) DEFAULT NULL,
`imageAlt` varchar(100) DEFAULT NULL,
`imageWidth` int(11) DEFAULT NULL,
`imageHeight` int(11) DEFAULT NULL,
`classId` int(11) DEFAULT NULL,
`imageSizeId` tinyint(4) NOT NULL,
`isImageEnabled` bit(1) DEFAULT b'0',
`sortOrderId` int(11) DEFAULT NULL,
PRIMARY KEY (`imageId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8$$
错误信息:
错误 1054:'NEW' SQL 语句中的未知列 'sortOrder':
CREATE TRIGGER insert_sortorderid BEFORE INSERT ON Image FOR EACH ROW BEGIN INSERT INTO nextcart.sortorder SET sortOrderId = NULL, sortOrder = NEW.sortOrder; SET NEW.sortOrderId = (SELECT LAST_INSERT_ID()); 结尾; 运行故障回复脚本时出错。详情如下。错误 1050:表 'image' 已存在 SQL 语句:CREATE TABLEimage
(imageId
int(11) NOT NULL AUTO_INCREMENT,
imageFileName
varchar(45) DEFAULT NULL,imagePath
varchar(255) DEFAULT NULL,imageTitle
varchar(100) DEFAULT NULL,imageAlt
varchar(100) DEFAULT NULL ,imageWidth
int(11) 默认空值,
imageHeight
int(11) 默认空值,classId
int(11) 默认空值,imageSizeId
tinyint(4) 非空值,isImageEnabled
位(1)默认 b'0',sortOrderId
int(11)默认 NULL,主键(imageId
))引擎=InnoDB AUTO_INCREMENT=5 默认字符集=utf8