A
A
AlexTest2013-06-07 14:45:19
MySQL
AlexTest, 2013-06-07 14:45:19

Tell me how to work with the table "dictionary"

There is a table in MySQL containing, among other things, two dozen columns with string information:
table (bla bla bla, string1, string2,…, string20
) columns, which sadly affects the amount of stored data.
Naturally, a desire immediately arises to split such a table into two, in the first in the corresponding fields to store row identifiers from the second table:
table1 (bla bla bla, id1, id2, ..., id20) table2
(id, string)
structures are very cumbersome (with a bunch of JOINs).
Usually, a wrapper/layer above the database is involved in building such queries and this does not cause discomfort, but unfortunately in this project there is noodle code that you don’t want to touch, because. it is only necessary to solve the problem of ts. "compactization" of this table.
I am not strong in MySQL, but something tells me that there should be a more convenient syntax for such queries, or perhaps this task should be solved in some other way.
Please tell me the best way to do this. Of course, if there are no other options, then a bunch of JOINs will do, but I would like something more elegant.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Shultc, 2013-06-07
@Shultc

Well, it seems to me that if the beauty of queries is important to you, then you should use VIEW .

F
frostosx, 2013-06-07
@frostosx

Hm. Normalization of the subject area.
In short, you need to get rid of duplication of information by creating a new table that implements a many-to-many relationship, and the columns
[string1, string2, ..., string20], [id1, id2, ..., id20], will turn into records

S
shavluk, 2013-06-09
@shavluk

I would replace this table with a one-to-many relationship
Add the "number" field and control the order through it
The structure is as follows:
main_table (id, bla bla bla)
and
detail_table (id, id_main_table, string, number)
Add a unique key to detail_table (id_main_table, number)
To support the old noodle code, you need to add VIEW TABLE1 based on
select m.*, d1.string string1, d2.string string2,… d20. string string20 from main_table m
left join detail_table d1 on d1.id = m.id_main_table and d1.number = 1
left join detail_table d2 on d2.id = m.id_main_table and d2.number = 2

left join detail_table d20 on d20.id = m.id_main_table and d2.number = 20

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question