Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference values: string or integer?

We have reference values created from a Sequence in a database, which means that they are all integers. (It's not inconceivable - although massively unlikely - that they could change in the future to include letters, e.g. R12345.)

In our [C#] code, should these be typed as strings or integers?

Does the fact that it wouldn't make sense to perform any arithmetic on these values (e.g. adding them together) mean that they should be treated as string literals? If not, and they should be typed as integers (/longs), then what is the underlying principle/reason behind this?

I've searched for an answer to this, but not managed to find anything, either on Google or StackOverflow, so your input is very much appreciated.

like image 261
ro͢binmckenzie Avatar asked Dec 30 '25 17:12

ro͢binmckenzie


2 Answers

There are a couple of other differences:

Leading Zeroes:

Do you need to allow for these. If you have an ID string then it would be required

Sorting:

Sort order will vary between the types:

Integer:

1
2
3
10
100

String

1
10
100
2
3

So will you have a requirement to put the sequence in order (either way around)?

The same arguments apply to your typing as applied in the DB itself too, as the requirements there are likely to be the same. Ideally as Chris says, they should be consistent.

like image 150
Jon Egerton Avatar answered Jan 01 '26 07:01

Jon Egerton


Here are a few things to consider:

  1. Are leading zeros important, i.e. is 010 different to 10. If so, use string.
  2. Is the sort order important? i.e. should 200 be sorted before or after 30?
  3. Is the speed of sorting and/or equality checking important? If so, use int.
  4. Are you at all limited in memory or disk space? If so, ints are 4 bytes, strings at minimum 1 byte per character.
  5. Will int provide enough unique values? A string can support potentially unlimited unique values.
  6. Is there any sort of link in the system that isn't guaranteed reliable (networking, user input, etc)? If it's a text medium, int values are safer (all non-digit characters are erraneous), if it's binary, strings make for easier visual inspection (R13_55 is clearly an error if your ids are just alphanumeric, but is 12372?)
like image 34
RoadieRich Avatar answered Jan 01 '26 08:01

RoadieRich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!