不指定列的SQL INSERT。怎么了? [英] SQL INSERT without specifying columns. What happens?

查看:176
本文介绍了不指定列的SQL INSERT。怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览心爱的W3schools时发现了页面,实际上学到了一些有趣的东西。我不知道您可以在不指定值列的情况下调用插入命令。例如;

  INSERT INTO table_name 
VALUES(值1,值2,值3 ...)

从我朦胧的记忆中拉出,我似乎还记得SQL教授提到过,您必须将字段视为不是以任何特定顺序排列(尽管在RDB方面,但这不能保证)。



我的问题是,服务器如何知道将哪些值分配给哪些字段?*我自己会对此进行测试,但不会使用生产服务器来做目前我可以访问的全部。



如果特定于此技术,我正在研究PostgresSQL。

解决方案

您的教授是正确的-您应在命名值之前明确命名列。 / p>

在这种情况下,虽然值将按照在表定义中出现的顺序插入。



这样做的问题是,如果顺序更改,或者删除或添加了列(即使它们可以为空),则插入将中断。



,在生产代码中就不多了。如果您要手动编码一个快速插入,那么它可能有助于节省您键入所有列名称的时间。


Was looking through the beloved W3schools and found this page and actually learned something interesting. I didn't know you could call an insert command without specifying columns to values. For example;

INSERT INTO table_name
 VALUES (value1, value2, value3,...)

Pulling from my hazy memory, I seem to remember the SQL prof mentioning that you have to treat fields as if they are not in any particular order (although there is on the RDB side, but it's not guaranteed).

My question is, how does the server know which values get assigned to which fields?* I would test this myself, but am not going to use a production server to do which is all I a have access to at the moment.

If this technology specific, I am working on PostgresSQL. How is this particular syntax even useful?

解决方案

Your prof was right - you should name the columns explicitly before naming the values.

In this case though the values will be inserted in the order that they appear in the table definition.

The problem with this is that if that order changes, or columns are removed or added (even if they are nullable), then the insert will break.

In terms of its usefulness, not that much in production code. If you're hand coding a quick insert then it might just help save you typing all the column names out.

这篇关于不指定列的SQL INSERT。怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