How to design my database with Laravel? - mysql

I'm just doing my first steps with Laravel and web development in general. For this step of my project I'm designing DB for tournament website.
What I eventually need: Every match has 3 maps. Every map has own score for each team (score_left_team -Map_name - score right team ). And every match has general score (left_team_score - right_team_score) Perfect example: https://overwatchleague.com/en-us/match/10223
How can I create the correct relationships and improve my design below?
Team::
id
name
Player::
id
team_id (belongsTo Team::class)
name
Map::
id
name
photo
Match_Map::
id
map_id (belongsTo Map::class)
match_id (belongsTo Match::class)
score_left
score_right
Match::
id
date
stage_name
group_name
left_team_id (belongsTo Team::class)
right_team_id (belongsTo Team::class)
score_left
score_right
winner (How to create here relationship?)
status
I appreciate any tips!

Looks fairly correct, if you have the scores for both teams when you are creating the record, you could have a winner_id be a foreign key for a team id. This could be both on match_map and on match. Besides that looks good.

Related

Relational database structure for storing multiple hobbies of a user

I am trying to register some users with basic details like username/email, password and their respective hobbies like reading, sports, dance etc and later on display users with similar hobbies. The current schema looks something like this.
Users
- id
- email
- password
- country
- hobbies_id
hobbies
- id
- user_id
- sports(values true/false)
- reading(values true/false)
- dance(values true/false)
Each hobby is placed as a column in hobbies table.
What will be the most optimized schema if I increase the number of hobbies from 3 to 20?
Also, can someone help me with a query to select users with similar hobbies/hobby? For example, if John likes reading and sports, and Kim likes sports and dance then they have sports as a common hobby.
Thanks in advance.
Following up on comments by #Madhur Bhaiya, I would adress this with 3 tables:
users
- id
- email
- password
- country
hobbies
- id
- name (sports, reading, dance, ...)
user_hobbies
- user_id
- hobbie_id
The users table is the master table for users (one record per user).
The hobbies table is the master table for hobbies (one record per hobby). When new hobbies are created, you do not need to create new columns, just add new rows.
The user_hobbies table maps users to hobbies: it contains one record for each user_id/hobbie_id tuple.
we can do with two solution ::
#GMB's solution
removed hobby table and save hobby data into user table only, in json data-type.
For this, I would recommend looking into Database Normalization. This issue should be solved by implementing the Third Normal Form (TNF). For this, you should remove hobby_id from the users table and remove user_id from the hobbies table. A normalized example of one solution to this problem would be to create a new table that uses user_id and hobby_id as a Composite Key. See below:
users:
- id
- email
- password
- country
user_hobby:
- user_id
- hobby_id
hobbies:
- id
- description
- type
In this situation, the user_hobby table would have a many to many relationship between users and hobbies. If a user has multiple hobbies, they will have multiple hobbies linked to their id in the user_hobby table, but each user and hobby should be listed only once in their respective tables.

Exam Result Publishing mysql

Hi I am developing Exam result publishing application. I am using mysql database for this. My table structure are like this here I didn't include other basic tables
table exams
id
name
year_id
semester_id
course_id
level_id
table exam_students
id
exam_id
student_id
exam_type /* regular/partial */
table exam_results
id
exam_id
student_id
subject_id
marks
Here exams table holds a exam information and students are enrolled in that exam and that information is saved in exam_students table and the result are stored in exam_results. These are fine for a regular students(who will sit for all the subjects exam) but how do I manage the students who were failed in some subjects last time and only sit for some subjects this time(partial type)
For eg, let say we have 3 subjects x,y and z and student A failed in y subject in last exam and this time he only sit for y subject and if he get pass mark this time in y subject then we will have to publish the result of student A along with the other subjects marks that he already gained and if he fails in that subject again he have to sit for it next time.
So how do I design table for this kind of logic.
Its hard for me to explain please if somebody know websites link to get the information on this it will be great help for me.
Thanks for your any help and suggestion
You can have one more table named as "Repeaters". In that, you can have attributes like id, exam_id, student_id and one more that is course_id to make you aware that which course he/she is repeating.

