Please see the Updated section of this question that follows after the initial question.
I have two tables, one for videos that contains a id, users_id, image, pcimagename, and video URL columns; and another for images that contains a id, users_id, file_name, user_file_name, and usage_count columns.
The primary key of both tables are the id columns, which are auto-incrementing, but the two id values are not related to each other. The images table also has a unique key on the file_name column. The usage_count column has 1 for its default value.
The phpMyAdmin shows the following information about the mySQL database that I'm using:
Database server
Server: Localhost via UNIX socket
Server type: MariaDB
Server version: 5.5.52-MariaDB - MariaDB Server
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)
My goal is that whenever a row is to be deleted from the videos table, I want to first copy the image information from the videos table into the images table, and if the image (specifically the file_name column value) doesn't already exist in the images table, set the usage_count to 1, but, if the image does exist, I want to increment the usage_count by 1 and perform an update. I searched for "mySQL INSERT or UPDATE" and came up with with the INSERT ... ON DUPLCATE KEY UPDATE variant, which appears to be what I'm trying to do.
However, I've never used the INSERT ... ON DUPLICATE KEY UPDATE ... statement before. Looking at the MySQL INSERT ON DUPLICATE KEY UPDATE and Insert into a MySQL table or update if exists pages, I came up with the following; unfortunately, I'm able to connect to my database at this time, so I'm unable to determine if it is correct:
INSERT
INTO `photos`
( `users_id`, `file_name`, `user_file_name` )
SELECT `users_id`, `image`, `pcimagename`
FROM `videos`
WHERE `status` = \'Deleted\'
AND `last_updated_on` <= DATE_SUB( NOW(), INTERVAL 1 DAY )
AND `tracks_id` = 0
ON DUPLICATE KEY UPDATE usage_count = VALUES( usage_count ) + 1;
My questions are:
Is the syntax correct, specifically:
1a) Is the placement of the ON DUPLICATE phrase correct?
1b) Do I need to enclose the SELECT statement in parenthesizes, I've seen examples which did and others that didn't.
Will the UPDATE usage_count = VALUES( usage_count ) + 1 phrase cause each row inserted into the image table to add 1 to the row's pre-insert usage_count value, or will all of the rows coming from the SELECT FROM videos be given the same value? I didn't see very much about how this part of the ON DUPLICAT statement actually worked. Most examples just assigned a fixed value.
Update
While working with the dbFiddle that Anina provided, I noticed that running the following INSERT statement didn't increment as I expected:
After several INSERT ... ON DUPLICATE KEY UPDATE ... statements the test
table contains:
id val test action comment
1 1 2 update
2 2 2 update
3 3 1 insert
Perform another INSERT ... ON DUPLICATE KEY UPDATE ... statement,
this time to increment val 1, expecting that the test column should increment to 3.
INSERT INTO test (val )
VALUES ( 1 )
ON DUPLICATE KEY UPDATE test = VALUES( test ) + 1,
action = 'update',
comment = 'Why isn''t test 3?';
However ....
id val test action comment
1 1 2 update Why isn't test 3?
2 2 2 update
3 3 1 insert
The table was created as follows:
CREATE TABLE test ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY( ID ),
val INT NOT NULL, UNIQUE( val ),
test INT DEFAULT 1,
action VARCHAR( 10 ) DEFAULT 'insert',
comment VARCHAR( 30 ) );
Thanks