Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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*

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Here is your answer.

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •