MySQL database data type - mysql

Im new to Database programming and I have a very basic question:
In my PHPMyAdmin GUI that Im using to create tables in my database, what does it mean when the column "type" (ie. datatype) has the data type and something in brackets after that.
For example:
int(20), bigint(30) .....
I understand the type int and bigint imply the number of bytes that are used and consequently the range of values that can be stored. But what does the value in the brackets mean?
What does the (20) and the (30) stand for.... what impact does this have on....
Sorry if the Q is basic, I am trying to understand databases....
Thanks a lot

Basically this is a Display Width.
I've found very good explanation of this concept here is so decided to not describe it myself and let you read it yourself from the original source.
In the same way that a max-length can be specified for string data types (e.g. VARCHAR(5) = Maximum 5 Characters), Numeric data type cells can have a "Display Length" specified ( E.g.: INT(5) ).
There is a common misconception that specifying a Display Length on an INT column will limit that column's range. As example, it is quite often thought that defining a column as INT(1) will reduce the column's unsigned range to 0 - 9, and that INT(2) would reduce the column's unsigned range to 0 - 99. This is not the case. An INT data column will ALWAYS have a viable unsigned range of 0 - 4294967295, or a signed range of -2147483648 to 2147483647, irrespective of the specified Display Width, whether it be 1 ( INT(1) ) or 20 ( INT(20) ).
Display width doesn't change storage requirements for a data type.
Display width doesn't alter the actual data in any way (ie: it stores the entire value for the data)
A column returns it's full value when called in a query, regardless of the display width (the book directly contradicts this claim it makes as seen above)

The value in the bracket is the size or length of the field. [Edit strike]If set to 2 a uint field can only host values from 0 to 99.[/strike] You can set this value on your own and thus save a bit of memory if you expect your values not to exceed this limitation. Useful in connection with varchar.
Here another thread about varchar sizes: What are the optimum varchar sizes for MySQL?

Link to the mysql doc which explains it http://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html

Related

why should we set field length while creating sql table?

I can't understand why should we set field length while creating SQL table fields.
INT(11),
VARCHAR(255),
Data lengths are another form of data validation - they allow you to easily constrain the data in the table and not allow values that are larger than your program's logic would allow.
These are two different cases.
INT(11)
The "length" of an integer is almost meaningless, and MySQL 8.0 deprecates this syntax. An INT is always a 32-bit integer, regardless of the length argument. The only practical use of the length argument is if you use:
INT(11) ZEROFILL
This pads the number with zeroes when you fetch it, not as it is stored. The number is still stored exactly the same as INT(2) or INT(50), as a 32-bit integer. See my answer to Types in MySQL: BigInt(20) vs Int(20)
VARCHAR(255)
It's necessary to define a length because that's how data types must work in relational theory. The definition of a data type is "a named, finite set of values." It can't be a finite set if the strings have infinite length.
There is also the practical reason: to store a string, MySQL precedes it with one or two bytes encoding the length, so it knows how many characters to read. One byte of length information is used if the length is up to 255. Two bytes of length information is used if the length is up to 65535. That's the maximum length supported for VARCHAR in MySQL.

Mysql zerofill length different from default field length

I am using MySQL and InnoDB.
I need to store a numeric id which length can vary but needs to be at least 10. For instance:
0000000001
11111111111 are both correct values.
Currently, I my column has the following attributes: bigint(10), unsigned zerofill. This works: if I try to insert "1" then "0000000001" is actually inserted, and if I insert a bigger number (with length>10) it also works.
So, in the end, what is the purpose of the length attribute in the field definition? I thought it was the maximum length, but apparently it is not the case...? Or is my current implementation going to crash eventually?
The length attribute is just a hint for MySQL how to format select query results in the command line client. Nothing more. It has no effect on the datatype actually. An int is an int with 4 bytes, no matter what length you specify. Same of course for bigint, but with 8 bytes.

MySQL - Size Limits to Integer Columns

I'm using phpMyAdmin to create my table structures.
I can read from the documentation pages on MySQL about size limits for Integer Types:
MySQL Integer Types Reference
So here is where I'm getting a little confused with creating a column.
I want to create a column in the table: tbl_note_categories called notescounter
I don't foresee myself creating thousands of noteids in the tbl_notes with any specific categoryid. But I do believe I'd create hundreds of notes to each categoryid.
I'm at that point of choosing between: tinyint, smallint, mediumint.
According the documentation link above, I'm guessing smallint is my best choice.
So here's my confusion. PhpMyAdmin asks for a Length/Values parameter to be specified.
I'm going to make sure this new column (notescounter) is unsigned, giving me up to 65536.
Does that mean I need the Length/Values to be (5)?
I'm guessing Length is character length, but I'm not sure. (comparing to varchar)
No, this is a common misconception about MySQL. In fact, the "length" has no effect on the size of an integer or the range of values it can store.
TINYINT is always 8 bits and can store 28 distinct values.
SMALLINT is always 16 bits and can store 216 distinct values.
INT is always 32 bits and can store 232 distinct values.
BIGINT is always 64 bits and can store 264 distinct values.
There's also a MEDIUMINT, but the engineers who work on MySQL tell me MEDIUMINT always gets promoted to a 32-bit INT internally, so there's actually no benefit to using MEDIUMINT.
The length is only for display, and this only matters if you use the ZEROFILL option.
See an example in my answer to What is the difference (when being applied to my code) between INT(10) and INT(12)?
Yes, you want to specify a length of 5.
In MySQL, the "length" attribute on the integer types is optional. It's a MySQL extension which is non-standard).
When it is omitted from the column declaration, MySQL provides a default value. For a SMALLINT UNSIGNED, the default value is 5.
This value does NOT have any impact on the range of values that can be stored for an integer type. It specifies a "display length", which is returned in resultset metadata, which a client can choose to use or ignore.
http://dev.mysql.com/doc/refman/5.5/en/numeric-type-attributes.html

Confuse about the size in mysql table fields

I just created one role table and i defined role_id tinyint(1), even though i was able to store 99, 200 max upto 255 as tinyint limit for unsigned. so then what is the meaning of passing this size in data-types?
when read little that, it is about the display width, but when i fetch result on my php page, it displays as they are stored in the table.
Any mentor, can you please guide me to clear it?
from 10.1.1. Numeric Type Overview:
M indicates the maximum display width for integer types. The maximum legal
display width is 255. Display width is unrelated to the range of values a type
can contain [...].
So tinyint(M), or tinyint(1) in your case, means that any value stored in role_id will be chopped to a length of one byte when being displayed by MySQL. Similarly tinyint(3) would case a one digit integer to be padded with two spaces. You can also use tinyint(3) zerofill to pad with 0s instead, but it has the side-effect of making your tinyints unsigned.
However, regardless how you specify your tinyints they will be stored internally as an integer in the range [-128,127] (signed) or [0,255] (unsigned), and so that's the value PHP will receive when querying your database.
You can use mysql_field_len and substr to limit the display width similarly with PHP.

What does the number in parenthesis really mean?

I always thought that the number in the parenthesis represented the field length?
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?
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.
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. ;)