Relationship between two tables

0
Hello, I would like to know how to create a relationship between two tables, Time and Start, and in the beginning I should put the columns: time1, placar1, time2, placar2, and a date. But my question is whether I need to inform the teams fields in this table or if this will occur in the relationship table, leaving only other columns in the table without the teams.

    
asked by anonymous 17.05.2018 / 16:52

3 answers

2

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

The same for:  time2: VARCHAR (50)

So, when you create a match in the games table, you will have 2 teams ids in the tbl_games table and you can get any information from these teams through this relationship.

    
17.05.2018 / 19:30
0

Hello,

The ideal is that in each game, the teams that have played are stored. To further improve, in place of the name of each respective id Team. So each line added to the table would form a game, leaving your relationship table (which of course I do not know what relationships you think to do) to solve other issues related to each game, for example:

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

(Id 1350 - Brasileirão, id 1360 - Brazil Cup)

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

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

First table - Games table
Second table - Championships table

I hope I've been able to help.

    
14.06.2018 / 04:19
0
  • Tip:

I think it would be nicer to add two fields in the second table: id_time1 and id_tim2 as foreign keys, imagine that there is a point in the your project that you want to pull information from each team of the game, with the name work? It gives yes, but it goes that depending on who is to fill, ends up having equal names or sla, with id this would not 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 in Time 1 and game , you could also do Time 2 simultaneously, without needing several querys or something.

Creating 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)'

Note the ON DELETE CASCADE , this is also used to exclude the child record if the main table record in the segunda_tabela context is deleted

    
14.06.2018 / 06:20