Collections:
Order of Columns in the SET Clause in MySQL
Is the Order of Columns in the SET Clause Important in MySQL?
✍: 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.
⇒ UPDATE Using Data from Other Tables in MySQL
⇐ Use Existing Column Values in the SET Clause in MySQL
2018-01-13, 1623🔥, 0💬
Popular Posts:
What is dba.FYIcenter.com Website about? dba.FYIcenter.com is a Website for DBAs (database administr...
How To Find Out What Privileges a User Currently Has in Oracle? Privileges granted to users are list...
Where to find answers to frequently asked questions on Storage Engines: MyISAM, InnoDB and BDB in My...
How To Format Time Zone in +/-hh:mm Format in SQL Server Transact-SQL? From the previous tutorial, y...
Can You Drop an Index Associated with a Unique or Primary Key Constraint in Oracle? You can not dele...