How to define pre-defined types in MySQL?

5

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

In the combobox of the registration I already have the option masculine and feminine and in the base register like M or F, already having these two options there registered. So it would have to appear to the user 'Male' or 'Feminine' being that in the table of the database I have this as field sex having as option of this field only M or F.

    
asked by anonymous 16.08.2015 / 19:04

1 answer

12

MySQL has no resources to create a data domain as it exists in other databases.

>

The solution usually takes is to create a enumeration , although many say that the ideal is not to use this kind 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 labor. There is no way.

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

Another way is not to worry about this in the database, to register the letters referring to the genre in the column and to treat the descriptions only in the application. This information usually does not change.

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

    
16.08.2015 / 19:38