Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich is the best way to store a gender column in MySQL for a web project
    primarykey
    data
    text
    <p>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</p> <pre><code>&lt;select name="gender"&gt; &lt;option value="0"&gt;Male&lt;/option&gt; &lt;option value="1"&gt;Female&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Now I've found 2 approaches to store this property in MySQL.</p> <ol> <li><p>use integer/char to store gender column:</p> <pre><code>gender tinyint(1) not null; </code></pre> <p>in this case, I have to write these in my Java code:</p> <pre><code>public class StaticData { public static final short MALE = 0; public static final short FEMALE = 1; } </code></pre> <p>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.</p></li> <li><p>use an enum to store gender column:</p> <pre><code>gender enum("MALE", "FEMALE") not null </code></pre> <p>and in this case, my Java code will depend on the enum's value. There are 2 ways to get these values:</p> <p>1) public enum Gender { MALE, FEMALE }, it's nearly the same problem as with method 1.</p> <p>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.</p></li> </ol> <p>Is there a common idiom for this? If not, what factors do I need to consider when choosing the appropriate solution for my project?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload