Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Is the Order of Columns in the SET Clause Important
Is the Order of Columns in the SET Clause Important? - MySQL FAQs - Understanding SQL INSERT, UPDATE and DELETE Statements
✍: FYIcenter.com
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:
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.
2007-05-11, 4830👍, 0💬
Popular Posts:
How To Enter Numeric Values as HEX Numbers? - MySQL FAQs - Introduction to SQL Basics If you want to...
What will be printed as the result of the operation below: main() { int a=0; if (a==0) printf("Cisco...
How To Delete All Rows a Table? - MySQL FAQs - Understanding SQL INSERT, UPDATE and DELETE Statement...
What is the quickest sorting method to use? The answer depends on what you mean by quickest. For mos...
What's wrong with this initialization? char *p = malloc(10); My compiler is complaining about an ``i...