开发者

foreign keys without on delete cascade

开发者 https://www.devze.com 2023-01-31 19:23 出处:网络
I have two tables: Challenge: challengeID INTEGER PRIMARY KEY ... And UserChallenge: ID INTEGERPRIMARY KEY challengeID INTEGER NOT NULL

I have two tables:

Challenge:

challengeID INTEGER PRIMARY KEY
...

And

UserChallenge:

ID INTEGER  PRIMARY KEY
challengeID INTEGER NOT NULL
...
FOREIGN KEY(challengeid) REFERENCES challenge(challengeID)
开发者_StackOverflow中文版

If I have one row on challenge table, with challengeID = 1, and one row on UserChallenge table with challengeID = 1.

What would happen with UserChallenge table if I delete the row from the Challenge? May I use ON DELETE CASCADE with UserChallenge.challengeID?

Thanks.


You could do a test...

If you delete the row from Challenge, the constraint on challengeID in UserChallenge gets violated. You have to delete all referencing records in this table as well, or update these records using another (correct) value or use a NULL. You could also use ON DELETE CASCADE when you want to delete the referencing records.


With DELETE CASCADE removing a record from Challenge causes all child records in UserChallenge to be deleted.

Maybe this helps: http://en.wikipedia.org/wiki/Foreign_key#Referential_Actions

0

精彩评论

暂无评论...
验证码 换一张
取 消