A2-04-08.MySQL DATA TYPES-MySQL INT Data Type Explained With Examples

时间:2022-06-01 19:23:44

转载自:http://www.mysqltutorial.org/mysql-int/

MySQL INT Data Type Explained With Examples

 

Summary: in this tutorial, you will learn about MySQL INT or integer data type and how to use it in your database table design. In addition, we will show you how to use the display width and ZEROFILLattributes of an integer column in a table.

Introduction to MySQL INT type

In MySQL, INT stands for the integer that is a whole number. An integer can be written without a fractional component e.g., it is 1, 100, 4, -10, etc., and it cannot be 1.2, 5/3, etc. An integer can be zero, positive, and negative.

MySQL supports all standard SQL integer types INTEGER or INT and SMALLINT. In addition, MySQL provides TINYINT MEDIUMINT, and BIGINT as extensions to the standard SQL.

MySQL INT data type can be signed and unsigned. The following table illustrates the characteristics of each integer type including storage in bytes, minimum value, and maximum value.

Type Storage Minimum Value Maximum Value
  (Bytes) (Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127
    0 255
SMALLINT 2 -32768 32767
    0 65535
MEDIUMINT 3 -8388608 8388607
    0 16777215
INT 4 -2147483648 2147483647
    0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807
    0 18446744073709551615

Using INT in a column

Because integer type represents exact numbers, you usually use it as the primary key of a table. In addition, the INT column can have an AUTO_INCREMENT attribute.

When you insert a NULL value or 0 into the INT AUTO_INCREMENT column, the value of the column is set to the next sequence value. Notice that the sequence value starts with 1.

When you insert a value, which is not NULL or zero, into the AUTO_INCREMENT column, the column accepts the value. In addition, the sequence is reset to next value of the inserted value.

Let’s take a look at an example of a table that uses an integer column with the AUTO_INCREMENT  attributes.

First, create a new table named items with an integer column as the primary key using the following statement:

you can use either INT or INTEGER in the CREATE TABLE statement above because they are interchangeable. Whenever you insert a new row into the items table, the value of the item_id column is increased by 1.

Next, the following INSERT statement inserts three rows into the items table.

Then, query data from the items table using the following SELECT statement:

A2-04-08.MySQL DATA TYPES-MySQL INT Data Type Explained With Examples

After that, insert a new row whose value of the item_id column is specified explicitly.

Because the current value of the item_id column is 10, the sequence is reset to 11. If you insert a new row, the AUTO_INCREMENT column will use 11 as the next value.

Finally, query the data of the items table again to see the result.

A2-04-08.MySQL DATA TYPES-MySQL INT Data Type Explained With Examples

Note that since MySQL 5.1, the AUTO_INCREMENT column only accepts positive values. Negative values are not supported for the AUTO_INCREMENT column.

MySQL INT and display width attribute

MySQL provides an extension that allows you to specify the display width along with the INT data type. The display width is wrapped inside parentheses following the INT keyword e.g., INT(5) specifies an INT with the display width of five digits.

It is important to note that the display width attribute does not control the value ranges that the column can store. The display width attribute is typically used by the applications to format the integer values. MySQL includes the display width attribute as the metadata of the returned result set.

MySQL INT with ZEROFILL attribute

In addition to the display width, MySQL provides a non-standard ZEROFILL attribute. In this case, MySQL replaces the spaces with zero. Consider the following example.

First, create a table named zerofill_tests using the following statement:

Second, insert a new row into the zerofill_tests table.

Third, query data from the zerofill_tests table.

A2-04-08.MySQL DATA TYPES-MySQL INT Data Type Explained With Examples
The v1 column has a display width 2 including ZEROFILL.Its value is 1 therefore, you see 01 in the output. MySQL replaces the first space by 0.

The v2 column has a display with 3 including ZEROFILL. Its value is 6 therefore, you see 00 as the leading zeros.

The v3 column has the display width 5 with ZEROFILL, while its value is 9, therefore MySQL pads 0000 at the beginning of the number in the  output.

Note that if you use ZEROFILL attribute for an integer column, MySQL will automatically add an UNSIGNED attribute to the column.

In this tutorial, we have shown you how to use MySQL INT data type in the table and also introduced you to the display width and ZEROFILL attributes of an integer column.