3

I have a simple question about database desing...

Let's say we have Table Customer with some fields:

(PK) Id, 
Firstname, 
Lastname, 
Address, 
City, 
(FK) Sex_Id...

So...

Would it be a good idea to have an additional table Table Sex where data about Sex ('M', 'W') would be saved?

Sex_Id,
Value

or should Sex values ('M' or 'W') be saved directly into table Customer? What about query speed etc.?

Thanks in advance, best Regards.

1
  • For a field like that, where you're pretty sure you'll never have more than possibly 3 values (male, female, unknown/irrelevant = for companies etc.), a separate lookup table is probably overkill. Just use a CHAR(1) field with a CHECK CONSTRAINT to make sure you have nothing but values 'M', 'F', 'U' (or whatever you want to use for the "unknown/irrelevant" case) and that will be just fine
    – marc_s
    Dec 22, 2010 at 19:10

5 Answers 5

2

Or, one could use an existing standard. ISO 5218 covers four codes:

0 = Not Known
1 = Male
2 = Female
9 = Not applicable (lawful person such as corporation, organization etc)

ISO 5218 is a legal encoding and does not apply for medical/biological aspect.

Obviously, a reference table containing those codes should use the natural key (as per above list), and not a syntetic key.

Joe Celko's Data Measurements And Standards in SQL is a great (albeit boring) read.

1

You could try a multivalued attribute, but I prefer to do this: If there are only 2 values, you could consider using a BOOL type for that attribute in your DB and making 0 = Male and 1 = Female (commenting, of course, to avoid confusion). When data is entered in the external program (given there is one), you could just do a quick mapping where if they check "male", the attribute is 0 in the DB, and if they check "female", the attribute value is 1 in the DB.

9
  • Sadly sex has more than 2 values. You also may have undefined and unknown (null). The first one unlikeky out side the medical field, the second onw... lets say you get a customer, name Michele, what do you put in? Hint: iti s a man's name, too, in some countries.
    – TomTom
    Dec 22, 2010 at 19:21
  • So you're specifying the sex on a form that somebody else filled out? That doesn't sound right. As for the null value, you could just allow for NULLs for that attribute, and then when you read it from the DB, again, instead of "Michele is male", simple mapping where result==NULL could read "Michele did not specify a gender". Even in the medical world (where I currently work), it's usually M, F, or "prefer not to respond", which triggers the null response in my DBs. Out of curiosity, what letters have you seen other than M or F?
    – rownage
    Dec 22, 2010 at 19:25
  • @TomTom: for humans, there are only 2 known values. The rest is unknown or preferably unknown. Unless eman planning to save Trolls ?
    – iDevlop
    Dec 22, 2010 at 19:26
  • Or, with credit to marc_s in the comment on your question, use CHECK constraints and allow for only one character if you're really that concerned with the third possibility. I just figured the BOOL type was much more efficient for the DB than checking constraints every time you look at the gender of an entry.
    – rownage
    Dec 22, 2010 at 19:29
  • No, 3. As I said - you may not KNOW the sex, so unknown is a third value (NULL). What you do if Micehele Stevens signs up - MAN OR WOMAN? Hint.... can be both. So, you leave it open for the moment. Also there are some few people that qualify, genetically, as both. Sorry. I went through that crap some years ago in a project.
    – TomTom
    Dec 23, 2010 at 7:59
0

How many different values are you planning on having for Sex? If you aren't going to be adding more possible values for that column, it doesn't make sense to use a foreign key.

1
  • I don't know. Sex was only an example for static values.
    – eman
    Dec 22, 2010 at 19:06
0

You can use a character for the column, storing "M" or "W", and also use a foreign key into a table (primary key of a character) if you need to store any more details about that thing; You get the benefit of easy to write/read queries (no join required) for basic stuff, but still have the possibility of adding more data later on.

That said, unless you actually do have more columns in your Sex table, you could probably not create it at all now and add it later when you actually do have a need for it.

0

in your example, the extra table does not buy you anything.

@marc_s has the right idea here to add a good CHECK CONSTRAINT to make sure the local values are in the proper subset.

now if your example contained additional attributes on the related object, like a 'name' or'description' or further links to other objects like 'alias' or some kind of date range - then absolutely yes, create another table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.