I'm new at creating Triggers in SQL and trying to get this one working (using Mysql). It's supposed to take the new (update) version of a blog entry and put it into the table called EditedPosts, and just leave the original row in the table Posts unchanged.
I've tried a few different ways but cannot get it to work. Anybody have any ideas?
My trigger:
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 ;
My table Posts:
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));
My table EditedPosts:
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));