Question

I have two tables like so:

Table1
| itemid (PK)|    
| typeid (FK)|     
| item count |     

Table2        
| typeid (PK)|  
| type name  |

Table2 have 4-5 records in it. ...And I'm stuck at this point... Basically the item can be of all types (it can have typeid (FK) = NULL or all 4-5). I just can't get my head round this..At the moment when I add new item to Table1 (using phpmyAdmin ui) it gives me a choice of one FK as a dropdown list.. But what if the item is of two or three types..? It's probably a piece of cake but my brain has stalled... What would be the best way to do it?

Was it helpful?

Solution

Your design support 1-to-many relationship. Perhaps, you need many-to-many relationship between Table1 and Table2. If yes, you need to add a new link table:

Table3:
itemid(FK) 
typeid(FK)

OTHER TIPS

You can define a composite key on itemid and typeid in Table1.

That allows combinations of 1-1, 1-2, 3-1, 3-4, etc on itemid and typeid values.

Even your question is not much clear, but what I am understanding that you are not able to insert records in child table (table1) as record does not exist in master table (table 2).

So first you need to add required typeid in master table (table2) then you can enter in its child table.

If your requirement is something different then elaborate, so that you can get better help from community.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top