codescribblr
codescribblr

Reputation: 1176

How would I add a foreign key in mysql for a column that has unrelated data?

I have two tables that need to be related--a users table and an address_book table I want each address book entry to be associated with a user_id so that when I delete a user, their address book entries get deleted. But I also have about 10000 entries in the address book that are shared among all users. The user id field for each of those entries is set to 0. There is no user with an id of 0. MySQL won't let me create a foreign key for that field if there's a broken child/parent relationship there. How would I go about setting a foreign key in the address_book table for all users except with an id of 0?

Upvotes: 1

Views: 54

Answers (1)

stew
stew

Reputation: 11366

have the user_id of the shared addresses be NULL instead of 0.

Upvotes: 5

Related Questions