O
O
Optimus2014-09-06 22:16:20
MySQL
Optimus, 2014-09-06 22:16:20

How to sample during normalization?

If I understand correctly what normalization is, then here is an example:
287e782b9459a835b7001b918556e915.jpg
Question 1: how can I select all users from Russia in this case, for example? (if there are many cities and countries in the database)
Question 2: if a query is made to a normalized table:
SELECT * FROM `юзеры` WHERE `id`='1'
Will it pull out all the data (city, region, country)? Or what would the region and country need to get a JOIN?
How to determine what data to display from a large table and bring to a normal form, and what to leave in a large table? (on 30 fields, for example).
So far, I understand that if the data is read more often, then you can leave it in a large table, but those that are often written should be placed in separate tables, i.e. do normalization.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vdem, 2014-09-06
@vdem

Spherical query in vacuum:

SELECT `юзеры`.*
FROM `юзеры`
    INNER JOIN `города`
        ON `города`.`id` = `юзеры`.`город_id`
    INNER JOIN `области`
        ON `области`.`id` = `города`.`область_id`
    INNER JOIN `страны`
        ON `страны`.`id` = `области`.`страна_id`
WHERE `страны`.`страна` = 'Россия'

It is clear that such a query will be somewhat slow, so I would make another table `addresses`, records from `users` will refer to it, and in this table to store both `city_id`, and `region_id`, and `country_id `. To increase performance, sometimes you have to abandon strict normalization, but in this version, at least the names are not duplicated, and if you need to change 'Moscow' to 'Moscowburg', then this will affect only one record.

X
xmoonlight, 2014-09-06
@xmoonlight

For this case, see nosql databases . There are multidimensional connections here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question