MySQL Forums
Forum List  »  Newbie

Re: Poblem with a create table Query
Posted by: Rick James
Date: October 18, 2014 12:47PM

> In the same way, i tried to use 'enum' for gender but then i saw on google that enum is difficult so i am now using char (1) for gender.

Difficult?

Each of these is equally efficient (space and speed); each has pros and cons:

gender ENUM('male', 'female') NOT NULL
gender TINYINT UNSIGNED NOT NULL COMMENT '0=male, 1=female'
gender CHAR(1) NOT NULL CHARACTER SET ascii COMMENT 'M=male, F=female'

Among the 3, there is no "best" way.

How often will you need to add another value? In the case of ENUM, the necessary ALTER is arguably more heavy weight than simply establishing a new value in TINYINT or CHAR.

TINYINT is arguably bad, since the application needs to remember the mapping between numbers and meaning.

A 4th way is to use TINYINT (or bigger int) and to have another table spelling out the mapping (0=male, etc). This becomes desirable when there are dozens/hundreds of distinct 'values'.

But...

* Do you want to include transvestites, etc? (There is a growing distinction between "gender" and "sexuality".)

* Will there be cases where you don't [yet] know the gender?
Plan A: state NULL instead of NOT NULL.
Plan B: (These are arguably better):
gender ENUM('unk', 'male', 'female') NOT NULL DEFAULT 'unk'
gender TINYINT UNSIGNED NOT NULL DEFAULT '0' COMMENT '0=unknown, 1=male, 2=female'
gender CHAR(1) NOT NULL CHARACTER SET ascii DEFAULT '?' COMMENT 'M=male, F=female; not the explicity ascii, not defaulting to utf8'

If you are teaching MySQL, please read this (and lots of other things):
http://mysql.rjweb.org/doc.php/ricksrots
I don't want you to be teaching your students things that will get them into trouble later. I have elaborated a lot on ENUM so that you can understand the issues, the alternatives, etc, and can be thinking about them.

Options: ReplyQuote


Subject
Written By
Posted
October 17, 2014 12:53PM
Re: Poblem with a create table Query
October 18, 2014 12:47PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.