3

I've built a JavaEE project and need to store the gender of a user in MySQL. And I want to show this property when adding a new user account. For example

<select name="gender">
    <option value="0">Male</option>
    <option value="1">Female</option>
</select>

Now I've found 2 approaches to store this property in MySQL.

  1. use integer/char to store gender column:

    gender tinyint(1) not null;
    

    in this case, I have to write these in my Java code:

    public class StaticData {
        public static final short MALE = 0;
        public static final short FEMALE = 1;
    }
    

    If so, my database will depend on my Java code to limit the value range and explain what 0/1 represents (low level depends on high level). I think that's not a good idea.

  2. use an enum to store gender column:

    gender enum("MALE", "FEMALE") not null
    

    and in this case, my Java code will depend on the enum's value. There are 2 ways to get these values:

    1) public enum Gender { MALE, FEMALE }, it's nearly the same problem as with method 1.

    2) retrieve these values from MySQL, such as "SHOW COLUMNS FROM user LIKE \"gender\"" and then use a regular expression to split the string. But this is a verbose method. And have to query from db everytime I load a JSP. I think it would be expensive.

Is there a common idiom for this? If not, what factors do I need to consider when choosing the appropriate solution for my project?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alexis
  • 1,080
  • 3
  • 21
  • 44
  • 1
    I will go with CHAR(1) storing F or M. It is more readable than 0,1. – Jonas T Oct 31 '12 at 08:28
  • 2
    Don't forget the other options, like 'Other', 'Rather not tell', ... – Mr47 Oct 31 '12 at 08:38
  • There's also handling other languages, and cases where you don't know (not specified). If they don't apply, you could also make it BOOL and make the field deliberately represent **is_female**, which would take care of most of the programming doubts of anybody looking at the database or code. – Stephen O'Flynn Oct 31 '12 at 08:49
  • @Mr47 How can someone have 'Other' gender? – talles Jun 07 '13 at 18:33
  • 2
    Possible duplicate of [Storing sex (gender) in database](https://stackoverflow.com/questions/4175878/storing-sex-gender-in-database) – Raedwald Oct 01 '19 at 11:17

2 Answers2

3

It's probably not needed for most applications to ask for gender, but if you are absolutely required, I would suggest you give the option for 'Other' and 'Unknown' as well, the former for people who don't exactly fit in the binary gender model, and another for if the person chose not to disclose it.

For those 4 values, the easiest might be to store this in your database as a CHAR(1), or an ENUM("F", "M", "O", "U") (note that I chose these alphabetically instead of the surprisingly typical 'male first').

Evert
  • 93,428
  • 18
  • 118
  • 189
1

I would make a Table "Gender" with an id as key and the Text wich should be shown to the user:

  • ID - Text
  • 1 - Male
  • 2 - Female

In the Person you save the gender-ID as foreign key.

When you want to show the person you join can join the gender to the person and print the text out. For the select control you select all rows in the gender table and generate the html with the rows.

If there is a new gender to support, you just add it in the database, no java code must be edited...

Salazaar
  • 384
  • 1
  • 9
  • Thx to your reply, as I said, everytime I load my JSP, it would lead to query db to retrive these values, wouldn't it be an expensive overhead? – Alexis Oct 31 '12 at 08:57
  • You want to cache things like the gender table, where the data almost never changes. We use [Ehcache](http://ehcache.org/) for that. Even if you dont cache it, its such a small table and an inexpensive select statement, it is also ok. – Salazaar Oct 31 '12 at 08:59
  • That's a good idea, it's that a common solution for your or for all business projects? :P – Alexis Oct 31 '12 at 09:01
  • 1
    I dont know if there is a pattern for that ;) Things like gender are "Value Ranges", you can also load them at application startup and save them internally in java. Keep in mind you have to restart the application for changes to take effect. – Salazaar Oct 31 '12 at 09:05
  • 1
    Don't forget to make it ready for other gender values as might be required in the future because of them being recognized by law. Here is an example form Germany: http://www.lifesitenews.com/news/generation-x-germany-to-allow-third-blank-gender-for-birth-certificates – Stiivi Jan 07 '14 at 12:47