I have a MySQL table for storing details of uploaded files, like this:
CREATE TABLE files (
file_id bigint not null primary key auto_increment,
file_name text,
file_path text,
file_extension varchar(15),
file_link text);
The actual name of the file on the disk is set to the file_id generated by the table when I insert the record – so although I can record all sorts of details, the database doesn’t actually know what the file is called at this point.
Sure it’s easy to just run another UPDATE statement shortly after to set the file_link column, but I wondered if there was a way to do something a bit more elegant here?
What I would like to do is have the value of file_link be automatically set on INSERT to this: CONCAT(file_path, file_id, file_extension) without needing to run another UPDATE statement immediately after.
Does anyone know how I can do this – and if it’s a bad idea can you explain why?
P.S I’m using PHP to run these statements, if that makes a difference.
You could do this with the following trigger:
…although I have to say, I personally would go with creating this with the
SELECTstatement instead of having the database do things like this on insert – sure it’s slightly more efficient to store static data, but it makes the database schema less transparent.