Subject Re: [firebird-support] Foreign Key violation on restore
Author Helen Borrie
At 12:26 PM 1/02/2005 +0000, you wrote:



>Hello!
>
>I have a database in FirebirdSS-1.0.3.972-0
>
>when i tried to restore this database, i cannot restore, one foreign
>key violation error is coming, but the database is working perfectly
>
>i have identified that there is invalid data in foreign key column
>defined table.
>
>1) How these problems are coming?

Firebird won't let you insert or update data that will break a foreign key
relationship; but possibly the foreign key constraint was added to a
nullable column that already contained nulls, or orphan keys, without first
checking for potential violations. Trying to change metadata by updating
the system tables directly (as some database tools do) can also create this
inconsistency.

>2) Can we restore such a database ?

First, you need to resolve the row or rows that cause the violation on
restore.

Try querying the two tables involved, in order to find the offending
"child" records (the ones having the bad foreign key values). Assuming
child is the table with the foreign key referencing parent:

select c.childid, c.parentid, p.parentid
from child c
left join parent p
on c.parentid = p.parentid
where p.parentid is null

should produce a set of the "orphan" children.

./hb