How to define predefined types in Mysql?

Asked

Viewed 3,246 times

5

In the site I’m developing I will, for example, a registration form where the user should define their gender in a combobox (male or female). The table users in the database will have all user fields as string, date, float. How do I define the sexo?

So that in the combobox registration I already load the male and female option and the base register as M or F, already having these two options there registered. Thus it would have to appear to the user 'Male' or 'Female' and in the table of the database I have it as field sex having as option of this field only M or F.

  • If I understood correctly, would you just make this "de-stop" between Masculine be M and Female be F in the application before retrieving/saving this information in the database. This is your question?

1 answer

12


Mysql has no resource to create a data domain as exists in other databases.

The commonly adopted solution is to create a enumeration, although many say that the ideal is not to use this type of data. Or create an auxiliary table with the data you need and make a relationship.

The second form has several ways of doing according to your need. But all will involve some manual work. There is no way.

In this case the most common to get the possible values is just to look at the auxiliary table, in this case it would be the table of sex or gender as it would be a more correct nomenclature. When you make a query in the user table you will have to make a JOIN for the sex column to take the description in the auxiliary table instead of the chosen sex code/id.

Another way is not to worry about it in the database, register the letters referring to gender in the column and treat the descriptions only in the application. In general this information does not change.

As a curiosity there is a standard to represent gender data and contrary to what people think, you should not use "M" or "F".

  • For some applications this table may be very restricted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.