My question regards how to design a database.
I have one table, called posts, with columns:
ID, subject, keywords, (and a few other columns)
and another table called keywords with:
kw_id, keyword.
Now, each “post” has several keywords and, sometimes, keywords are deleted because they don’t make sense or are duplicates.
My question is:
-
Can keywords column in table posts be a foreign key? (each row will have multiple keywords)
-
If I can’t, what is the best way to ensure data integrity (specially when a keyword is deleted)?
thanks in advance
EDIT: Can you point me any books or documents I should read about database design? It seems I’m laking key knowledge about database design.
You must flip dependencies: Table KEYWORD should reference back to post. You may want someting in between to find all posts for a given keyword (pseudo code):
Now you can easily delete a keyword from a given post by just removing the relationship in
POSTKEYWORDREL.EDIT: As always, for documentation let me point you to Wikipedia. You should also have a look at normalization (in my opinion the most important concept when it comes to database design).