mysql - Auto Increment column when row is updated -
i have mysql table structure seen below. last column represents number of downloads of file. each time file accessed, it's uuid changes.
table structure:
+---------+--------------------+--------------+-------------+ | dkey | file_name | file | downloads | +---------+--------------------+--------------+-------------+ | uuid | file.mp4 | file alias | 0 | | uuid2 | another_file.mov | file alias | 3 | +---------+--------------------+--------------+-------------+
i wondering if there way increment download count if row updated. it's not requirement, can change when change uuid, wondering if there way this.
i looked around , couldn't find showed how it, or if couldn't done.
you can use trigger.
the syntax this:
create trigger increment_downloads before update on mytable each row begin set new.downloads = old.downloads + 1 end;
edit
when creating trigger, may need change delimiter, this:
delimiter // create trigger increment_downloads before update on mytable each row begin set new.downloads = old.downloads + 1; end; // delimiter ;
i tested in mysql first inserting row this:
insert mytable (uuid, downloads) values ('test', 0);
i updated so:
update mytable set uuid = 'test1' uuid = 'test';
and when pulled row, downloads equal 1:
note: in trigger, cannot set value of new.
column in after update
trigger, why answer edited use before
.
Comments
Post a Comment