2012-03-20 149 views
-1

什麼可能導致FK錯誤?我將一個'Activity'記錄插入到一​​個數據庫中,該數據庫上有一個'StaffId'字段(FK with Staff table),我查找了有問題的staffId(沒有空格等),它存在。還有什麼可以導致與外鍵字段錯誤?外鍵約束問題

編輯:錯誤:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Activities_Staff". The conflict occurred in database "DataWarehouseB", table "dbo.Staff", column 'StaffId'. The statement has been terminated.

+1

另一種關係呢?你可能會發布你的DDL,代碼和錯誤。 – 2012-03-20 17:13:06

+1

檢查觸發器 – HLGEM 2012-03-20 17:15:54

+3

您能顯示**確切且完整的**錯誤信息嗎?另外:**什麼***數據庫和哪個版本? – 2012-03-20 17:26:02

回答

2

SQL Foreign Key文件說

If the database schema contains foreign key errors that require looking at more than one table definition to identify, then those errors are not detected when the tables are created. Instead, such errors prevent the application from preparing SQL statements that modify the content of the child or parent tables in ways that use the foreign keys. Errors reported when content is changed are "DML errors" and errors reported when the schema is changed are "DDL errors". So, in other words, misconfigured foreign key constraints that require looking at both the child and parent are DML errors. The English language error message for foreign key DML errors is usually "foreign key mismatch" but can also be "no such table" if the parent table does not exist. Foreign key DML errors are may be reported if:

  • The parent table does not exist.
  • The parent key columns named in the foreign key constraint do not exist.
  • The parent key columns named in the foreign key constraint are not the primary key of the parent table and are not subject to a unique constraint using collating sequence specified in the CREATE TABLE.
  • The child table references the primary key of the parent without specifying the primary key columns and the number of primary key columns in the parent do not match the number of child key columns.

有些數據塊也可能支持使用non-unique index as a foreign key reference