主鍵或唯一鍵失敗是尋找重複項,如果您有空值,那麼您需要先排序它們。
E.g給出MyTable(KeyField int not null)
然後
Select KeyField From MyTable
inner join (Select KeyField,Count() as NumberOfTimes Group By KeyField) Duplicates
Where NumberOfTimes > 1
那麼你就必須拿出東西來與他們無關。刪除或重新密鑰。
外鍵只是一個外部聯接與其中key是空
e.g鑑於MyTable (KeyField int not null, ForeignKeyField int not null)
和 MyLookUpTable(LookUpkey int not null, Description VarChar(32) not null)
然後
Select KeyField From MyTable
Left Join MyLookUpTable On MyTable.LookUpField = MyLookUpTable.LookUpKey
Where MyTable.LookUpField Is Null
同樣,你就必須決定如何處理他們做查詢。你可以刪除它們,但這可能有幫助。 一種方法是在查找表中插入「缺失」記錄,抓住它的關鍵,然後用連接進行更新。所以鑑於這個關鍵是999
Update m
Set LookUpField = 999
From MyTable m
Left Join MyLookUpTable On m.LookUpField = MyLookUpTable.LookUpKey
Where m.LookUpField Is Null
現在你可以挖掘999s和在你的閒暇時間處理它們。
你是指在約束條件下哪些行會失敗? –
是@TonyHopkinson受影響的行顯示某種警告和那些會失敗的行。 –