sql server数据库中运行时条目期间的引用完整性约束

时间:2021-09-10 09:14:59

Ii have two forms in which data is being added into two tables. One table has a FK which refers to the other table primary key. In the second form I insert values in to the form and want to store it in the db.

Ii有两种形式,其中数据被添加到两个表中。一个表有一个FK,它引用另一个表主键。在第二个表单中,我向表单中插入值,并希望将其存储在db中。

I get the following error

我得到以下错误

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_StuDetail_eduid__5AA469F6". The conflict occurred in database "Dunstan_DB", table "dbo.EduDetails", column 'eduid'. The statement has been terminated.

插入语句与外键约束“FK_StuDetail_eduid__5AA469F6”冲突。冲突发生在数据库“Dunstan_DB”、表“dbo”中。EduDetails”,列“eduid”。声明已被终止。

How can this be resolved? How do we know during runtime that this value present in parent table as primary key or not?

如何解决这个问题?如何在运行时知道父表中的这个值是否为主键?

1 个解决方案

#1


0  

Within SQL Mangement studio, there is a tool called profiler (may not be in express version) it allows you to see the exact queries that are being sent to the database. However its pretty certain that the value you are sending to the detail table does not contain the pk-fk value in the master table. You need to ensure the master table is inserted to before the detail. Break your program when you are updating / inserting the detail , and simply run the query

在SQL Mangement studio中,有一个名为profiler的工具(可能不是express版本),它允许您查看发送到数据库的确切查询。然而,可以肯定的是,您发送到明细表的值在主表中不包含pk-fk值。您需要确保在细节之前插入主表。在更新/插入细节时中断程序,并简单地运行查询

Select * from MasterTable WHERE ID = PassedFK 

#1


0  

Within SQL Mangement studio, there is a tool called profiler (may not be in express version) it allows you to see the exact queries that are being sent to the database. However its pretty certain that the value you are sending to the detail table does not contain the pk-fk value in the master table. You need to ensure the master table is inserted to before the detail. Break your program when you are updating / inserting the detail , and simply run the query

在SQL Mangement studio中,有一个名为profiler的工具(可能不是express版本),它允许您查看发送到数据库的确切查询。然而,可以肯定的是,您发送到明细表的值在主表中不包含pk-fk值。您需要确保在细节之前插入主表。在更新/插入细节时中断程序,并简单地运行查询

Select * from MasterTable WHERE ID = PassedFK