Database Design for a system that has Facebook like groups

I'm creating a system that has Groups. These can be thought of like Facebook Groups. Users can create new groups. Currently I have the following types of groups:
City Group - Groups based on a certain city. For example "London Buy and Sell Group"
School Group - Groups based on schools. For example "London University Study Group"
Interest Group - Groups that are not tied to a place. For example "Over 50's Knitting Group"
In the future more group types will be added. Each group can have different types of options, but all groups have the same basic data:
An ID
A creator ID
A name
An option description
I'm struggling on putting together a database design for this. My initial thought was to create different tables for the different groups.
For example have a single table called group. This table has an id, creator id, name, description, member count, timestamps.
Then have other tables to represent the other groups, and link them to group. So I have a city_group table that contains and id, group_id, city_id. And the same for the other group types.
The only problem I have with this is interest_group doesn't have any extra data that a normal group. But for the purpose of being able to query only Interest Groups I thought it might make sense to create an interest_group table. It would only have the following columns: id, group_id, timestamps ... which seems a bit wasteful to have a table just for this purpose.
Here's a diagram to make things easier:
Are there any issues with my solution, or any better ways to solve this design problem?
I've got an idea, which is a workaround basically: have another table like: group_type in which you have id(the PK) and then you have tablename (the full table name of the type).
Then, you should have a FK from your Group table linking to this group_type table.
id tablename
--------------------
1 School Group
2 Interest Group
After all this is done, you could build your queries based on the values from this table, as an example:
JOIN (SELECT tablename FROM group_type WHERE id=group.group_type_id) ON ..

getting ids from 3 tables

a team table which containd a team ID and a team name and then 2 further tables that contain data from a quiz and another with data from a questionnaire.
I need to get a list of team IDs that have completed BOTH the quiz (so have some quiz data) AND the questionnaire (so have some questionnaire data) but I can't see how to join the 3 tables to get the list of ids I need.
Any pointers greatly received
As you said in upper comment..
I actually want team IDs that are in either of the data tables. How
would that amend the query? – Dave
Try this, This is what I understood with your words..
select teamID,teamName from team where teamID in (select teamID from quiz) or teamID in (select teamID from questionnaire);
Assuming you have some reference of team table inside quiz and questionaire tables, you will need to make a join this way -
SELECT team.id, team.name
from
team, quiz, questionnaire
where
team.id=quiz.team_id
AND team.id=questionnaire.team_id
when you do team.id=quiz.team_id AND team.id=questionnaire.team_id only those team IDs which are present in both quiz and questionnaire tables willbe shown in the result.
If you need team ID which exists in either of the tables quiz or questionaire, then you will to replace AND by OR-
SELECT team.id, team.name
from
team, quiz, questionnaire
where
team.id=quiz.team_id
OR team.id=questionnaire.team_id

mysql multiple tables vs 1 table

Im making a simple score keeping system and each user can have stats for many different games
But i don't know if each user should have his own score table or have one big table containing all the scores with a user_id column
Multiple Tables:
table name: [user id]_score
game_id | high_score | last_score | average_score
Single Table:
user_id | game_id | high_score | last_score | average_score
Definitely have a single table with the userID as one of the fields. It would be very difficult (and needlessly so) to deal with multiple tables like that.
You will likely want at least one index to include the userId field, so that the records for each userId can be quickly found by queries.
Go with a single table and a composite primary key around user_id and game_id. You may also want to create a supplementary index around game_id so that lookups of high scores by game are fast. (The composite key will not suffice if user_id is not part of the criteria as well.)
Using a one-table-per-user approach would be cause for some LARTing.
Keep one table with all of the user_ids in them, definitely. Generally, you want to stay away from any solution that involves having generated data (userIDs) in table names (there are exceptions, but they're rare).
you'll probably want:
a user table user_info[], user_id
a game table game_info[], game_id
a user_game_join table that holds user_id, game_id, score, insertdate
that way you have all the data to get high score by game, by user, by game & user, averages & whatnot or whatever else.
always build extensibly. you never know when you will need to refine your code and redoing a database is a real PITA if you don't lay it our right to start with.