Order of Columns in the SET Clause in MySQL

Q

Is the Order of Columns in the SET Clause Important in MySQL?

✍: FYIcenter.com

A

Yes. The order of columns in the SET clause of the UPDATE statement is important. There is a BIG DIFFERENCE between MySQL and Oracle on update columns with previous values:

  • Oracle provides you the existing values from the database on columns names used in new value expressions.
  • MySQL provides you the updated values on columns names used in new value expressions. So the order of columns in the SET clause is important.

Tutorial exercise 1 - Setting "id" first and "counts" second:

mysql> SELECT id, url, notes, counts, DATE(created) 
   FROM fyi_links WHERE url = 'dev.fyicenter.com';
+-----+-------------------+-------+--------+---------------+
| id  | url               | notes | counts | DATE(created) |
+-----+-------------------+-------+--------+---------------+
| 101 | dev.fyicenter.com | Good. |    999 | 2006-04-30    |
+-----+-------------------+-------+--------+---------------+
1 row in set (0.00 sec)

mysql> UPDATE fyi_links SET id = id+200, counts = id*2 
   WHERE url = 'dev.fyicenter.com';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT id, url, notes, counts, DATE(created) 
   FROM fyi_links WHERE url = 'dev.fyicenter.com';
+-----+-------------------+-------+--------+---------------+
| id  | url               | notes | counts | DATE(created) |
+-----+-------------------+-------+--------+---------------+
| 301 | dev.fyicenter.com | Good. |    602 | 2006-04-30    |
+-----+-------------------+-------+--------+---------------+
1 row in set (0.00 sec)

mysql> UPDATE fyi_links SET id = 101, counts = 999 
   WHERE url = 'dev.fyicenter.com';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Notice that the "id" in the "counts" new value expression is taking the new value of the "id" column, because it appears before the "counts" columns in the SET clause.

Tutorial exercise 2 - Setting "counts" first and "id" second:

mysql> SELECT id, url, notes, counts, DATE(created) 
   FROM fyi_links WHERE url = 'dev.fyicenter.com';
+-----+-------------------+-------+--------+---------------+
| id  | url               | notes | counts | DATE(created) |
+-----+-------------------+-------+--------+---------------+
| 101 | dev.fyicenter.com | Good. |    999 | 2006-04-30    |
+-----+-------------------+-------+--------+---------------+
1 row in set (0.00 sec)

mysql> UPDATE fyi_links SET counts = id*2, id = id+200 
   WHERE url = 'dev.fyicenter.com';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT id, url, notes, counts, DATE(created) 
   FROM fyi_links WHERE url = 'dev.fyicenter.com';
+-----+-------------------+-------+--------+---------------+
| id  | url               | notes | counts | DATE(created) |
+-----+-------------------+-------+--------+---------------+
| 301 | dev.fyicenter.com | Good. |    202 | 2006-04-30    |
+-----+-------------------+-------+--------+---------------+
1 row in set (0.00 sec)

mysql> UPDATE fyi_links SET id = 101, counts = 999 
   WHERE url = 'dev.fyicenter.com';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Notice that the "id" in the "counts" new value expression is taking the old value of the "id" column, because it appears after the "counts" columns in the SET clause.

 

UPDATE Using Data from Other Tables in MySQL

Use Existing Column Values in the SET Clause in MySQL

INSERT, UPDATE and DELETE Statements in MySQL

⇑⇑ MySQL Database Tutorials

2018-01-13, 1489🔥, 0💬