Collection_ ( #Id_Collection, Libelle_Collection, Id_Editeur_Editeur )
Editeur ( #Id_Editeur, Libelle_Editeur )
兩個表當我想從表「Editeur」刪除一些記錄,它給了我這個錯誤:
The DELETE statement conflicted with the REFERENCE constraint "FK_Collection_Id_Editeur_Editeur". The conflict occurred in database "Gestion_bib", table "dbo.Collection_", column 'Id_Editeur_Editeur'. The statement has been terminated.
我知道錯誤,因爲在Collection_表的外鍵是在Editeur表中的PK,我用的是瀑布聲明另一個數據庫,但我不得不與它的問題,所以我不要」不喜歡使用它也使用另一種方法使用ADO.Net:
for (int i = 0; i < myClass.ds.Tables["Collection_"].Rows.Count; i++)
if (valToDelete == myClass.ds.Tables["Collection_"].Rows[i][2].ToString())
myClass.ds.Tables["Collection_"].Rows[i][2] = DBNull.Value;
SqlCommandBuilder cmb = new SqlCommandBuilder(adapter);
adapter.Update(myClass.ds, "Editeur");
但是,當我處理包含許多FK的表時,這種方法變得越來越複雜。
那麼,如果我刪除了包含PK的表格,請問是否有其他方法將FK更改爲Null值?
一個可能級聯操作是'ON DELETE SET NULL'的。這是你說你不想使用的選項嗎? –
你真的想孤兒你的數據嗎?這對其他查詢中的連接將不再起作用的影響是什麼? – Jodrell
不需要觸發器。正如@Martin所說的,你可以用'ON DELETE SET NULL'選項來定義FK。 –