【发布时间】:2021-05-25 04:06:24
【问题描述】:
我是在 SQL 中创建触发器并试图让这个触发器工作(使用 Mysql)的新手。它应该将博客条目的新(更新)版本放入名为 EditedPosts 的表中,而只保留表 Posts 中的原始行不变。
我尝试了几种不同的方法,但无法让它发挥作用。有人有什么想法吗?
我的触发器:
DELIMITER $$
CREATE TRIGGER editedPost
BEFORE UPDATE on Posts
for each row
begin
insert into editedPosts VALUES (PostID, Titel, Content, Created, UserName, BlogID) FROM new;
insert into Posts VALUES (PostID, Titel, Content, Created, UserName, BlogID) FROM old;
END $$
DELIMITER ;
我的表帖子:
create table Posts (PostID INTEGER AUTO_INCREMENT not null,
Titel VARCHAR (50) not null,
Content TEXT not null,
Created timestamp DEFAULT current_timestamp,
UserName VARCHAR (50) not null,
BlogID INTEGER not null,
primary key (PostID),
foreign key(UserName) references User_Accounts(UserName),
foreign key (BlogID) REFERENCES Blogs (BlogID));
我的表已编辑帖子:
create table EditedPosts (EPostID INTEGER AUTO_INCREMENT not null,
PostID INTEGER not null,
Titel VARCHAR (50) not null,
Content TEXT not null,
Created timestamp,
Edited timestamp DEFAULT current_timestamp,
UserName VARCHAR (50) not null,
BlogID INTEGER not null,
primary key (EPostID),
foreign key (PostID) REFERENCES Posts(PostID),
foreign key(UserName) references User_Accounts(UserName),
foreign key (BlogID) REFERENCES Blogs (BlogID));
【问题讨论】: