简体   繁体   English

多表数据库设计

[英]Multiple table database design

I have a website where I display a list of sports players and their stats. 我有一个网站,其中显示体育运动员及其统计信息的列表。 Users can register and create a team of these players where I sum and analyze the team stats. 用户可以注册并创建一个由这些球员组成的团队,在这里我对团队统计信息进行汇总和分析。 I would like to give each user the ability to "cross out" a player from the list (yet not remove them from the database) and reinstate a player that has been crossed out back to the list if they need to. 我想赋予每个用户从列表中“淘汰”一名球员的能力(但尚未将其从数据库中删除),并在需要时恢复已经被淘汰的一名球员。 The players that are crossed out or not crossed out need to be saved in a database so when the user returns to the site their list is saved. 被淘汰或未被淘汰的球员需要保存在数据库中,这样当用户返回站点时,他们的名单就被保存了。

These are the tables I currently have: 这些是我目前拥有的表:

Players Table (COLS: Name, Team, Position, Stats, etc.)
Users Table (COLS: User ID, Name, Email, etc.)
Teams Table (COlS: User ID, 1, 2, 3, etc.)

I'm pretty sure that there is a better way to store the info in the Teams Table where I have a column for user id and then 30 columns (numbered 1-30) where I store the player name and position he was selected for in the cell (in this format Player Name/Position). 我很确定,有一种更好的方法可以在Teams Table中存储信息,在该表中我有一列用户ID,然后在30列(编号1-30)中存储了玩家的名字和他在其中所选择的位置单元格(采用“播放器名称/位置”格式)。 A team can consist up to 30 players. 一个团队最多可包含30名球员。

I think I would need an "Available Table" that stores which players are crossed out by which users. 我想我需要一个“可用表”来存储哪些玩家被哪些用户淘汰。 I'm not sure what is the best way to go about this considering a user can cross out up to 600 players from the list. 考虑到用户可以从列表中淘汰多达600名玩家,我不确定执行此操作的最佳方法是什么。

Any suggestions would be greatly appreciated, I'm completely new to dealing with databases and I have seem to hit a wall. 任何建议都将不胜感激,我是数据库处理的新手,而且我似乎碰壁了。

Thanks! 谢谢!

You should probably remove the player data from the team table and set up a join table for the players and team data. 您可能应该从团队表中删除玩家数据,并为玩家和团队数据建立一个联接表。

So you end up with 所以你最终

Teams Table (Cols: ID, User ID, Team Name)
Team_Players Table (Cols: Team_ID, Player_ID, Position, Active)

You can then get which players are in which team by running a query on Team_Players and check if they are active or not. 然后,您可以通过在Team_Players上运行查询来获取哪些球员属于哪个团队,并检查他们是否处于活动状态。

I didn't add an ID field because the join of Team_ID and Player_ID would be unique and could be used as the key for this table. 我没有添加ID字段,因为Team_ID和Player_ID的联接是唯一的,并且可以用作此表的键。

Read this tutorial . 阅读本教程 It is very helpful: 这非常有帮助:

http://www.tomjewett.com/dbdesign/dbdesign.php?page=association.php http://www.tomjewett.com/dbdesign/dbdesign.php?page=association.php

The design of your database depends on whether a given player can belong to more than one team in the database. 数据库的设计取决于给定的玩家是否可以属于数据库中的多个团队。 If it can, that is a has and belongs to many relationship and you would need a linking table, perhaps called Players_Teams (always list tables in alpha order). 如果可以,那是一个has and belongs to many关系,您将需要一个链接表,该表可能称为Players_Teams (始终以alpha顺序列出表)。 This table could also contain the crossedOff column you were talking about. 该表还可以包含您正在谈论的crossedOff列。

Using an ORM (Object Relational Mapper) like ActiveRecord or Doctrine can greatly reduce the amount of code you need to write to make the management of these relationships somewhat "automatic" in your code. 使用ActiveRecord或Doctrine之类的ORM(对象关系映射器)可以大大减少您需要编写的代码量,以使这些关系的管理在代码中有些“自动”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM