Relationship between two tables

Asked

Viewed 215 times

0

Hello, I would like to know how to create a relationship between two tables, Time and Match, and in the match I should put the columns: time1, scoreboard 1, time2, scoreboard 2, and a date. But my question is whether I need to inform the fields of the teams in this table or whether this will occur, in the relationship table, getting in the table Games only the other columns without the teams. relação de duas tabelas

  • In the table games, you don’t need the team, you need the team ID. From this conception, create a relationship by entering the team id instead of the name.

3 answers

2

the way it is modeled, change the: time1: VARCHAR(50) of tbl_games by a foreign key of id_Times.

The same for the: time2: VARCHAR(50)

so when you create a match on the table games, you will have 2 team ids on the table tbl_games and you can get any information from these teams by this relationship.

0

Hello,

Ideally, in each game, be stored the teams that played. To improve further, in place of the name of each respective Time id. So each row added to the table would form a game, leaving your relationship table (which obviously do not know what relationships you think to do) to solve other issues related to each game, such as:

Relationship table "championships" that holds the id of each existing championship in Brazil

(Id 1350 - Brasileirão, id 1360 - Copa Do Brasil)

and the id of each game that was played in it:

(id 1450 - Game 1 (data1, id 1550 - Flamengo, score1 - 2, id 1650 - Vasco, score2 - 2))
(id 1460 - Game 2 (data2, id 1570 - Palmeiras, score1 - 1, id 1580 - São Paulo, score2 - 1)).

First table - table of games
Second table - Championships table

I hope I was able to help.

0

  • Tip:

I think it would be nicer to add in the second table tbl_games two fields: id_time1 and id_tim2 As foreign keys, imagine that there comes a point of your project that you want to pull information from each team in the game, with the name right? Yes, but depending on who you fill in, you end up with the same names or sla, with the id that wouldn’t happen. As @Luis Felipe said, so you have the information of all 2 teams at any time without needing much effort, just make a INNER JOIN:

SELECT * FROM `tbl_jogos` AS jogo INNER JOIN `tbl_time` AS time ON jogo.id_time_1 = time.id_Times WHERE jogo.id_Jogos = 1

In this query you get all the information from Team 1 and of game, could also make the Team 2 simultaneously, without needing several querys or anything of the kind.

To create foreign keys is easy:

`CREATE TABLE IF NOT `sua tabela` (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, id_segunda_tabela int NOT NULL, FOREIGN KEY(`id_segunda_tabela`) REFERENCES `segunda_tabela`(`id_da_segunda_tabela`) ON DELETE CASCADE)`

Understand the ON DELETE CASCADE, this serves to delete also the child record if the main table record, in the context segunda_tabela, be excluded

Browser other questions tagged

You are not signed in. Login or sign up in order to post.