错误代码:1109。字段列表中的未知表'evrz.account'

时间:2019-05-22 17:58:09

标签: mysql

我正在尝试创建触发器,但出现此错误:

  

错误代码:1109。字段列表中的未知表'evrz.account'

我尝试执行此操作:

INSERT INTO `record` (`record`.account_id) VALUES (289688082)

这是我的触发器:

CREATE DEFINER=`root`@`localhost` TRIGGER `evrz`.`record_BEFORE_INSERT` BEFORE INSERT ON `record` FOR EACH ROW
BEGIN
   IF `evrz`.`account`.`status` ='OUT' in (
            SELECT `evrz`.`account`.`status`
            FROM `evrz`.`account`
            WHERE (account_id = NEW.account_id)
        )THEN
UPDATE `evrz`.`account` SET `evrz`.`account`.`status` = 'IN' WHERE (`evrz`.`account`.`account_id` = NEW.account_id);
END IF;
IF `evrz`.`account`.`status` ='IN' in (
            SELECT `evrz`.`account`.`status`
            FROM `evrz`.`account`
            WHERE (account_id = NEW.account_id)
        )THEN
UPDATE `evrz`.`account` SET `status` = 'OUT' WHERE (`evrz`.`account`.`account_id` = NEW.account_id);
END IF;

END

这些是我的桌子:

CREATE TABLE `employee` (
    `employee_id` int(10) NOT NULL AUTO_INCREMENT,
    `name` varchar(45) NOT NULL,
    `lastname` varchar(45) NOT NULL,
    `address` varchar(45) NOT NULL,
    `phone` varchar(45) NOT NULL,
    `mail` varchar(45) NOT NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT 'CURRENT_TIMESTAMP()',
    PRIMARY KEY (`employee_id`)
);

CREATE TABLE `account` (
    `account_id` int(10) NOT NULL AUTO_INCREMENT,
    `status` enum('IN','OUT') NOT NULL DEFAULT ''OUT'',
    `employee_id` int(10) NOT NULL UNIQUE,
    PRIMARY KEY (`account_id`)
);

CREATE TABLE `record` (
    `record_id` int(10) NOT NULL AUTO_INCREMENT,
    `account_id` int(10) NOT NULL,
    `creted_at` TIMESTAMP(0) NOT NULL DEFAULT 'CURRENT_TIMESTAMP()',
    PRIMARY KEY (`record_id`)
);

ALTER TABLE `account` ADD CONSTRAINT `account_fk0` FOREIGN KEY (`employee_id`) REFERENCES `employee`(`employee_id`);

ALTER TABLE `record` ADD CONSTRAINT `record_fk0` FOREIGN KEY (`account_id`) REFERENCES `account`(`account_id`);

我正在尝试使一名员工登录数据库时,其状态将被更改。

1 个答案:

答案 0 :(得分:0)

IF语句只能引用正在插入的行,而使用NEW.columnName引用新行中的列。

当更新另一个表时,在值中使用IF函数(或CASE表达式),它可以引用要更新的行中的现有值。

CREATE DEFINER=`root`@`localhost` TRIGGER `evrz`.`record_BEFORE_INSERT` BEFORE INSERT ON `record` FOR EACH ROW
BEGIN
   UPDATE evrz.account
   SET status = IF(status = 'OUT', 'IN', 'OUT')
   WHERE account_id = NEW.account_id;
END