1

I have a principle question regarding definition of a code in a relational database. Let's assume (case 1) we have a code, for example, nationality code:

1
2
...
23
...
125
...

What is the preferred datatype to use for this kind of code (for example INT(3) or VARCHAR(3)?). The range is between 1 and 999.

Now, let's assume we have again a numeric code (case 2) but with fixed length, for example, 4:

2342
3252
2641
...

What would be in this case the appropriate datatype?

In both cases there is no information, that is, the position of a digit has no meaning, the code is just a distinction id.

I would appreciate any comments. Thanks.

1
  • The answer might depend on whether it was actually a nationality code or something else, as there are standard abbreviations used in internationalization, e.g. "en-us". But typically you'd use some form of integer. Int16, int32, smallint. Depends on what is available in the RDBMS. Commented Mar 6, 2013 at 11:00

2 Answers 2

3

Integers are usually faster to search on than varchars and take up a little less space, so I'd say if there are no other factors involved, go for integer.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Performance is a good reason to choose integer.
@giordano Of course, performance gains/losses would depend on the RDBMS and the INT type you choose, so test for yourself to be sure.
1

Strings are larger, slower in database. If you don't need something like "0289", then go with integer.

If performance is very important for you, then you can event store it as integer "289" in database and in your application you can complete it to "0289" programmatically, but it really depends.

String indexes in database is not the preferred way btw.

1 Comment

Thanks! I will use integer. Your statement regarding index arise a question: index makes no sense for categorical variable, that is, it has to be convert/mapped to an integer and than indexed the new variable? Or do indexed categorical variables perform better than non indexed but it is not optimal and creates problems in very huge and busy database?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.