Jump to content

MySQL LENGTH parameter


nick1

Recommended Posts

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*

Link to comment
Share on other sites

From the page you referenced in your original post:

 

"When asked to store a value in a numeric column that is outside the data type's allowable range, MySQL's behavior depends on the SQL mode in effect at the time. For example, if no restrictive modes are enabled, MySQL clips the value to the appropriate endpoint of the range and stores the resulting value instead. However, if the mode is set to TRADITIONAL, MySQL rejects a value that is out of range with an error, and the insert fails, in accordance with the SQL standard."

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.