Mysql 使用新旧表触发,插入两个表

Mysql 使用新旧表触发,插入两个表,mysql,sql,triggers,Mysql,Sql,Triggers,我不擅长在SQL中创建触发器,并尝试让它工作(使用Mysql)。它应该获取博客条目的新(更新)版本,并将其放入名为EditedPosts的表中,而只保留表Posts中的原始行不变 我试过几种不同的方法,但都无法奏效。有人有什么想法吗 我的触发器: DELIMITER $$ CREATE TRIGGER editedPost BEFORE UPDATE on Posts for each row begin insert into editedPosts VALUES (PostID, Tit

我不擅长在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));

我想说,这通常是作为一个after
update
触发器来完成的。但这并没有什么区别

在任何情况下,您根本不需要更新POST中的行。然后,您需要将插入内容修复到
EditedPosts

DELIMITER $$ 
CREATE TRIGGER trg_post_update
BEFORE UPDATE on Posts
for each row
begin
    insert into editedPosts (PostID, Titld, Content, Created, UserName, BlogId)
        VALUES (new.PostID, new.Title, new.Content, new.Created, new.UserName, new.BlogID);
END $$
DELIMITER ;
实际上,如果你想保留原始值。而不是更新它们

我不认为MySQL有一个
而不是
触发器。我认为没有一种方法可以在不产生错误的情况下阻止更新。剩下的是重新分配值:

DELIMITER $$ 
CREATE TRIGGER trg_post_update
BEFORE UPDATE on Posts
for each row
begin
    insert into editedPosts (PostID, Titld, Content, Created, UserName, BlogId)
        VALUES (new.PostID, new.Title, new.Content, new.Created, new.UserName, new.BlogID);

    set new.PostID = old.PostId,
        new.Title = old.Title,
        new.Content = old.Content,
        new.Created = old.Created,
        new.UserName = old.UserName,
        new.BlogID = old.BlogID;
END $$
DELIMITER ;

为此,您确实希望在更新之前有一个
触发器。

用于输入。正如您所说,Mysql中没有instead-of触发器,所以我以前一直在使用。如果我按照您在第二个触发器中显示的那样重新分配值,这会不会将旧值移动到EditedPosts,而不是将它们保留在原始Posts表中?我希望新的(编辑过的)输入存储在EditedPosts中。是否更容易创建一个触发器来从Posts表收集某些类型的数据,并从一开始就在“edited Posts”表中创建编辑?@SoX。不,不会的。在更改新值之前,新值将进入编辑帖子。非常感谢,终于可以使用了!你是个明星!