i have two tables Brand and Store
A brand can be sold in multiple stores and a store can sell multiple brands.
I have a bridge table brand_store.
There is a parent store table called StoreGroup.
This storegroup will contain all stores that belongs to a parent. For eg: All stores of KFC will be mapped to a single parent in the store_group table.
Now I have to create a User table where a user can play multiple roles.
if the user is an admin then he has to be tied to the storegrp if the user is a storeowner then he has to be tied to a particular store.
The merchant table looks like"
Merchant
id
login_email_id
login_password
store_or_store_group_id
role_id
....
Scenario:
Each KFC head can appoint an admin, a storemanager etc.
In the merchant table, if the role id is admin then it should be tied to KFC head in store_group table, but if the role id is store manager then it should be tied to a particular store of KFC in store table.
how do i handle this scenario. is this the right way of doing ?
If you are using InnoDB, and you want MySQL to enforce foreign key constraints, you'd need to create two separate columns in
merchant
table, rather than the single column.There's no declarative constraint to prevent both columns from being populated, of you want the database to enforce that, you'd need to create a BEFORE INSERT and BEFORE UPDATE trigger.
If you go with the single combined column, then you won't be able to define a foreign key constraint. You'll also need a way to determine whether the value stored in that column is a reference to the
Store
table or theStoreGroup
table (or, is that the purpose of therole_id
column?)