简体   繁体   English

SQL Server数据库中运行时条目期间的参照完整性约束

[英]referential integrity constraint during runtime entries in sql server database

Ii have two forms in which data is being added into two tables. ii有两种形式,其中数据被添加到两个表中。 One table has a FK which refers to the other table primary key. 一个表具有一个FK,该FK引用了另一个表的主键。 In the second form I insert values in to the form and want to store it in the db. 在第二种形式中,我将值插入到形式中,并希望将其存储在数据库中。

I get the following error 我收到以下错误

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ StuDetail _eduid__5AA469F6". INSERT语句与FOREIGN KEY约束“ FK_ StuDetail _eduid__5AA469F6”冲突。 The conflict occurred in database "Dunstan_DB", table "dbo.EduDetails", column 'eduid'. 数据库“ Dunstan_DB”的表“ dbo.EduDetails”的“ eduid”列中发生了冲突。 The statement has been terminated. 该语句已终止。

How can this be resolved? 如何解决? How do we know during runtime that this value present in parent table as primary key or not? 我们如何知道在运行时该值是否作为主键出现在父表中?

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. 在SQL Mangement Studio中,有一个称为探查器的工具(可能不是快速版本),它使您可以查看发送到数据库的确切查询。 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. 但是,可以肯定的是,要发送到明细表的值在主表中不包含pk-fk值。 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 在更新/插入细节时中断程序,只需运行查询

Select * from MasterTable WHERE ID = PassedFK 

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

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