Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use INT, CHAR or VARCHAR for Social Security Number? [closed]

I am designing the database for a new project, and I am not sure if I should use INT, CHAR or VARCHAR for the Social Security Number of the employees. In my country the SSN is composed of 11 digits.

Notice that I'll be using an ID column as primary key on this table, so the SSN won't be used as key.

INT considerations: it won't be possible to store text incorrectly on the column, which is good, but I won't be able to make sure the inserted value has the right number of digits.

CHAR considerations: I can make sure entries have the right number of digits, but I can't be sure those will be numbers, and the length of the SSN might increase in the future.

VARCHAR considerations: doesn't have any of the benefits of the other two, but is the most flexible one and won't suffer if the length changes, for example.

Is there a standard data type for SSN? What do you guys use?

like image 552
Daniel Scocco Avatar asked Sep 03 '14 23:09

Daniel Scocco


People also ask

What data type should a Social Security number use?

The database should not allow anything that hurts the integrity of the data, but an invalid SSN isn't harmful in that way. I would use a varchar , as it offers the flexibility that you mention. It would take 13 bytes for most entries as a varchar , per MS's definition here.

Should I use varchar or int?

1 Answer. Int comparisons are faster than varchar comparisons, and ints take much less space than varchars. This is applicable true for both unindexed and indexed access. You can use an indexed int column to make it faster.

What data type should I use for a phone number?

I generally use VARCHARs to store telephone numbers. Storage is not so expensive these days that I benefit that much from the savings found by storing them as numeric values.


Video Answer


1 Answers

Although the SSN is only digits, it doesn't represent a single number. You won't ever be doing any numeric calculations on the whole SSN, so there is really not much point in storing it as a number.

Storing it as a number would use a little less space, but not much, as you would need a bigint (8 bytes) to hold an 11 digit number. Using a varchar would use 11 bytes for the characters and another byte for the length, so the difference isn't enough to motivate storing it as a number.

Regarding validating the length and content of the SSN, that's really the job of the user interface. The database should not allow anything that hurts the integrity of the data, but an invalid SSN isn't harmful in that way.

I would use a varchar, as it offers the flexibility that you mention.

like image 69
Guffa Avatar answered Oct 22 '22 02:10

Guffa