CONCAT returns NULL
if any argument is NULL
.
You col_c
is too short so it will never update as the value is not different then before.
CREATE TABLE my_table (
col_a VARCHAR(20) NOT NULL,
col_b VARCHAR(10) NOT NULL,
col_c VARCHAR(5) NULL DEFAULT NULL,
UNIQUE INDEX (col_a, col_b)
);
INSERT INTO my_table (col_a,col_b,col_c) VALUES ('ABCDEF-123','1543','Apples');
Query OK, 1 row affected, 1 warning (0.06 sec)
We got "1 warning":
SHOW WARNINGS;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1265 | Data truncated for column 'col_c' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)
col_c
allows only 5 characters so values will be truncated on insert. We can verify this as we check the table:
SELECT * FROM my_table;
+------------+-------+-------+
| col_a | col_b | col_c |
+------------+-------+-------+
| ABCDEF-123 | 1543 | Apple |
+------------+-------+-------+
1 row in set (0.00 sec)
Ok, now try to insert another line:
insert into my_table (col_a,col_b,col_c) values ('ABCDEF-123','1543','Oranges') ON DUPLICATE KEY UPDATE col_c=concat(col_c,',','Oranges');
Query OK, 0 rows affected, 2 warnings (0.00 sec)
It says "2 warnings". So let's check:
SHOW WARNINGS;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1265 | Data truncated for column 'col_c' at row 1 |
| Warning | 1265 | Data truncated for column 'col_c' at row 1 |
+---------+------+--------------------------------------------+
2 rows in set (0.00 sec)
The CONCAT value would be "Apple,Oranges" which will be truncated to "Apple" which is the same as before.