User owning multiple items in mysql database -
so have few tables in database setup , i'm wondering best way is.
essentially have 1-to-many relationship single user can "own" multiple items, represented array in php, return unique item number, type, , values of item. how go storing in mysql table?
would best have lookup table of every single item along it's id , present owner? make more sense put line item in user table had csv list of every item owned every player (that nightmare index?)
what makes sense here?
example:
class character contains items[]
character 1 has following entries in items list:
1, 2, 5, 10,11,12
character 2 has following entries in items list:
3,4,6,7,8,9,13
what decent way store data?
thanks,
never, ever store delimited string values in database. normalize data creating many-to-many table instead. way you'll able query data.
that being said schema might like
create table characters ( char_id int not null auto_increment primary key, name varchar(32), ... ) engine = innodb; create table items ( item_id int not null auto_increment primary key, name varchar(6), ... ) engine = innodb; create table item_list ( char_id int not null, item_id int not null, primary key (char_id, item_id), foreign key (char_id) references characters (char_id), foreign key (item_id) references items (item_id) ) engine = innodb;
if later need produce delimited list of items per character can explode
values while iterating on resultset can use query this
select char_id, group_concat(item_id) items item_list -- ... group char_id
here sqlfiddle demo
Comments
Post a Comment