Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to implement a separate table consisting of only two rows Female and Male?

Assume we have a Person table with 3 columns:

  1. PersonId of type int. (Primary key)
  2. Name of type string.
  3. GenderId of type int (Foreign key referencing Gender table).

The Gender table consists of 2 columns:

  1. GenderId of type int.
  2. Name of type string.

My question is: Is it worth implementing the Gender table? Or it causes performance degradation? What is the best way to handle this?

Edit 1:

I have to populate a drop down control with a list of fixed genders (female and male) in my UI.

like image 321
LaTeX Avatar asked Feb 14 '11 05:02

LaTeX


People also ask

Why is it better to store data in multiple tables?

In many cases, it may be best to split information into multiple related tables, so that there is less redundant data and fewer places to update.

What are the most common uses of tables and why?

Tables are used to organize data that is too detailed or complicated to be described adequately in the text, allowing the reader to quickly see the results. They can be used to highlight trends or patterns in the data and to make a manuscript more readable by removing numeric data from the text.

Can a database have multiple tables?

The majority of databases you'll work with as a developer will have more than one table, and those tables will be connected together in various ways to form table relationships.

What is table and types of table?

There are three types of tables: base, view, and merged. Every table is a document with its own title, viewers, saved visualizations, and set of data. The data in each type of table has different properties.


2 Answers

I think the best approach in this case is a compromise:

  1. Create a table called Gender with a single varchar column called 'Name' or 'Gender'. Gender is really a natural primary key. Put the values 'Male' and 'Female' in it.
  2. Create foreign key to your Person table on a column named 'Gender'.

Now you only need to query from one table, but you're still protected from data inconsistencies by the foreign key, and you can pull the values for your dropdown from the Gender table if you want to. Best of both worlds.

Additionally, it makes life easier for someone working in the database, because they don't need to remember which arbitrary ids you've assigned to Male/Female.

like image 93
Bennor McCarthy Avatar answered Sep 27 '22 18:09

Bennor McCarthy


If you have a field with only two possible values, you don't need another table for it. You can just use something like a BIT (0=male, 1=female) or a CHAR ('M' and 'F').

like image 28
Mark Cidade Avatar answered Sep 27 '22 19:09

Mark Cidade