关于插入空元组以准备更新语句的 SQL 问题

发布于 2024-10-01 17:44:22 字数 129 浏览 6 评论 0原文

我正在制作一个表格,该表格将从另一个表格借用几列,但不是全部。现在我的新桌子上没有任何东西。我想将 X 个空元组添加到我的表中,以便我可以开始通过更新语句将旧表中的数据添加到新表中。

这是最好的方法吗?添加空行的语法是什么?谢谢

I am making a table that will be borrowing a couple of columns from another table, but not all of them. Right now my new table doesn't have anything in it. I want to add X number of empty tuples to my table so that I can then begin adding data from my old table to my new table via update statements.

Is that the best way to do it? What is the syntax for adding empty rows? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期以后 2024-10-08 17:44:22

您不能直接从另一个表插入数据,而不是插入空值然后更新它们。使用类似的东西 -

INSERT INTO Table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM Table2
WHERE <some condition>

如果您仍然想插入空记录,那么您必须确保所有列都允许空值。然后你可以使用类似这样的东西 -

Table1

PrimaryKey_Col | col1 | col2 | col3

Insert INTO Table1 (PrimaryKey_col) values (<some value>)

这将确保你插入一个带有主键的新行,并且其余列为空。并且这些记录可以在以后更新。

Instead of inserting nulls and then updating them, cant you just insert data from the other table directly. using something like this -

INSERT INTO Table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM Table2
WHERE <some condition>

If you still want to insert empty records, then you will have to make sure that all your columns allow nulls. Then you can use something like this -

Table1

PrimaryKey_Col | col1 | col2 | col3

Insert INTO Table1 (PrimaryKey_col) values (<some value>)

This will make sure your a new row is inserted with a primary key and the rest of the columns are nulls. And these records can be updated later.

笑看君怀她人 2024-10-08 17:44:22

不,这连一个好办法都算不上,更不是最好的办法。

如果您从概念上看,添加空行没有任何意义。
在数据库中,表的每一行对应一个真实的陈述(事实)。添加全为 NULL 的行(即使可能)不会记录任何内容,并且其本身表示不一致的数据。特别是有多个空记录。

另外,如果您甚至能够将包含所有 NULL 的行添加到表中,则表明您没有该行的数据完整性规则,这就是数据库的主要内容 - 完整性规则和数据质量。设计良好的数据库不应接受矛盾或无意义的数据(空行毫无意义),

正如 Pavanred 回答的那样,插入真实数据是单个命令,因此将其设置为两个或多个命令没有任何好处。

No, this is not even a good way, let alone the best way.

If you look at it conceptually adding empty rows serves no purpose.
In databases each row of a table corresponds to a true statement (fact). Adding a row with all NULLs (even if possible) records nothing and represents inconsistent data on its own. Especially having multiple empty records.

Also, if you are even able to add a row with all NULLs to a table that's an indication that you have no data integrity rules for the row so and that's mostly what databases are about - integrity rules and quality of data. A well designed database should not accept contradictory or meaningless data (and empty row is meaningless)

As Pavanred answered inserting real data is a single command, so there is no benefit in making it two or more commands.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文