insert data in on table from another table in MySQL - mysql

I have 2 tables called teams and players and I want to copy the field points from the table teams to the table players using the foreign key team in the table players which is the primary key id_team in the table teams. I need to have each player with the points of your own team. I'm trying
INSERT INTO players (points_team) SELECT points FROM teams WHERE
and here I don't know how to write the correct where sentence

where id_team = (whatever you the fk/pk is named of the other table);
this will essential connect the fk to the pk of both the tables.

Related

mysql Script for updating data in a second foreign key column using an existing foreign key relationship

I have 3 tables. Leagues, teams, players. Each team belongs to only one league and each player belongs to only one team. Every player is on a team and every team is in a league.
Table 2 [teams] has a foreign key to table 1 [leagues]. ( t2fk = t1id)
Table 3 [players] has a foreign key to table 2 [teams]. (t3fk = t2id)
I want to use the above relationship to write a script that adds [t1id] to table 3 as a second foreign
key. In other words, I want to add the league id [t1id] to the players table using
the shared relationship with teams.
The end goal is for table 3 to have two foreign keys [t3fk = t2id] and [t3fk = t1id].

Multiple foreign keys to different tables on a single id

I'm trying to create a rather complicated relationship between the tweets and its author. In my system, I have 3 sources which can be related to a tweet (teams, players and persons). The issue is that instead of having a teamId, personId and teamId in each tweet and only one of them is not null. I've come up with below solution with a relational table called created. However the issue is that I'm not sure it is possible to create a foreign key from the sourceId to all 3 ids in Teams, Players and Persons tables? What can I do in order to create the right table structure for this type of system?
To explain the Created table further it contains free columns:
id = primary key and auto_increment
sourceType = enum can be (persons, teams or Players). By doing this i know whether it is a person, team or player
sourceId = is the id of the particular person, team or player.
Players
id
name
image
twitter_user
teamId
Teams
id
name
twitter_user
Persons
id
name
image
twitter_user
teamId
Tweet
id
created
text
createdId
Created
id
sourcetype
sourceId

Deciding database table architecture

I'm very new to Databases and looking forward to develop an application with some advanced functionality.
Here I'm going to have a table with users just like the following one.
So, in the sports column, value will be the sports the user plays. They will be some ids from the records of sports table.
So my problem is how to store those multiple values in the same row same column ?
Thanks
PS: Please note that I'm a beginner...
The proper way to do this would be to maintain three different tables.
User: id, name, age
Sport: id, name
UserSport: user_id, sport_id
The UserSport table references the primary key (id) of both tables (User and Sport) and contains a separate entry for each sport the user participates in.
The primary key of UserSport should then be a composite key of both user_id and sport_id. This allows you to have multiple rows for each user and multiple rows for each sport, but a unique combination of both the user and sport.

Relationship tables concept: check my understanding, please

I'm designing my first big database and want to check, whether I'm OK in providing tables relationships.
I'm designing a webapp where
users can play games in teams
each game has it's categories
users create their team for the game and choose their team category
each game has it's own subset of categories enabled
in fact categories slightly differ from game to game. For example, there are slowpoke categories in game1 and game2, but they differ in alowed time.
How do I hope to store this:
there are tables users, teams, games,
categories, each with it's adequate data fields and INT primary_keys
there is user-team M-N relationship table
there is game-category M-N relationship table, with alowed time column
there are category_id and game_id foreign key columns at team table
My common queries:
user -> all his/her teams, in which they were games, categories
game -> all it's categories
game -> all teams paticipated, with category, with users
game -> all users participated
team -> it's game, category
My suggestions to me:
Make a real-category id creating a separate INT primary index in category-game relation table and use it in team table
Create separate table with team_id category_id game_id and set all them as a concatenated primary index
Just set game_id in teams table as INDEX
The question is if this thing is OK and what suggestions to accept?
Some thoughts:
Use UNSIGNED INT for primary keys rather than INT.
You must have some TEAM -> GAME mapping, right?
Given that, it seems like you can easily process your "common queries".
What problems were your "suggestions" trying to solve?

having trouble with foreign key queries

I'm new to SQL and I'm having a hard time figuring out how to execute queries with foreign keys on MySQL Workbench.
In my example, I have three tables: people, places, and people_places.
In people, the primary key is people_id and there's a column called name with someone's name.
In places, the primary key is places_id and there's a column called placename with the name of a place.
People_places is a junction table with three columns: idpeople_places (primary key), people_id (foreign key), and places_id (foreign key). So this table relates a person to a place using their numerical IDs from the other two tables.
Say I want the names of everyone associated with place #3. So the people_places table has those associations by number, and the people table relates those numbers back to the actual names I want.
How would I execute that query?
Try this to find all the people names who are associated with place id 3.
SELECT p.name
FROM people as p
INNER JOIN people_places as pp on pp.people_id = p.people_id
WHERE pp.places_id = 3
OK, so you need to "stitch" all three tables together, yeah?
Something like this:
select people.name
from people -- 1. I like to start with the table(s) that I want data from, and
, people_places -- 2. then the "joining" table(s), and
, places -- 3. finally the table(s) used "just" for filtering.
where people.people_id = people_places.people_id -- join table 1 to table 2
and people_places.place_id = places.place_id -- join table 2 to table 3
and places.name = "BERMUDA" -- restrict rows in table 3
I'm sure you can do the rest.
Cheers. Keith.