We look at the syntax for creating a trigger:
CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_body trigger_time: { BEFORE | AFTER } trigger_event: { INSERT | UPDATE | DELETE }
Your code seems to match the specified syntax:
CREATE TRIGGER `CreatePhotoAlbumUser` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO usersalbums Set UsersAlbumsName = 'Diplomas, awards, certificates', UsersAlbumsTime = NEW.UsersTime, UsersAlbumsType = 1, UsersAlbumsIdToUser = NEW.idUsers; END
What is the problem?
But the fact is that mysql executes the command as soon as you enter it completely.
What does it mean to "enter it completely"? This means that you typed a command and ended it with a command separator. By default, this delimiter is the sign ; (semicolon)
Those. in fact, mysql thinks you entered the command
CREATE TRIGGER `CreatePhotoAlbumUser` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO usersalbums Set UsersAlbumsName = 'Diplomas, awards, certificates', UsersAlbumsTime = NEW.UsersTime, UsersAlbumsType = 1, UsersAlbumsIdToUser = NEW.idUsers;
Obviously, this command is not valid.
How, then, correctly specify the command in the trigger? To do this, there is a special syntax for changing the command separator:
DELIMITER DELIMITER_CHAR
Thus, if you change the command delimiter, then mysql will take our command as one. The main thing is to remember to complete our design with a new separator (which, by the way, you don’t have) and return our old separator to its place.
As a result, we obtain that the following code is needed to create a trigger:
DELIMITER | CREATE TRIGGER `CreatePhotoAlbumUser` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO usersalbums Set UsersAlbumsName = 'Diplomas, awards, certificates', UsersAlbumsTime = NEW.UsersTime, UsersAlbumsType = 1, UsersAlbumsIdToUser = NEW.idUsers; END | DELIMITER ;
Now, after DELIMITER | sign ; is not a command separator and mysql will not give an error when it encounters this sign. After creating our trigger, we return the old command separator ; (semicolon)