Home >> FAQs/Tutorials >> MySQL Tutorials

MySQL Tutorial - Order of Columns in the SET Clause

By: FYIcenter.com

(Continued from previous topic...)

Is the Order of Columns in the SET Clause Important?

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.

(Continued on next topic...)

  1. What Are DML Statements?
  2. How To Create a Testing Table?
  3. How To Insert a New Row into a Table?
  4. How To Specify Default Values in INSERT Statement?
  5. How To Omit Columns with Default Values in INSERT Statement?
  6. What Happens If Unique Value Constraints Are Violated?
  7. How To Insert Multiple Rows with One INSERT Statement?
  8. How To Update Values in a Table?
  9. How To Update Column Values on Multiple Rows?
  10. How To Use Existing Column Values in the SET Clause?
  11. Is the Order of Columns in the SET Clause Important?
  12. How To Use Values from Other Tables in UPDATE Statements?
  13. What Happens If the UPDATE Subquery Returns No Rows?
  14. What Happens If the UPDATE Subquery Returns Multiple Rows?
  15. How To Delete an Existing Row from a Table?
  16. How To Delete Multiple Rows from a Table?
  17. How To Delete All Rows in a Table?

MySQL Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...