0

I have this table:

create table teams (team char(1) primary key, players text);

insert into teams('A', 'Jhon');
insert into teams('B', 'Mark');

Now, how do I add the player 'Carl' in team 'A'?

The column 'players' maybe like a list?

3

5 Answers 5

2

You would do:

insert into teams('A', 'Carl');

after you remove the primary key constraint.

Actually, what you really want is:

create table TeamPlayers (
    TeamPlayerId int auto_increment,
    team char(1),
    players text
);

Then you do the inserts that you want. This is a junction table (sort of). It suggests that you also want a Teams table with one row per team and a Players table with one row per player. Depending on the application, those tables may not be necessary.

Sign up to request clarification or add additional context in comments.

Comments

1

You have made team as primary key so you can't duplicate it.

create some column id make it the primary key then you can add as many members as you want to team A

Comments

1

This should add Carl:

update teams set players =concat(concat(players,','),'Carl') where team='A' 

But it is not a good database design.

Comments

0

What i suppose you want:

update teams set players = players + ', Carl' where team='A'

better if you keep team and player in separate tables

and use team_id in players table.

Comments

0

Two way you can do,

one :

By append the new player with old player with cama seprated

update teams set players =concat(concat(players,','),'Carl') where team='A'

Two :

Don't use primary key for team field and add one auto increment fiend with primary key

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.