简体   繁体   English

是否有任何理由使用INSERT INTO tbl_name()VALUES(); 语法,而不是SET col_name = col_value,…?

[英]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: 但是,Internet中(以及堆栈溢出中)的大多数示例和操作方法都使用INSERT VALUES语法,如下所示:

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. 尤其是如果插入(更新)列的数量超过3或4。如果我需要向查询中添加或从查询中排除一个或多个列,则很容易导致错误。

So the question is : is there any reason to use INSERT/UPDATE () VALUES () syntax over INSERT/UPDATE SET ? 因此,问题是 :是否有理由在INSERT/UPDATE SET上使用INSERT/UPDATE () VALUES ()语法?

Because the SET 因为SET

INSERT INTO tab_name
SET
    col1 = value1,
    col2 = value2

is not SQL standard. 不是SQL标准。

If you have to use other DBMS you can not use this syntax. 如果必须使用其他DBMS,则不能使用此语法。

The first is SQL standard, the second is MySQL's extension. 第一个是SQL标准,第二个是MySQL的扩展。

Feel free to check also: MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET 还可以随时检查: MySQL INSERT INTO表VALUES .. vs INSERT INTO表SET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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