SQL Management Studio插入语句冲突

发布于 2024-09-30 03:21:04 字数 746 浏览 2 评论 0原文

我正在为我的老板创建一个开发数据库。我有创建所有表的 SQL 脚本,但现在我有大量数据需要导入。数据库中共有 5 个表,它们的 ID 彼此相关,如下所示:

注意:我不允许共享数据库/表名称等信息

Table1
+-- Table2
    +-- Table3
        +-- Table4
            +-- Table5

这意味着表 5 code> 有一个指向 Table 4 的 ID 的字段,依此类推。

所以我正在做一堆这样的插入:

INSERT INTO [dbo].[Table2] (Table1_ID, AnotherField) VALUES (159268, 408659)

但它会为每个插入抛出此异常:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TABLE2_TABLE1". The conflict occurred in database "--removed by author--", table "dbo.TABLE1", column 'TABLE1_ID'.

编辑: 所以问题是我需要先将数据插入Table1中。所以我的老板找到了一些足以满足表1的数据...所以问题解决了:)

I'm working on creating a development database for my boss. I have the SQL script that creates all the tables, but now I have loads of data to import. There are 5 tables total in the database, and their ID's are related to eachother like so:

Note: I'm not allowed to share info like database/table names

Table1
+-- Table2
    +-- Table3
        +-- Table4
            +-- Table5

Meaning that Table 5 has a field that points to a Table 4's ID, and so on.

So I'm doing a bunch of inserts like this:

INSERT INTO [dbo].[Table2] (Table1_ID, AnotherField) VALUES (159268, 408659)

But it throws this exception for each one:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TABLE2_TABLE1". The conflict occurred in database "--removed by author--", table "dbo.TABLE1", column 'TABLE1_ID'.

EDIT:
So the problem was that I needed to insert the data in Table1 first. So my Boss found some data that will suffice for Table1... So Problem solved :)

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

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

发布评论

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

评论(3

冷血 2024-10-07 03:21:04

没有什么可添加的——错误消息非常清楚。

您正在尝试将 ID 值插入到 Table2 中,但该 ID 值在 Table1 中不存在。因此出现错误消息。

您需要先将数据插入表 1,然后再插入表 2

编辑:

如果表 1 没有数据,则数据库约束考虑不周。您必须从 Table2 中删除约束。

然后您必须向他们询问 Table1 的数据。

There isn't really much to add - the error message is pretty clear.

You are trying to insert an ID value in to Table2 however the that ID value doesn't exist in Table1. Hence the error message.

You need to insert the data in to Table1 before you insert in to Table2

EDIT:

If there is no data for Table1 then the database constraints are poorly thought out. You will have to remove the constraint from Table2.

You will have to ask them for the data for Table1 then.

旧人哭 2024-10-07 03:21:04
  1. 您应该执行自上而下的导入:首先导入 Table1 中的所有记录,然后导入 Table2 等。
  2. 或者,为了使事情变得更容易并获得更快的体验,您可以在执行之前禁用所有约束,并在完成后重新启用它们。请看这里:如何临时外键约束使用 T-SQL 禁用?

编辑:

好吧,如果您没有 Table1 数据,您所能做的就是删除引用 Table1 的所有外键约束(可能来自Table2)。或者,您可以尝试生成“假”Table1 数据,考虑 Table2 上可用的所有不同 Table1 ID (aaarrgh)。

  1. You should do a top-down import: first import all records from Table1, then Table2, etc.
  2. Or, to make things easier, and have a faster experience, you could disable all your constraints before executing, and re-enable them after completion. Please, take a look here: How can foreign key constraints be temporarily disabled using T-SQL?

EDIT:

Well, if you don't have Table1 data all you can do is remove all foreign-key constraints that reference Table1 (probably from Table2). Or you could try generating "fake" Table1 data, considering all distinct Table1 IDs available on Table2 (aaarrgh).

热情消退 2024-10-07 03:21:04

您可以暂时禁用外键约束,执行插入,然后使用 CHECKNOCHECK 重新启用它。

WITH NOCHECK CONSTRAINT 禁用约束并允许通常违反约束的插入。

WITH CHECK CONSTRAINT 重新启用约束。

下面是关于如何执行此操作的详细说明

You can temporarily disable the foreign key constraint, do the insert, and then re-enable it using CHECK and NOCHECK.

WITH NOCHECK CONSTRAINT disables the constraint and allows an insert that would normally violate the constraint.

WITH CHECK CONSTRAINT re-enables the constraint.

Here's a detailed explanation of how to do this.

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