M
M
Muranx2021-10-24 06:27:31
MySQL
Muranx, 2021-10-24 06:27:31

How to organize the table of achievements on the site?

Hello!
The idea is simple, there is a table user

+----+-----------+
| id | user_name |
+----+-----------+
|  1 | Dima      |
|  2 | Sveta     |
|  3 | Katya     |
|  4 | Jenya     |
+----+-----------+

You need to make a table of achievements , which will be associated with users from the table user, for example, idand which will store the achievements of users in the training program on the site! the first thing that comes to mind considering my 3 weeks experience of learning skl is....
+-----------------+----------+----------------+-----------------+---------------+
| achievements_id | users_fk | pass_first_lvl | pass_second_lvl | invite_friend |
+-----------------+----------+----------------+-----------------+---------------+
|               1 |        1 |              1 |               0 |             1 |
|               2 |        2 |              0 |               0 |             1 |
|               3 |        3 |              1 |               0 |             0 |
|               4 |        4 |              0 |               0 |             1 |
+-----------------+----------+----------------+-----------------+---------------+

... where, of course achievements_id, this is the primary key, and users_fkthis is a foreign key associated with the table userby field id, where in the cell , of 1course, the achievement is received, but where, 0what would you think...? Achievement not completed yet! But the question immediately arises:
1. What if there are more than 4 achievements (and their 100% will be more than 4), for example, 100 achievements, will this be a monster table?
Further... Of course, the gold standard for achievements is the date when they were received (well, let's say in the future to use this date in some kind of overall rating), there is an idea....
+-----------------+----------------------+---------------------+----------------------+--------------------+
| achievements_id | achievements_date_fk | pass_first_lvl_date | pass_second_lvl_date | invite_friend_date |
+-----------------+----------------------+---------------------+----------------------+--------------------+
|               1 |                    1 | 2021-12-05          | 0000-00-00           | 2020-04-01         |
|               2 |                    2 | 0000-00-00          | 0000-00-00           | 1987-11-03         |
|               3 |                    3 | 2011-05-05          | 0000-00-00           | 0000-00-00         |
|               4 |                    4 | 0000-00-00          | 0000-00-00           | 2019-07-16         |
+-----------------+----------------------+---------------------+----------------------+--------------------+

.. one more table - a monster with the dates of these achievements, which is connected to the table userby id
In general, the main question here I think about the implementation of the database structure, and how reasonable is the idea to organize achievement tables exactly the way I do from the height of my experience ( -_- ) ? Thank you for your attention, be correct and don't be overbearing!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-10-24
@Muranx

Despite the fact that you have drawn several tables, from what you write, it is clear that all these tables are the same (one-to-one relationship), i.e. there is no normalization.
A user can have many achievements, of different types, so there should be a one-to-many relationship, something like:

+-----------------+----------+----------------+-----------------+
| achievements_id | users_fk |      type      |      date       |
+-----------------+----------+----------------+-----------------+
|               1 |        1 |              1 |      2021-10-01 |
|               2 |        2 |              1 |      2021-10-04 |
|               3 |        1 |              2 |      2021-10-08 |
|               4 |        1 |              4 |      2021-10-15 |
+-----------------+----------+----------------+-----------------+

If the achievements themselves have different properties depending on the type, you can make another level of one-to-many communication:
+-----------------+-----------+----------------+-----------------+
| achieve_prop_id | acieve_fk |      type      |      value      |
+-----------------+-----------+----------------+-----------------+
|               1 |         1 |              1 |           'abc' |
|               2 |         2 |              1 |           '145' |
+-----------------+-----------+-----------------+----------------+

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question