| Author |
Topic |
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2006-11-08 : 15:13:34
|
| Hi, all,I tried to run this next DELETE and got the error below.delete from dbo.account where left(account_number, 1) = 'P'DELETE statement conflicted with COLUMN REFERENCE constraint 'fk_user_order'. The conflict occurred in database 'QWright', table 'UserOrder', column 'UserId'.The statement has been terminated.My problem is that I could not find this 'fk_user_order' either on account or UserOrder. I was thinking of running a drop constraint.I have this trigger on the table, but fk_user_order is no where to be found on the UserIDMaster.CREATE TRIGGER wt_td_account_instead on dbo.account INSTEAD of DELETEASBEGINSET NOCOUNT ONDECLARE @old_account_number varchar(20), @old_account_type varchar(2)SELECT @old_account_number = account_number, @old_account_type = account_typeFROM deleted-- Delete ORDERS FIRSTDELETEFROM order_headerWHERE account_number = @old_account_number-- Then USERS that are CONTACTS of this ACCOUNTDELETEFROM UserIdMasterFROM UserAccount AS aWHERE a.account_number = @old_account_numberAND UserIdMaster.userId = a.UserIdAND UserIdMaster.UserTypeCode = @old_account_type-- Lastly the actual ACCOUNTdelete dbo.accountfrom dbo.account t2, deleted t1where t2.account_number = t1.account_number--end of triggerBTW, this operation will first apply to test data then run on production data during the night one time only. |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2006-11-08 : 15:18:18
|
| is DISABLE TRIGGER a good option? |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-08 : 16:02:06
|
If you run this, do you get any rows back?sp_fkeys @fktable_name = [UserOrder] |
 |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2006-11-08 : 16:52:46
|
| Yes, it did return two rows. One is related here (I guess).PK tbl =UserIdMaster PK col=UserId FK tbl= UserOrder FK col= UserId delete rule=1FK name=fk_user_orderPK name=PK__UserIdMaster__74AE54BCThe problem is I did not find their connection(s) with the table I am dealing with: account. |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-08 : 17:24:10
|
| Foreign keys are defined in the referencing table, not the primary key table. You need to delete the rows from UserOrder before you can delete the rows from Account - or you can drop the constraint (from the UserOrder table) and get corrupt data. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2006-11-09 : 09:25:07
|
| Thanks!I will try Michael's script later today.I think my issue is that from my account table, I did not find any ref to UserOrder. There must be some kind of three-way relationship that I have not found. |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-09 : 10:35:11
|
quote: Originally posted by Hommer Thanks!I will try Michael's script later today.I think my issue is that from my account table, I did not find any ref to UserOrder. There must be some kind of three-way relationship that I have not found.
No, I already told you - there is nothing on your account table because relationships are established with a foreign key constraint on the child table. You can look all you like, but there is nothing on the account table and certainly no "kind of three-way relationship", you have simply got a foreign key constraint on the UserOrder table, exactly like the error message says. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-11-09 : 14:04:07
|
| You can lead a horse to water...CODO ERGO SUM |
 |
|
|
|