Here's an example from scratch including a potentially major screwup with a way to prevent it.
First we create ourselves an example table to work with in my
test database.
mysql> USE test;
Database changed
mysql> CREATE TABLE `prepend_example`(
-> `col` VARCHAR(8)
-> );
Query OK, 0 rows affected (0.18 sec)
mysql> INSERT INTO `prepend_example`
-> VALUES ('one'),('two'),('three');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM `prepend_example`;
+-------+
| col |
+-------+
| one |
| two |
| three |
+-------+
3 rows in set (0.02 sec)
- mysql> USE test;
-
- Database changed
-
- mysql> CREATE TABLE `prepend_example`(
- -> `col` VARCHAR(8)
- -> );
- Query OK, 0 rows affected (0.18 sec)
-
- mysql> INSERT INTO `prepend_example`
- -> VALUES ('one'),('two'),('three');
- Query OK, 3 rows affected (0.00 sec)
- Records: 3 Duplicates: 0 Warnings: 0
-
- mysql> SELECT * FROM `prepend_example`;
- +-------+
- | col |
- +-------+
- | one |
- | two |
- | three |
- +-------+
- 3 rows in set (0.02 sec)
Now it's as simple as using the CONCAT string function to prepend a value to our column.
mysql> UPDATE `prepend_example`
-> SET `col` = CONCAT('prefix_', `col`)
-> WHERE `col` = 'two';
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 1
- mysql> UPDATE `prepend_example`
- -> SET `col` = CONCAT('prefix_', `col`)
- -> WHERE `col` = 'two';
- Query OK, 1 row affected, 1 warning (0.02 sec)
- Rows matched: 1 Changed: 1 Warnings: 1
Or is it ?
Rows matched: 1 Changed: 1 Warnings: 1
What gives ?
mysql> SELECT * FROM `prepend_example`;
+----------+
| col |
+----------+
| one |
| prefix_t |
| three |
+----------+
3 rows in set (0.00 sec)
- mysql> SELECT * FROM `prepend_example`;
- +----------+
- | col |
- +----------+
- | one |
- | prefix_t |
- | three |
- +----------+
- 3 rows in set (0.00 sec)
The warning was telling us that the new string was truncated due VARCHAR(8) not being large enough.
Alrighty then.
Let's rewind back to the point right before doing the UPDATE and pretend we haven't done it yet.
First we need to know how long the longest entry that we will be working with is.
mysql> SELECT MAX(LENGTH(`col`)) FROM `prepend_example`;
+--------------------+
| MAX(LENGTH(`col`)) |
+--------------------+
| 5 |
+--------------------+
1 row in set (0.01 sec)
- mysql> SELECT MAX(LENGTH(`col`)) FROM `prepend_example`;
- +--------------------+
- | MAX(LENGTH(`col`)) |
- +--------------------+
- | 5 |
- +--------------------+
- 1 row in set (0.01 sec)
5, cool. We know that "prefix_" is 7 characters so we need at least a VARCHAR(12).
It's been awhile since we created this example table and my memory is rather short,
so we better check to see how big our column is real quick.
mysql> SHOW columns FROM `prepend_example`;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| col | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.01 sec)
- mysql> SHOW columns FROM `prepend_example`;
- +-------+------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+------------+------+-----+---------+-------+
- | col | varchar(8) | YES | | NULL | |
- +-------+------------+------+-----+---------+-------+
- 1 row in set (0.01 sec)
Well now VARCHAR(8) will never do, we better make that bigger.
mysql> ALTER TABLE `prepend_example`
-> CHANGE `col` `col` VARCHAR( 16 );
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
- mysql> ALTER TABLE `prepend_example`
- -> CHANGE `col` `col` VARCHAR( 16 );
- Query OK, 3 rows affected (0.02 sec)
- Records: 3 Duplicates: 0 Warnings: 0
Let's try that UPDATE again, but for fun we'll append something this time.
mysql> UPDATE `prepend_example`
-> SET `col` = CONCAT(`col`, ' points')
-> WHERE `col` = 'three';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
- mysql> UPDATE `prepend_example`
- -> SET `col` = CONCAT(`col`, ' points')
- -> WHERE `col` = 'three';
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
And from half-court at the buzzer.
mysql> SELECT * FROM `prepend_example`;
+--------------+
| col |
+--------------+
| one |
| two |
| three points |
+--------------+
3 rows in set (0.00 sec)
- mysql> SELECT * FROM `prepend_example`;
- +--------------+
- | col |
- +--------------+
- | one |
- | two |
- | three points |
- +--------------+
- 3 rows in set (0.00 sec)
Swish!

Strong with this one, the sudo is.