i want my database to support one company haveing many users how can i do that?
example
users table(UID,username,password)
company table(CID,companyname,usersthatistheownerofthecompany)
how can i do that? what should i do ? should i make an array in php like 1241,423,4123 *uid’s that will be inserted on usersthatistheownerofthecompany row ? or is there any better idea ?
If you want each user to have one (and never more) company, you should have :
usertableuidusernamecompany_idcompanytablecompany_idcompany_nameThen,
user.company_idwould be a foreign key, that referencescompany.company_id.And, then, you store :
userfor each usercompany_idof the right line in thecompanytable.companyThere is no user’s related information stored in the company table — and as each user “points” to a company, a company can have several users.
Storing data as an array like you suggested is definitely not quite a good idea — just not the way a relational database works.
If each user can have several companies, and each comparny can have several users, you’ll have to add a third table (a join table), that will indicate, for each user(s), to which company(ies) they are attached to :
usertableuidusernamecompanytablecompany_idcompany_nameuser_companytableuidcompany_idIn this situation, there is no user-related stuff in the
companytable, and there is no company-related stuff in theusertable : the link between those is in theuser_companytable.Of course,
user_company.uidwill be a foreign-key touser.uid; anduser_company.company_idwill be a foreign-key tocompany.company_id.