K
K
Kirill Ushakov2016-07-05 13:12:32
MySQL
Kirill Ushakov, 2016-07-05 13:12:32

Automatic set in mysql?

Let's say there are three bases: Users, Groups and a linking user-group.
The first two have two fields, id and name. The third has three: id, uName, gName.
The uName and gName fields are of type set.
And now the question.
Maybe someone knows how when adding, for example, a user named John, to the user-group table in the uName field, the name was automatically added to the set ( set('John') )? And in general, is there such a possibility?
PS Do not ask why and why. I understand that there is not much logic here. Just need an example, or ways of similar implementation. Also it is not necessary, please, to advise to connect on keys. I know this very well.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Melkij, 2016-07-05
@melkij

Why set? A link table typically consists of a pair of foreign keys to the linked tables. Those. usually two ints and a composite primary key.
Possibility to write a trigger. But first I would like to understand the meaning.

R
Rsa97, 2016-07-05
@Rsa97

You can, of course, implement it through a trigger and ALTER TABLE, but in general SET is not intended for this. SET and ENUM are used when the list of values ​​is known in advance and limited.
PS And what, you can have all users at once in this field?

V
Vitaly, 2016-07-05
@vshvydky

and why the user should be duplicated in 2 tables at all, the name is written in the user, the user group is written in the ID from the user table
, you can generally make the user group not a table, but a view. And you can also have 2 tables, users - id, username, groupid and groups - id, groupname
and the connection will be through users.groupid = groups.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question