i'm developing Fantasy App.
I create one table called 'Tournaments' and one table called 'Entries'.
I want have the total number of entries for each tournament.
I really don't want query everytime the table 'Entries' to count the number of entries for each 'Tournament'. So i think in put also some column on 'Tournaments' table calling column 'entries_total'.
How is the best way to perform this ?
To resume the goal: Have the total number of tournaments entries in an column on the 'Tournaments' table.
Related
I am going to write some code to retrieve and add to/remove from a student's hours that they have signed up for. For example...
student 1:
October 20th:
12am
4pm
7pm
October 21st:
8pm
student 2
October 19th
1pm
6pm
I'm trying to wrap my head around how to create this type of table setup on phpmyadmin with each student having a dynamic number of hours, and different times, and different days. I am new to mysql management, am vaguely familiar with joins and stuff, and am just now starting to expand my database to more complex things like this. What I have learned so far is that enums is NOT where I want to go. Just unsure of a starting point...
What is a good strategy for doing something like this?
Thank you,
you need to create many to many relation
first i try to explain it simple and fast:
1- you need to make a table for hours, each hours have 1 row.
2- i guess you already have a student table
3- now you need a table that contain only 2 column, first column is hours table id, second column is student id.
at the end you simply need to execute select command like this:
select * from StudentHours Table where student-id = 1;
Detailed Information:
Relational database systems usually don't allow you to implement a direct many-to-many relationship between two tables. Consider the example of keeping track of invoices. If there were many invoices with the same invoice number and one of your customers inquired about that invoice number, you wouldn't know which number they were referring to. This is one reason for assigning a unique value to each invoice.
To avoid this problem, you can break the many-to-many relationship into two one-to-many relationships by using a third table, called a join table. Each record in a join table includes a match field that contains the value of the primary keys of the two tables it joins. (In the join table, these match fields are foreign keys.) These foreign key fields are populated with data as records in the join table are created from either table it joins.
A typical example of a many-to many relationship is one between students and classes. A student can register for many classes, and a class can include many students.
The following example includes a Students table, which contains a record for each student, and a Classes table, which contains a record for each class. A join table, Enrollments, creates two one-to-many relationships—one between each of the two tables.
I have 40 students and each have enrolled in five courses. Every course tracks attendance: the date on which each student was present. I'm thinking to create a separate table for each student. Is there any efficient way to do this?
Do NOT create a table for each student.
What you should have is a Student table, a Course table, and an Attendance table. The Attendance table should have columns for the student key, the course key, the date, and the status (present, tardy, absent, etc).
This will be far more efficient than using separate tables for each student.
You could write a stored proc that takes a comma-separated list of your student names and builds the tables from that. However, creating 40 tables that are identical except for the name seems really inefficient. SQL can handle large row sets, no problem. Even if you put all of the students into the same table and they had an attendance row for all 365 days of the year (40 students x 5 courses x 365 days) is only 73,000 rows/year; that's nothing for SQL.
Also, if you ever wanted to do any reporting with multiple student tables, you will need to target EVERY table instead of just targeting your one main table and grouping by studentID.
In short, yes you can write a proc that will dynamically create identical tables for each student. Is it a good (read: efficient) idea? Personally, I don't think so. I think you could get way more performance (losing some because of larger searches but gaining some for not having to JOIN any tables) and reporting ability in a much shorter amount of time/work by keeping it all on one table.
I am trying to figure out the best way to store some data in my database. I plan on joining 3 different tables.
users table
fighters table
moveset table
In the fighters table I have a row for 'moveset' in which I am trying to figure out a way that move_id's from the moveset table can then be associated within the fighters table under the moveset row so each fighter can list a bunch of moves that belongs to them.
Ex.
fighters table has
fighter_id,
fighter_name,
moveset
moveset table has
move_id,
move_name,
move_damage
Multiple move_id's need to get put in the moveset row in the fighters table.
Any help on how to set this up properly in mysql?
You need a "many-to-many" relationship.
To store this, the best way is by creating an association table, which has the key if both tables being related:
fighter_moves
-------------
id
fighter_id
move_id
You can even add other non-key columns, called assoiation data, that describe the relationship. For example, speed to define how fast the fighter does the move.
Let's say there is a database with two tables: one customer table and one country table. Each customer row contains (among other things) a countryId foreign key. Let's also assume that we are populating the database from a data file (i.e., it is not an operator that is selecting a country from a UI).
What is the best practice for this?
Should one query the database first and get all ID's for all countries, and then just supply the (now known) country id's in the insert query? This is not a problem for my 'country' example, but what if there is a large number of records in the table that is being referred?
Or should the insert query use a sub query to get the country id based on the country name? If so, what if the record for the country does not exist yet and has to be added?
Or another approach? Or does it depend? :)
I would suggest using a join in your insert query to get the country id based on the country name. However, I don't know if that's something possible with every SGBD and you don't give more precision on the one you're using.
for example,access example db, every student record have a nested table about guardians.
http://img101.imageshack.us/img101/9881/53882937.jpg
No. Something like that is almost always done as a single mapping table for all students, with a foreign key column pointing to the student table to specify which student a particular row relates to.
You then just filter the table to match a given student, and present that list without a student column, in the UI. It looks like a separate table to the user, but that's not actually how it's stored.
(If you did create a separate guardians table for each student, you'd make it impossible to do queries like ‘find students for a particular guardian’.)