简体   繁体   中英

Is there any reason to use INSERT INTO tbl_name () VALUES(); syntax instead of SET col_name = col_value,…?

I'm personally prefer to use the following syntax:

INSERT INTO tab_name
SET
    col1 = value1,
    col2 = value2

I prefer this syntax as it gives me the full control over the correspondence between column names and the assigning values. It is better visualized from my point of view.

However most of the examples and howto's in the Internet (and on the Stack Overflow as well) uses the INSERT VALUES syntax like the following:

INSERT INTO tab_name (col1,col2) VALUES (value1,value2)

I'm agree that it can be slightly shorter. But in spite of this I hardly imagine how this can be handy. Especially if the number of inserting (updating) columns is more than 3 or 4. It can easily lead to mistakes if I will need to add or exclude one or more columns to/from the query.

So the question is : is there any reason to use INSERT/UPDATE () VALUES () syntax over INSERT/UPDATE SET ?

Because the SET

INSERT INTO tab_name
SET
    col1 = value1,
    col2 = value2

is not SQL standard.

If you have to use other DBMS you can not use this syntax.

The first is SQL standard, the second is MySQL's extension.

Feel free to check also: MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM