database design - MySQL: Do I need a foreign key? -


i have table called "peoplecategory" , includes peoplecategoryid, description

i have 2 records added

1, customers 2, partners 

then have table "people" , includes peopleid, peoplecategoryid, name

some records:

1, 1, george 2, 1, john 3, 2, nick 

the peoplecategoryid tell kind of people defined.

do need foreign key? tried code:

alter table peoplecategories add constraint fk_test foreign key (peoplecategoryid) references people(peoplecategoryid) on update cascade on delete cascade; 

but fails error 150.

any ideas?

if going add foreign key, should in opposite direction:

alter table people   add constraint fk_test   foreign key (peoplecategoryid) references peoplecategory(peoplecategoryid)   on update cascade   on delete cascade; 

that way, people depend on category , can't add people without corresponding category.

you getting error because column not primary key in people table.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -