我对MySQL很新,并且遇到了我的程序错误,我希望能够为我的表更新,插入和删除调用相同的东西,因此需要一个程序来调用。 我看过MySQL Fire Trigger for both Insert and Update和The MySQL "DELIMITER" keyword isn't working以及其他一些但没有任何帮助。 (delimiter关键字也不起作用产生一个sepperate错误,但没有帮助) 错误是这个,我无法解决它的含义:
"错误 SQL查询:
-- trigger on work log change
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES CREATE DEFINER = root@localhost PROCEDURE PROC_TRACK_CHANGES BEGIN INSERT INTO CHANGES( LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone )
SELECT NOW( ) , EmployeeID, LogTimeStamp, ProposalID, WorkDone
FROM WORKLOG;
MySQL说:
#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在#" CREATE DEFINER = root @ localhost PROCEDURE PROC_TRACK_CHANGES附近使用正确的语法 开始 插入'在第3行"
有问题的错误代码是:
CREATE TABLE WORKLOG(
EmployeeID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY PKEY
LogTimeStamp TIMESTAMP, -- PKEY
ProposalID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY
WorkDone VARCHAR(200),
CONSTRAINT WORKLOG_FOREIGN_KEY_EMP FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEES (EmployeeID),
CONSTRAINT WORKLOG_FOREIGN_KEY_PRO FOREIGN KEY (ProposalID) REFERENCES PROPOSALS (ProposalID),
CONSTRAINT WORKLOG_PROPOSALS_PRIMARY_KEY PRIMARY KEY (EmployeeID, LogTimeStamp)
);
CREATE TABLE CHANGES(
LoggedTimeStamp TIMESTAMP, -- PKEY
EmployeeID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY
LogTimeStamp TIMESTAMP, -- fkey
ProposalID BIGINT UNSIGNED NOT NULL UNIQUE, -- FKEY
WorkDone VARCHAR(200),
CONSTRAINT CHANGES_FOREIGN_KEY_EMP FOREIGN KEY (EmployeeID) REFERENCES EMPLOYEES (EmployeeID),
CONSTRAINT CHANGES_FOREIGN_KEY_PRO FOREIGN KEY (ProposalID) REFERENCES PROPOSALS (ProposalID),
CONSTRAINT CHANGES_PRIMARY_KEY PRIMARY KEY (LoggedTimeStamp)
);
-- trigger on work log change
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES
CREATE DEFINER=root@localhost PROCEDURE PROC_TRACK_CHANGES
BEGIN
INSERT INTO CHANGES (LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone)
SELECT now(), EmployeeID, LogTimeStamp, ProposalID, WorkDone FROM WORKLOG;
END;
DROP TRIGGER IF EXISTS TR_WORKLOG_UPDATE
CREATE DEFINER=root@localhost TRIGGER TR_WORKLOG_UPDATE
BEFORE UPDATE ON 'WORKLOG'
FOR EACH ROW
BEGIN
CALL PROC_TRACK_CHANGES();
END;
DROP TRIGGER IF EXISTS TR_WORKLOG_INSERT
CREATE DEFINER=root@localhost TRIGGER TR_WORKLOG_INSERT BEFORE INSERT ON WORKLOG
FOR EACH ROW
BEGIN
CALL PROC_TRACK_CHANGES();
END;
DROP TRIGGER IF EXISTS TR_WORKLOG_DELETE
CREATE DEFINER=root@localhost TRIGGER TR_WORKLOG_DELETE BEFORE DELETE ON WORKLOG
FOR EACH ROW
BEGIN
CALL PROC_TRACK_CHANGES();
END;
这可能是我想要的简单但MYSQL调试器让我感到困惑。 非常感谢!
答案 0 :(得分:0)
试试这个:
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES;
注意最后的分号。
答案 1 :(得分:0)
过程drop
也是单个语句和过程create
。用分隔符分隔它们。
另一件事是你需要定义另一个分隔符而不是;
。否则,DB将对;
处的每个语句进行终结,这使得某些触发器和过程定义不完整。所以使用例如
delimiter |
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES
|
CREATE DEFINER=root@localhost PROCEDURE PROC_TRACK_CHANGES
BEGIN
INSERT INTO CHANGES (LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone)
SELECT now(), EmployeeID, LogTimeStamp, ProposalID, WorkDone FROM WORKLOG;
END
|
答案 2 :(得分:0)
您应该指定除分号以外的临时分隔符,并在DROP PROCEDURE
和CREATE PROCEDURE
语句的末尾使用该分隔符。
此外,您需要在CREATE PROCEDURE
语句中的过程名称后添加括号,即使它不带参数。
这应该适合你:
DELIMITER $$
DROP PROCEDURE IF EXISTS PROC_TRACK_CHANGES $$
CREATE DEFINER=root@localhost PROCEDURE PROC_TRACK_CHANGES ()
BEGIN
INSERT INTO CHANGES (LoggedTimeStamp, EmployeeID, LogTimeStamp, ProposalID, WorkDone)
SELECT now(), EmployeeID, LogTimeStamp, ProposalID, WorkDone FROM WORKLOG;
END $$
DELIMITER ;