Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-28-2007, 12:02 AM   PM User | #1
nkline
New Coder

 
Join Date: Nov 2005
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
nkline is an unknown quantity at this point
MySQL LENGTH parameter

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/...ric-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*
nkline is offline   Reply With Quote
Old 03-28-2007, 01:23 AM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Here is your answer.
guelphdad is offline   Reply With Quote
Old 03-28-2007, 02:37 AM   PM User | #3
nkline
New Coder

 
Join Date: Nov 2005
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
nkline is an unknown quantity at this point
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*
nkline is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:11 AM.


Advertisement
Log in to turn off these ads.