Joining Tables and Data for a Football/Soccer Website
up vote
0
down vote
favorite
Im learning PHP and currently creating a website for a local football club.
They have multiple teams at various age groups. I have created a database of 'team' which has all of the information with regards to team name, which league, team photo, training information. I also have a 'coaches' table which as name, dob, qualifications, contact info. How would i add information to be displayed in a table that link the two.
So in 'coach1' and 'coach2' it will display the name of the two coaches linked to that club.
I am thinking that when i create a new coach (via form) they will be able to select the team from the database via a dropdown box.
what would be the best way to link this together, when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach? Im thinking that i would add an additional drop down that would fill in 'role' column with either Manager or Assistant, i would then have to add field where 'SELECT coach WHERE role = Assistant AND id = $id'
<table class="table">
<thead>
<tr>
<th>Team Name</th>
<th>Training</th>
<th>League</th>
<th>Coaches</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($team))
{
echo '<tr>
<td>'." u".$row['age_group']." ".$row['team_name'].'</td>
<td>'.$row['training_day'].", ".$row['location']." at ".$row['training_time'].'</td>
<td>'.$row['league']." - ".$row['match_day'].'</td>
<td>'.$row['coach1'].'</td>
<td>'.$row['coach2'].'</td>
<td><a href="viewapp.php?id='.($row['team_id']).'" class="btn btn-warning pull-right btn-xs">View</a></td>
</tr>';
$no++;
}?>
</tbody>
</table>
php mysql
|
show 1 more comment
up vote
0
down vote
favorite
Im learning PHP and currently creating a website for a local football club.
They have multiple teams at various age groups. I have created a database of 'team' which has all of the information with regards to team name, which league, team photo, training information. I also have a 'coaches' table which as name, dob, qualifications, contact info. How would i add information to be displayed in a table that link the two.
So in 'coach1' and 'coach2' it will display the name of the two coaches linked to that club.
I am thinking that when i create a new coach (via form) they will be able to select the team from the database via a dropdown box.
what would be the best way to link this together, when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach? Im thinking that i would add an additional drop down that would fill in 'role' column with either Manager or Assistant, i would then have to add field where 'SELECT coach WHERE role = Assistant AND id = $id'
<table class="table">
<thead>
<tr>
<th>Team Name</th>
<th>Training</th>
<th>League</th>
<th>Coaches</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($team))
{
echo '<tr>
<td>'." u".$row['age_group']." ".$row['team_name'].'</td>
<td>'.$row['training_day'].", ".$row['location']." at ".$row['training_time'].'</td>
<td>'.$row['league']." - ".$row['match_day'].'</td>
<td>'.$row['coach1'].'</td>
<td>'.$row['coach2'].'</td>
<td><a href="viewapp.php?id='.($row['team_id']).'" class="btn btn-warning pull-right btn-xs">View</a></td>
</tr>';
$no++;
}?>
</tbody>
</table>
php mysql
By linking, what do you mean? Database side? Back end side? Front end side?
– Cid
Nov 11 at 19:29
i think ERR0 gets my drift. basically 2 database tables which link the coach and the team.
– Andy Terry
Nov 11 at 19:50
Its a many to many situation, so have acoach
(links user as a coach) andteam_coach
(links coach to team) table, then use a JOIN to link them. Many coaches can be on many teams then. I guess you're just using columns and not normalising.
– Lawrence Cherone
Nov 11 at 20:02
Unrelated to your question, but do not concatenate arbitrary data directly into the context of HTML without escaping it first! At best, you'll be introducing invalid HTML. At worst, you open yourself up to some bad security issues. Always usehtmlspecialchars()
around data you're concatenating into HTML.
– Brad
Nov 11 at 20:10
thanks brad, im going to go through all the security once i have the page complete.
– Andy Terry
Nov 11 at 20:18
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im learning PHP and currently creating a website for a local football club.
They have multiple teams at various age groups. I have created a database of 'team' which has all of the information with regards to team name, which league, team photo, training information. I also have a 'coaches' table which as name, dob, qualifications, contact info. How would i add information to be displayed in a table that link the two.
So in 'coach1' and 'coach2' it will display the name of the two coaches linked to that club.
I am thinking that when i create a new coach (via form) they will be able to select the team from the database via a dropdown box.
what would be the best way to link this together, when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach? Im thinking that i would add an additional drop down that would fill in 'role' column with either Manager or Assistant, i would then have to add field where 'SELECT coach WHERE role = Assistant AND id = $id'
<table class="table">
<thead>
<tr>
<th>Team Name</th>
<th>Training</th>
<th>League</th>
<th>Coaches</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($team))
{
echo '<tr>
<td>'." u".$row['age_group']." ".$row['team_name'].'</td>
<td>'.$row['training_day'].", ".$row['location']." at ".$row['training_time'].'</td>
<td>'.$row['league']." - ".$row['match_day'].'</td>
<td>'.$row['coach1'].'</td>
<td>'.$row['coach2'].'</td>
<td><a href="viewapp.php?id='.($row['team_id']).'" class="btn btn-warning pull-right btn-xs">View</a></td>
</tr>';
$no++;
}?>
</tbody>
</table>
php mysql
Im learning PHP and currently creating a website for a local football club.
They have multiple teams at various age groups. I have created a database of 'team' which has all of the information with regards to team name, which league, team photo, training information. I also have a 'coaches' table which as name, dob, qualifications, contact info. How would i add information to be displayed in a table that link the two.
So in 'coach1' and 'coach2' it will display the name of the two coaches linked to that club.
I am thinking that when i create a new coach (via form) they will be able to select the team from the database via a dropdown box.
what would be the best way to link this together, when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach? Im thinking that i would add an additional drop down that would fill in 'role' column with either Manager or Assistant, i would then have to add field where 'SELECT coach WHERE role = Assistant AND id = $id'
<table class="table">
<thead>
<tr>
<th>Team Name</th>
<th>Training</th>
<th>League</th>
<th>Coaches</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($team))
{
echo '<tr>
<td>'." u".$row['age_group']." ".$row['team_name'].'</td>
<td>'.$row['training_day'].", ".$row['location']." at ".$row['training_time'].'</td>
<td>'.$row['league']." - ".$row['match_day'].'</td>
<td>'.$row['coach1'].'</td>
<td>'.$row['coach2'].'</td>
<td><a href="viewapp.php?id='.($row['team_id']).'" class="btn btn-warning pull-right btn-xs">View</a></td>
</tr>';
$no++;
}?>
</tbody>
</table>
php mysql
php mysql
edited Nov 11 at 21:09
asked Nov 11 at 19:24
Andy Terry
246
246
By linking, what do you mean? Database side? Back end side? Front end side?
– Cid
Nov 11 at 19:29
i think ERR0 gets my drift. basically 2 database tables which link the coach and the team.
– Andy Terry
Nov 11 at 19:50
Its a many to many situation, so have acoach
(links user as a coach) andteam_coach
(links coach to team) table, then use a JOIN to link them. Many coaches can be on many teams then. I guess you're just using columns and not normalising.
– Lawrence Cherone
Nov 11 at 20:02
Unrelated to your question, but do not concatenate arbitrary data directly into the context of HTML without escaping it first! At best, you'll be introducing invalid HTML. At worst, you open yourself up to some bad security issues. Always usehtmlspecialchars()
around data you're concatenating into HTML.
– Brad
Nov 11 at 20:10
thanks brad, im going to go through all the security once i have the page complete.
– Andy Terry
Nov 11 at 20:18
|
show 1 more comment
By linking, what do you mean? Database side? Back end side? Front end side?
– Cid
Nov 11 at 19:29
i think ERR0 gets my drift. basically 2 database tables which link the coach and the team.
– Andy Terry
Nov 11 at 19:50
Its a many to many situation, so have acoach
(links user as a coach) andteam_coach
(links coach to team) table, then use a JOIN to link them. Many coaches can be on many teams then. I guess you're just using columns and not normalising.
– Lawrence Cherone
Nov 11 at 20:02
Unrelated to your question, but do not concatenate arbitrary data directly into the context of HTML without escaping it first! At best, you'll be introducing invalid HTML. At worst, you open yourself up to some bad security issues. Always usehtmlspecialchars()
around data you're concatenating into HTML.
– Brad
Nov 11 at 20:10
thanks brad, im going to go through all the security once i have the page complete.
– Andy Terry
Nov 11 at 20:18
By linking, what do you mean? Database side? Back end side? Front end side?
– Cid
Nov 11 at 19:29
By linking, what do you mean? Database side? Back end side? Front end side?
– Cid
Nov 11 at 19:29
i think ERR0 gets my drift. basically 2 database tables which link the coach and the team.
– Andy Terry
Nov 11 at 19:50
i think ERR0 gets my drift. basically 2 database tables which link the coach and the team.
– Andy Terry
Nov 11 at 19:50
Its a many to many situation, so have a
coach
(links user as a coach) and team_coach
(links coach to team) table, then use a JOIN to link them. Many coaches can be on many teams then. I guess you're just using columns and not normalising.– Lawrence Cherone
Nov 11 at 20:02
Its a many to many situation, so have a
coach
(links user as a coach) and team_coach
(links coach to team) table, then use a JOIN to link them. Many coaches can be on many teams then. I guess you're just using columns and not normalising.– Lawrence Cherone
Nov 11 at 20:02
Unrelated to your question, but do not concatenate arbitrary data directly into the context of HTML without escaping it first! At best, you'll be introducing invalid HTML. At worst, you open yourself up to some bad security issues. Always use
htmlspecialchars()
around data you're concatenating into HTML.– Brad
Nov 11 at 20:10
Unrelated to your question, but do not concatenate arbitrary data directly into the context of HTML without escaping it first! At best, you'll be introducing invalid HTML. At worst, you open yourself up to some bad security issues. Always use
htmlspecialchars()
around data you're concatenating into HTML.– Brad
Nov 11 at 20:10
thanks brad, im going to go through all the security once i have the page complete.
– Andy Terry
Nov 11 at 20:18
thanks brad, im going to go through all the security once i have the page complete.
– Andy Terry
Nov 11 at 20:18
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Since it's DB design only, let's explode the problem.
You have coaches and teams.
Case 1 : A coach can belong to 1 team
A unique coach belongs to a unique team (this is a 1,1 relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, 1)-> [coach_team] <-(1, n)- [team]
In this kind of relation, the foreign key of the team will be stored in the coach
table
As exemple, I have 2 teams, foo and bar. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar.
Let's consider the table team
has 2 fields, id
and name
and the table coach
has 3 fields, id
, name
and team_id
. We can build the tables this way :
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar");
INSERT INTO coach VALUES (default, "John Smith", 1), (default, "Jane Doe", 1), (default, "Zinedine Zidane", 2);
How do I know who are the coaches of the team foo ? Using a JOIN
on the foreign key :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE t.name = "foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE c.name = "Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
You can try it yourself on DB Fiddle
Case 2 : A coach can belong to n team
A unique coach belongs to one or many teams (this is a 1,n relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, n)-> [coach_team] <-(1, n)- [team]
In this kind of relation, a new table coach_team
is needed, where both foreign keys of the coach and the team will be stored. To avoid duplication of the couples coach/team, I prefer declaring the PRIMARY KEY
as a composition of both foreign keys.
As exemple, I have 3 teams, foo, bar and team 42. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar and the team 42 has Zidane and Jane as coach.
Coach
id | name
----+-----------------
1 | John Smith
2 | Jane Doe
3 | Zinedine Zidane
Team
id | name
----+--------
1 | foo
2 | bar
3 | team 42
coach_team
coach_id | name_id
----------+---------
1 | 1
2 | 1
3 | 2
2 | 3
3 | 3
Table creations
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach_team
(
coach_id INT(6) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(coach_id) REFERENCES coach(id),
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id),
PRIMARY KEY (coach_id, team_id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar"), (default, "Team 42");
INSERT INTO coach VALUES (default, "John Smith"), (default, "Jane Doe"), (default, "Zinedine Zidane");
INSERT INTO coach_team VALUES (1, 1), (2, 1), (3, 2), (2, 3), (3, 3);
How do I get the coaches of the team foo ? I query the team, JOIN
on the reference table, coach_team
, then another JOIN
on the table coach.
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE t.name="foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE c.name="Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
| Team 42 | Zinedine Zidane |
You can try it yourself on DB Fiddle
thats great Thanks Cid. Just another addidtion question... what if a coached also coached an addition team (so a coach, coaches 2 different teams.)
– Andy Terry
Nov 12 at 14:08
@AndyTerry Can it be the case for your application?
– Cid
Nov 12 at 15:33
@AndyTerry I edited my answer, check case 2
– Cid
Nov 12 at 15:58
Cid that is perfect mate. i appreciate the time and effort into the answer. 2 or 3 coaches in our club do coach mutliple teams within the club. eg steve coaches the under 13s and 15s. so this is exactly what i was needing clarifying.. appreciate it.
– Andy Terry
Nov 12 at 21:27
You're welcome :) Hint : Since a coach can manage many teams, when creating a new coach, a dropdown isn't the best choice (1 team to select) but checkboxes might suits better your needs (multiples teams can be selected)
– Cid
Nov 13 at 7:36
add a comment |
up vote
0
down vote
You could use a foreign key between the two tables to connect the information. In example, add a column named something like team_id in the 'coaches' table. This column should have the id of the team from the 'team' table.
Then, when you register a new coach in a form, you can use the id as the value from the dropdown.
thats exactly what im looking for. As i will be adding players also i would also need to link those to the team table. what would the query look like, i know how to do simple queries, not dealt with joins as of yet.
– Andy Terry
Nov 11 at 19:50
when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach?
– Andy Terry
Nov 11 at 19:57
As I said, you could add a column in the coaches table, so the structure would be something like: name, dob, qualifications, contact info, team_id where the team_id should be the id of the team in the team table. Then in your schema for signing up coaches, you would have a dropdown something like this (fill it with the data from the DB): <select> <option value="team_id_1">Name of team 1</option> <option value="team_id_2">Name of team 2</option> </select> And then you can use the team_id_x value in the INSERT statement
– ERR0
Nov 11 at 22:20
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Since it's DB design only, let's explode the problem.
You have coaches and teams.
Case 1 : A coach can belong to 1 team
A unique coach belongs to a unique team (this is a 1,1 relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, 1)-> [coach_team] <-(1, n)- [team]
In this kind of relation, the foreign key of the team will be stored in the coach
table
As exemple, I have 2 teams, foo and bar. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar.
Let's consider the table team
has 2 fields, id
and name
and the table coach
has 3 fields, id
, name
and team_id
. We can build the tables this way :
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar");
INSERT INTO coach VALUES (default, "John Smith", 1), (default, "Jane Doe", 1), (default, "Zinedine Zidane", 2);
How do I know who are the coaches of the team foo ? Using a JOIN
on the foreign key :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE t.name = "foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE c.name = "Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
You can try it yourself on DB Fiddle
Case 2 : A coach can belong to n team
A unique coach belongs to one or many teams (this is a 1,n relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, n)-> [coach_team] <-(1, n)- [team]
In this kind of relation, a new table coach_team
is needed, where both foreign keys of the coach and the team will be stored. To avoid duplication of the couples coach/team, I prefer declaring the PRIMARY KEY
as a composition of both foreign keys.
As exemple, I have 3 teams, foo, bar and team 42. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar and the team 42 has Zidane and Jane as coach.
Coach
id | name
----+-----------------
1 | John Smith
2 | Jane Doe
3 | Zinedine Zidane
Team
id | name
----+--------
1 | foo
2 | bar
3 | team 42
coach_team
coach_id | name_id
----------+---------
1 | 1
2 | 1
3 | 2
2 | 3
3 | 3
Table creations
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach_team
(
coach_id INT(6) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(coach_id) REFERENCES coach(id),
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id),
PRIMARY KEY (coach_id, team_id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar"), (default, "Team 42");
INSERT INTO coach VALUES (default, "John Smith"), (default, "Jane Doe"), (default, "Zinedine Zidane");
INSERT INTO coach_team VALUES (1, 1), (2, 1), (3, 2), (2, 3), (3, 3);
How do I get the coaches of the team foo ? I query the team, JOIN
on the reference table, coach_team
, then another JOIN
on the table coach.
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE t.name="foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE c.name="Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
| Team 42 | Zinedine Zidane |
You can try it yourself on DB Fiddle
thats great Thanks Cid. Just another addidtion question... what if a coached also coached an addition team (so a coach, coaches 2 different teams.)
– Andy Terry
Nov 12 at 14:08
@AndyTerry Can it be the case for your application?
– Cid
Nov 12 at 15:33
@AndyTerry I edited my answer, check case 2
– Cid
Nov 12 at 15:58
Cid that is perfect mate. i appreciate the time and effort into the answer. 2 or 3 coaches in our club do coach mutliple teams within the club. eg steve coaches the under 13s and 15s. so this is exactly what i was needing clarifying.. appreciate it.
– Andy Terry
Nov 12 at 21:27
You're welcome :) Hint : Since a coach can manage many teams, when creating a new coach, a dropdown isn't the best choice (1 team to select) but checkboxes might suits better your needs (multiples teams can be selected)
– Cid
Nov 13 at 7:36
add a comment |
up vote
0
down vote
accepted
Since it's DB design only, let's explode the problem.
You have coaches and teams.
Case 1 : A coach can belong to 1 team
A unique coach belongs to a unique team (this is a 1,1 relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, 1)-> [coach_team] <-(1, n)- [team]
In this kind of relation, the foreign key of the team will be stored in the coach
table
As exemple, I have 2 teams, foo and bar. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar.
Let's consider the table team
has 2 fields, id
and name
and the table coach
has 3 fields, id
, name
and team_id
. We can build the tables this way :
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar");
INSERT INTO coach VALUES (default, "John Smith", 1), (default, "Jane Doe", 1), (default, "Zinedine Zidane", 2);
How do I know who are the coaches of the team foo ? Using a JOIN
on the foreign key :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE t.name = "foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE c.name = "Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
You can try it yourself on DB Fiddle
Case 2 : A coach can belong to n team
A unique coach belongs to one or many teams (this is a 1,n relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, n)-> [coach_team] <-(1, n)- [team]
In this kind of relation, a new table coach_team
is needed, where both foreign keys of the coach and the team will be stored. To avoid duplication of the couples coach/team, I prefer declaring the PRIMARY KEY
as a composition of both foreign keys.
As exemple, I have 3 teams, foo, bar and team 42. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar and the team 42 has Zidane and Jane as coach.
Coach
id | name
----+-----------------
1 | John Smith
2 | Jane Doe
3 | Zinedine Zidane
Team
id | name
----+--------
1 | foo
2 | bar
3 | team 42
coach_team
coach_id | name_id
----------+---------
1 | 1
2 | 1
3 | 2
2 | 3
3 | 3
Table creations
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach_team
(
coach_id INT(6) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(coach_id) REFERENCES coach(id),
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id),
PRIMARY KEY (coach_id, team_id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar"), (default, "Team 42");
INSERT INTO coach VALUES (default, "John Smith"), (default, "Jane Doe"), (default, "Zinedine Zidane");
INSERT INTO coach_team VALUES (1, 1), (2, 1), (3, 2), (2, 3), (3, 3);
How do I get the coaches of the team foo ? I query the team, JOIN
on the reference table, coach_team
, then another JOIN
on the table coach.
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE t.name="foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE c.name="Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
| Team 42 | Zinedine Zidane |
You can try it yourself on DB Fiddle
thats great Thanks Cid. Just another addidtion question... what if a coached also coached an addition team (so a coach, coaches 2 different teams.)
– Andy Terry
Nov 12 at 14:08
@AndyTerry Can it be the case for your application?
– Cid
Nov 12 at 15:33
@AndyTerry I edited my answer, check case 2
– Cid
Nov 12 at 15:58
Cid that is perfect mate. i appreciate the time and effort into the answer. 2 or 3 coaches in our club do coach mutliple teams within the club. eg steve coaches the under 13s and 15s. so this is exactly what i was needing clarifying.. appreciate it.
– Andy Terry
Nov 12 at 21:27
You're welcome :) Hint : Since a coach can manage many teams, when creating a new coach, a dropdown isn't the best choice (1 team to select) but checkboxes might suits better your needs (multiples teams can be selected)
– Cid
Nov 13 at 7:36
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Since it's DB design only, let's explode the problem.
You have coaches and teams.
Case 1 : A coach can belong to 1 team
A unique coach belongs to a unique team (this is a 1,1 relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, 1)-> [coach_team] <-(1, n)- [team]
In this kind of relation, the foreign key of the team will be stored in the coach
table
As exemple, I have 2 teams, foo and bar. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar.
Let's consider the table team
has 2 fields, id
and name
and the table coach
has 3 fields, id
, name
and team_id
. We can build the tables this way :
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar");
INSERT INTO coach VALUES (default, "John Smith", 1), (default, "Jane Doe", 1), (default, "Zinedine Zidane", 2);
How do I know who are the coaches of the team foo ? Using a JOIN
on the foreign key :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE t.name = "foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE c.name = "Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
You can try it yourself on DB Fiddle
Case 2 : A coach can belong to n team
A unique coach belongs to one or many teams (this is a 1,n relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, n)-> [coach_team] <-(1, n)- [team]
In this kind of relation, a new table coach_team
is needed, where both foreign keys of the coach and the team will be stored. To avoid duplication of the couples coach/team, I prefer declaring the PRIMARY KEY
as a composition of both foreign keys.
As exemple, I have 3 teams, foo, bar and team 42. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar and the team 42 has Zidane and Jane as coach.
Coach
id | name
----+-----------------
1 | John Smith
2 | Jane Doe
3 | Zinedine Zidane
Team
id | name
----+--------
1 | foo
2 | bar
3 | team 42
coach_team
coach_id | name_id
----------+---------
1 | 1
2 | 1
3 | 2
2 | 3
3 | 3
Table creations
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach_team
(
coach_id INT(6) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(coach_id) REFERENCES coach(id),
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id),
PRIMARY KEY (coach_id, team_id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar"), (default, "Team 42");
INSERT INTO coach VALUES (default, "John Smith"), (default, "Jane Doe"), (default, "Zinedine Zidane");
INSERT INTO coach_team VALUES (1, 1), (2, 1), (3, 2), (2, 3), (3, 3);
How do I get the coaches of the team foo ? I query the team, JOIN
on the reference table, coach_team
, then another JOIN
on the table coach.
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE t.name="foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE c.name="Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
| Team 42 | Zinedine Zidane |
You can try it yourself on DB Fiddle
Since it's DB design only, let's explode the problem.
You have coaches and teams.
Case 1 : A coach can belong to 1 team
A unique coach belongs to a unique team (this is a 1,1 relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, 1)-> [coach_team] <-(1, n)- [team]
In this kind of relation, the foreign key of the team will be stored in the coach
table
As exemple, I have 2 teams, foo and bar. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar.
Let's consider the table team
has 2 fields, id
and name
and the table coach
has 3 fields, id
, name
and team_id
. We can build the tables this way :
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar");
INSERT INTO coach VALUES (default, "John Smith", 1), (default, "Jane Doe", 1), (default, "Zinedine Zidane", 2);
How do I know who are the coaches of the team foo ? Using a JOIN
on the foreign key :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE t.name = "foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach c
ON c.team_id = t.id
WHERE c.name = "Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
You can try it yourself on DB Fiddle
Case 2 : A coach can belong to n team
A unique coach belongs to one or many teams (this is a 1,n relation)
A unique team can have one or many coaches. (this is a 1,n relation)
We can draw a relational schema of those entities this way :
[coach] -(1, n)-> [coach_team] <-(1, n)- [team]
In this kind of relation, a new table coach_team
is needed, where both foreign keys of the coach and the team will be stored. To avoid duplication of the couples coach/team, I prefer declaring the PRIMARY KEY
as a composition of both foreign keys.
As exemple, I have 3 teams, foo, bar and team 42. And 3 coaches, John Smith, Jane Doe and Zinedine Zidane. John and Jane are the coaches of the team foo and Zidane is the coach of the team bar and the team 42 has Zidane and Jane as coach.
Coach
id | name
----+-----------------
1 | John Smith
2 | Jane Doe
3 | Zinedine Zidane
Team
id | name
----+--------
1 | foo
2 | bar
3 | team 42
coach_team
coach_id | name_id
----------+---------
1 | 1
2 | 1
3 | 2
2 | 3
3 | 3
Table creations
CREATE TABLE team
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach
(
id INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE coach_team
(
coach_id INT(6) NOT NULL,
team_id INT(6) NOT NULL,
CONSTRAINT FOREIGN KEY(coach_id) REFERENCES coach(id),
CONSTRAINT FOREIGN KEY(team_id) REFERENCES team(id),
PRIMARY KEY (coach_id, team_id)
);
INSERT INTO team VALUES (default, "foo"), (default, "bar"), (default, "Team 42");
INSERT INTO coach VALUES (default, "John Smith"), (default, "Jane Doe"), (default, "Zinedine Zidane");
INSERT INTO coach_team VALUES (1, 1), (2, 1), (3, 2), (2, 3), (3, 3);
How do I get the coaches of the team foo ? I query the team, JOIN
on the reference table, coach_team
, then another JOIN
on the table coach.
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE t.name="foo";
Result
| Team name | Coach name |
+-----------+------------+
| foo | John Smith |
| foo | Jane Doe |
How do I know what is the team coached by Zidane? Using the same query, only the WHERE
clause changes :
SELECT t.name AS "Team name", c.name AS "Coach name"
FROM team t
INNER JOIN coach_team ct
ON ct.team_id = t.id
INNER JOIN coach c
ON ct.coach_id = c.id
WHERE c.name="Zinedine Zidane";
Result
| Team name | Coach name |
+-----------+-----------------+
| bar | Zinedine Zidane |
| Team 42 | Zinedine Zidane |
You can try it yourself on DB Fiddle
edited Nov 12 at 15:55
answered Nov 12 at 8:54
Cid
2,97421024
2,97421024
thats great Thanks Cid. Just another addidtion question... what if a coached also coached an addition team (so a coach, coaches 2 different teams.)
– Andy Terry
Nov 12 at 14:08
@AndyTerry Can it be the case for your application?
– Cid
Nov 12 at 15:33
@AndyTerry I edited my answer, check case 2
– Cid
Nov 12 at 15:58
Cid that is perfect mate. i appreciate the time and effort into the answer. 2 or 3 coaches in our club do coach mutliple teams within the club. eg steve coaches the under 13s and 15s. so this is exactly what i was needing clarifying.. appreciate it.
– Andy Terry
Nov 12 at 21:27
You're welcome :) Hint : Since a coach can manage many teams, when creating a new coach, a dropdown isn't the best choice (1 team to select) but checkboxes might suits better your needs (multiples teams can be selected)
– Cid
Nov 13 at 7:36
add a comment |
thats great Thanks Cid. Just another addidtion question... what if a coached also coached an addition team (so a coach, coaches 2 different teams.)
– Andy Terry
Nov 12 at 14:08
@AndyTerry Can it be the case for your application?
– Cid
Nov 12 at 15:33
@AndyTerry I edited my answer, check case 2
– Cid
Nov 12 at 15:58
Cid that is perfect mate. i appreciate the time and effort into the answer. 2 or 3 coaches in our club do coach mutliple teams within the club. eg steve coaches the under 13s and 15s. so this is exactly what i was needing clarifying.. appreciate it.
– Andy Terry
Nov 12 at 21:27
You're welcome :) Hint : Since a coach can manage many teams, when creating a new coach, a dropdown isn't the best choice (1 team to select) but checkboxes might suits better your needs (multiples teams can be selected)
– Cid
Nov 13 at 7:36
thats great Thanks Cid. Just another addidtion question... what if a coached also coached an addition team (so a coach, coaches 2 different teams.)
– Andy Terry
Nov 12 at 14:08
thats great Thanks Cid. Just another addidtion question... what if a coached also coached an addition team (so a coach, coaches 2 different teams.)
– Andy Terry
Nov 12 at 14:08
@AndyTerry Can it be the case for your application?
– Cid
Nov 12 at 15:33
@AndyTerry Can it be the case for your application?
– Cid
Nov 12 at 15:33
@AndyTerry I edited my answer, check case 2
– Cid
Nov 12 at 15:58
@AndyTerry I edited my answer, check case 2
– Cid
Nov 12 at 15:58
Cid that is perfect mate. i appreciate the time and effort into the answer. 2 or 3 coaches in our club do coach mutliple teams within the club. eg steve coaches the under 13s and 15s. so this is exactly what i was needing clarifying.. appreciate it.
– Andy Terry
Nov 12 at 21:27
Cid that is perfect mate. i appreciate the time and effort into the answer. 2 or 3 coaches in our club do coach mutliple teams within the club. eg steve coaches the under 13s and 15s. so this is exactly what i was needing clarifying.. appreciate it.
– Andy Terry
Nov 12 at 21:27
You're welcome :) Hint : Since a coach can manage many teams, when creating a new coach, a dropdown isn't the best choice (1 team to select) but checkboxes might suits better your needs (multiples teams can be selected)
– Cid
Nov 13 at 7:36
You're welcome :) Hint : Since a coach can manage many teams, when creating a new coach, a dropdown isn't the best choice (1 team to select) but checkboxes might suits better your needs (multiples teams can be selected)
– Cid
Nov 13 at 7:36
add a comment |
up vote
0
down vote
You could use a foreign key between the two tables to connect the information. In example, add a column named something like team_id in the 'coaches' table. This column should have the id of the team from the 'team' table.
Then, when you register a new coach in a form, you can use the id as the value from the dropdown.
thats exactly what im looking for. As i will be adding players also i would also need to link those to the team table. what would the query look like, i know how to do simple queries, not dealt with joins as of yet.
– Andy Terry
Nov 11 at 19:50
when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach?
– Andy Terry
Nov 11 at 19:57
As I said, you could add a column in the coaches table, so the structure would be something like: name, dob, qualifications, contact info, team_id where the team_id should be the id of the team in the team table. Then in your schema for signing up coaches, you would have a dropdown something like this (fill it with the data from the DB): <select> <option value="team_id_1">Name of team 1</option> <option value="team_id_2">Name of team 2</option> </select> And then you can use the team_id_x value in the INSERT statement
– ERR0
Nov 11 at 22:20
add a comment |
up vote
0
down vote
You could use a foreign key between the two tables to connect the information. In example, add a column named something like team_id in the 'coaches' table. This column should have the id of the team from the 'team' table.
Then, when you register a new coach in a form, you can use the id as the value from the dropdown.
thats exactly what im looking for. As i will be adding players also i would also need to link those to the team table. what would the query look like, i know how to do simple queries, not dealt with joins as of yet.
– Andy Terry
Nov 11 at 19:50
when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach?
– Andy Terry
Nov 11 at 19:57
As I said, you could add a column in the coaches table, so the structure would be something like: name, dob, qualifications, contact info, team_id where the team_id should be the id of the team in the team table. Then in your schema for signing up coaches, you would have a dropdown something like this (fill it with the data from the DB): <select> <option value="team_id_1">Name of team 1</option> <option value="team_id_2">Name of team 2</option> </select> And then you can use the team_id_x value in the INSERT statement
– ERR0
Nov 11 at 22:20
add a comment |
up vote
0
down vote
up vote
0
down vote
You could use a foreign key between the two tables to connect the information. In example, add a column named something like team_id in the 'coaches' table. This column should have the id of the team from the 'team' table.
Then, when you register a new coach in a form, you can use the id as the value from the dropdown.
You could use a foreign key between the two tables to connect the information. In example, add a column named something like team_id in the 'coaches' table. This column should have the id of the team from the 'team' table.
Then, when you register a new coach in a form, you can use the id as the value from the dropdown.
answered Nov 11 at 19:45
ERR0
225
225
thats exactly what im looking for. As i will be adding players also i would also need to link those to the team table. what would the query look like, i know how to do simple queries, not dealt with joins as of yet.
– Andy Terry
Nov 11 at 19:50
when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach?
– Andy Terry
Nov 11 at 19:57
As I said, you could add a column in the coaches table, so the structure would be something like: name, dob, qualifications, contact info, team_id where the team_id should be the id of the team in the team table. Then in your schema for signing up coaches, you would have a dropdown something like this (fill it with the data from the DB): <select> <option value="team_id_1">Name of team 1</option> <option value="team_id_2">Name of team 2</option> </select> And then you can use the team_id_x value in the INSERT statement
– ERR0
Nov 11 at 22:20
add a comment |
thats exactly what im looking for. As i will be adding players also i would also need to link those to the team table. what would the query look like, i know how to do simple queries, not dealt with joins as of yet.
– Andy Terry
Nov 11 at 19:50
when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach?
– Andy Terry
Nov 11 at 19:57
As I said, you could add a column in the coaches table, so the structure would be something like: name, dob, qualifications, contact info, team_id where the team_id should be the id of the team in the team table. Then in your schema for signing up coaches, you would have a dropdown something like this (fill it with the data from the DB): <select> <option value="team_id_1">Name of team 1</option> <option value="team_id_2">Name of team 2</option> </select> And then you can use the team_id_x value in the INSERT statement
– ERR0
Nov 11 at 22:20
thats exactly what im looking for. As i will be adding players also i would also need to link those to the team table. what would the query look like, i know how to do simple queries, not dealt with joins as of yet.
– Andy Terry
Nov 11 at 19:50
thats exactly what im looking for. As i will be adding players also i would also need to link those to the team table. what would the query look like, i know how to do simple queries, not dealt with joins as of yet.
– Andy Terry
Nov 11 at 19:50
when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach?
– Andy Terry
Nov 11 at 19:57
when a coach is signing up, selecting their team from the dropdown. They are linked. but some teams have two coaches, im guessing the second coach selecting the team would then overwrite the previous, how would you add this as an additional coach?
– Andy Terry
Nov 11 at 19:57
As I said, you could add a column in the coaches table, so the structure would be something like: name, dob, qualifications, contact info, team_id where the team_id should be the id of the team in the team table. Then in your schema for signing up coaches, you would have a dropdown something like this (fill it with the data from the DB): <select> <option value="team_id_1">Name of team 1</option> <option value="team_id_2">Name of team 2</option> </select> And then you can use the team_id_x value in the INSERT statement
– ERR0
Nov 11 at 22:20
As I said, you could add a column in the coaches table, so the structure would be something like: name, dob, qualifications, contact info, team_id where the team_id should be the id of the team in the team table. Then in your schema for signing up coaches, you would have a dropdown something like this (fill it with the data from the DB): <select> <option value="team_id_1">Name of team 1</option> <option value="team_id_2">Name of team 2</option> </select> And then you can use the team_id_x value in the INSERT statement
– ERR0
Nov 11 at 22:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252344%2fjoining-tables-and-data-for-a-football-soccer-website%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
By linking, what do you mean? Database side? Back end side? Front end side?
– Cid
Nov 11 at 19:29
i think ERR0 gets my drift. basically 2 database tables which link the coach and the team.
– Andy Terry
Nov 11 at 19:50
Its a many to many situation, so have a
coach
(links user as a coach) andteam_coach
(links coach to team) table, then use a JOIN to link them. Many coaches can be on many teams then. I guess you're just using columns and not normalising.– Lawrence Cherone
Nov 11 at 20:02
Unrelated to your question, but do not concatenate arbitrary data directly into the context of HTML without escaping it first! At best, you'll be introducing invalid HTML. At worst, you open yourself up to some bad security issues. Always use
htmlspecialchars()
around data you're concatenating into HTML.– Brad
Nov 11 at 20:10
thanks brad, im going to go through all the security once i have the page complete.
– Andy Terry
Nov 11 at 20:18