Foreign key preventing row deletion

TheWizardofInt

Well-known member
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
I have a Table called GridGroups with a primary field GridGroupID

There is a secondary table called GridTrans which also has a field, GridGroupID, which is not primary.

This second table uses a ForeignKey relationship to the primary for the two GridGroupIDs. Dont ask me why - not my database, and I dont like Foreign Keys for just this reason.

Now I want to delete the GridGroups, so (and correct me if I am wrong) I should:

1. Delete all of the rows in GridTrans with this GridGroupID
2. THEN delete the one row in GridGroups with that GridGroupID

I can do (1), (2) throws this error:

Integrity constraint violation: Primary key for row in table GridGroups is referenced by foreign key...

Have I missed a step here? This, by the way, is a Sybase database
 
If you can/may change the database and the FK, then you should set the FK delete rule to cascade.
Cascade means that when you want to delete/update the row in primary table then the rows are updated/deleted from the tables with foreign key too. In your case you should then delete only from table GridGroups and matched rows from GridTrans would deleted automaticly.

Other option, if you cant change the FK rule...
You dont say in your error with which table the integrity constraint violation appears? I mean there may be other tables with FKs to GridGroups table. If so, then you cant delete from GridGroups unless you first delete all matching rows from all other tables with relations.
 
Back
Top