...

View Full Version : MySQL LENGTH parameter



nkline
03-28-2007, 01:02 AM
Greetings,

In short, the MySQL LENGTH parameter is driving me a little crazy.
For example, lets say we have a table named TEST with just one column called NUMBER.
The datatype for NUMBER is set as SMALLINT(3). "(3)" is the LENGTH parameter.
At first glance I would think that its purpose is to limit the number of digits allowed in the column called NUMBER. Maybe this really is its purpose and I'm just not understanding.
According to http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html :

" Another extension is supported by MySQL for optionally specifying the display width of integer data types in parentheses following the base keyword for the type (for example, INT(4)). This optional display width is used to display integer values having a width less than the width specified for the column by left-padding them with spaces.

The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range allowed by three characters are displayed using more than three characters. "

I decided to test this out using the TEST table mentioned above:

Table Name: TEST
Table Info:

Field____Type_________Null____Key____Default____Extra
NUMBER___smallint(3)__YES_____blank__NULL_______blank

Inserting 12345678 into NUMBER results in 32767 being inserted instead.
This partially makes sense since SMALLINT has a range of -32768 to 32767.

My questions are:

1.) What's the point of declaring SMALLINT(3) then?

2.) The way the MySQL documentation makes it sound is that specifying the LENGTH parameter allows the range to be expanded by LENGTH. For example, SMALLINT(3) would mean -32768xxx to 32767xxx. Therefore, 12345678 should've been inserted.

3.) I was really hoping MySQL would've returned an error when trying to insert 12345678 into NUMBER, but it didn't. It inserted 32767 instead, appearing as though the command completed without error. Is there a way to make MySQL say "hey, the number you're trying to insert into this column is longer then 3 digits - access denied."?

I hope this makes sense. Thank you for your time,

*Nick*

guelphdad
03-28-2007, 02:23 AM
Here (http://www.guelphdad.wefixtech.co.uk/sqlhelp/numericdata.shtml) is your answer.

nkline
03-28-2007, 03:37 AM
guelphdad,

Thank you for the prompt reply and for pointing me to this clear explanation - it answers my first two questions.

Regarding my third question:

3.) I was really hoping MySQL would've returned an error when trying to insert 12345678 into NUMBER, but it didn't. It inserted 32767 instead, appearing as though the command completed without error. Is there a way to make MySQL say "hey, the number you're trying to insert into this column is longer then 3 digits - access denied."?.

Thanks,

*Nick*



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum