K
K
KarambiG2021-07-13 23:27:48
MySQL
KarambiG, 2021-07-13 23:27:48

Do I need to use relationships between mySQL tables?

Suppose I have a groups table, and a subscribers table (the id of users who joined the community - groups are stored here). That is, I select the groups table, do a join - that is, I attach the subscribes table by groups.id=subscribes.id_group. Well, in this way I understand whether the user is subscribed to a particular community or not.

It seems everything is ok. But recently I learned that there is a type of relationship many to many. Where in the database itself this connection needs to be created. The question arises, why indicate these links in the database, if this join works without them?

And the second mini-question.
I recently learned that when you make a database select, for example WHERE id=20, the database does not search for this record by enumeration starting from id=1, but accesses id=20 immediately. This was a shock for me. How does it find the entry right away? He's not Nostradamus)) Recommend someone an article on how the database works in this regard? And then in the internet I find only books of 700 pages :((

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-07-13
@galaxy

It seems everything is ok. But recently I learned that there is a type of relationship many to many. Where in the database itself this connection needs to be created. The question arises, why indicate these links in the database if this join works without them?

It sounds like subscribers is a many-to-many relationship (between the users and groups entities).
I recently learned that when you make a database select, for example WHERE id=20, the database does not search for this record by enumeration starting from id=1, but accesses id=20 immediately. This was a shock for me. How does it find the entry right away?

It doesn't find it right away. If there is no index, then there will be a complete enumeration (only not starting with id=1, id=2, but rather id=4324, id=88, id=281... - in the order they are in the table).
This can be compared to a library: if books are piled up, the librarian will have to go through them one by one until he finds the right one, say, "War and Peace". And if he has a catalog, he will quickly find a box with the letter "V", in it is a compartment "VL-VR", in it is a card "War and Peace" and in it there is already a rack, shelf and row where to look for a book.

S
SagePtr, 2021-07-14
@SagePtr

id you most likely have PRIMARY KEY, and the primary key is in itself a unique index.
It makes no sense to create an index for it separately.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question