sqlite3 - SQL foreign key mutex -
i not know know how name question because i'm still learning sql. use sqlite3 simple tests , problem following:
i have 2 tables primary key. first table auto increments key on insert , second references key. let's inserted first table , after insert second table previous inserted key. have foreign key constraint. works let's before start insert second table thread deletes key first table , inserts key. have same primary key integer value (i've read in sqlite documentation). data inconsistent.
what solution case? wrapping 2 inserts in transaction here?
if you've created foreign key, insert fail error if key didn't exist in first table. that's point of creating fk relationships: ensures data remains consistent.
similarly, if insert rows both tables , try delete row 'parent' table, throw error unless you've set cascading deletes, delete related rows in second, 'child', table. either way, consistency preserved.
Comments
Post a Comment