简体   繁体   中英

How to insert multiple values into one column?

I'm setting up a database for a game server and I'm new to databases, I've encountered a problem where I have to insert multiple players into the same column , basically I have a group and in that group I need to insert multiple players, so I have these columns at the moment:

Group Name:
Group Owner:
Players:
Creation Date:

How would I go about adding multiple players into a group? Would I need to just insert all of them into the same column and use String.split() in Java? Or is there a better way of doing this?

I'm going to assume that you are setting up a relational DB. To be blunt, you don't want to insert multiple values into one column. You want to design your tables based on the data and their relationships to each other. Right now, you seem to have two entities: Groups and Players. The relationship beween these two is that a Player can belong to a Group, correct? So then, you want to have a table for each entity and link them based on a key that represents their relationship:

Group:
 - group id (PRIMARY KEY)
 - name
 - owner
 - creation date

Player:
 - player id (PRIMARY KEY)
 - (any other player info you may need, like a name)
 - group id (FOREIGN KEY)

A primary key is a unique identifier for your table - it identifies one and only one entry. A foreign key references one and only one entry in another table (in this case, Group). The above design works if players can belong to one and only one group . If players can belong to multiple groups, you need another table to represent this relationship:

Group:
 - group id (PRIMARY KEY)
 - name
 - owner
 - creation date

Player:
 - player id (PRIMARY KEY)
 - (any other player info you may need, like a name)

PlayerGroup:
 - player id (FOREIGN KEY)
 - group id (FOREIGN KEY)

This design is now relational and normalized . You can get the data in your original design (or near to it) via queries (especially with joins). Maybe this explains a little better: https://www.ntu.edu.sg/home/ehchua/programming/sql/Relational_Database_Design.html

If possible to redesign the DB I would suggest to normalize it a bit and not store everything in a single table. For example you could have : A Player table, storing all players (ef First/Last name, a unique ID etc) Then a Group table storing Group Name , owner , creation date and a unique group ID. And finally a table with Player & Group links that would store the player ID and the group ID for each Player<>Group relationship.

From such structure you can easily get any info you want via SQL (eg from java). For example to get all players for a given group :

select p.player_name from Player_table p
join Player_Group_table pg on p.player_ID = pg.player_ID
join Group_table g on pg.group_ID = g.group_ID
where g.group_name = 'Your Group Name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM