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, 1702🔥, 0💬
Popular Posts:
What are DDL (Data Definition Language) statements for tables in SQL Server? DDL (Data Definition La...
Where to find answers to frequently asked questions on INSERT, UPDATE and DELETE Statements in MySQL...
Where to find SQL Server Transact-SQL language references? You can find SQL Server Transact-SQL lang...
What are binary literals supported in SQL Server Transact-SQL? Binary literals in Transact-SQL are s...
How To Drop a Stored Procedure in Oracle? If there is an existing stored procedure and you don't wan...