tags:

views:

106

answers:

2

I always thought that the number in the parenthesis represented the field lenght? However, I understand that is not always the case. Maybe it's a mysql issue? Someone told me if I set a field to 9 characters long, I can add a value that's more than 9 characters but only the first 9 will be saved.

Example ...

create table "person" ( id INT, age INT(2));

If that's the case, shouldn't I select something like TINYINT instead of INT for age?

+7  A: 

INT(2) will generate an INT with the minimum display width of 2:

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

this does not affect the range of possible values that can be stored in the field; neither is it the number of bytes used to store it. It seems to be only a recommendation for applications how to show the value, unless ZEROFILL is used (see the linked page).

A unsigned TINYINT (0...255) would probably do as well, unless cryopreservation takes a big step forward during the lifetime of your application.

Pekka
And it continues: "The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly."
Anders Fjeldstad
so why bother implying that the field can take a value of up to 2? I don't think that has anything to do w/ the length of the value for mysql's internal, correct? It's just a display thing.
luckytaxi
@Anders, is that a mysql issue or does it apply to postgres, oracle and others?
luckytaxi
@lucky it's only a display thing, and what's more, it seems to be nothing more than a recommendation for applications. The exception is if you use `ZEROFILL` in which case the value will be padded: `1` => `01`...
Pekka
@Anders I added the 2nd paragraph for clarity
Pekka
for vchar, `name(10)` means display up to 10 chars, I can fit more than 10 chars i assume?
luckytaxi
@luckytaxi no, for `varchar`, it is the actual maximum width that a value can take.
Pekka
nice, all this time ive been using INT. tinyint would've suffice for many of my fields. thx for the clarification.
luckytaxi
A: 

That's the case for field types like vchar, but for numbers it represents the number of bytes which it uses to represent the number. An integer of two bytes means you can hold a number 2^16 - 1 (8 bits in a byte, so 16 total). If it's age, I figure you ought to be safe with two bytes. ;)

Neil
I used to think the `(2)` stood for bytes too, but it's incorrect.
Pekka
ah ok, so it's more for vchar. So if I wanted to display SSN, I shouldn't have do `ssn INT(9)`
luckytaxi
Apparently I was mistaken. Listen to Pekka. o.O
Neil