Home  >  Article  >  Database  >  mysql int how many bytes

mysql int how many bytes

青灯夜游
青灯夜游Original
2022-06-15 17:51:516140browse

In mysql, the int type requires 4 bytes for storage. int represents a standard integer, which can represent ordinary-sized integers; the int type can be signed or unsigned, the unsigned range is "0~4294967295", and the signed range is "-2147483648~2147483647". Because the integer type represents an exact number, the int column is usually used as the primary key of the table, and the AUTO_INCREMENT attribute is set. Each time a record is added, the int column data will automatically grow with the same step size.

mysql int how many bytes

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

The main integer types provided by MySQL are TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT, and AUTO_INCREMENT auto-increment constraints can be added to their attribute fields.

The int type:

Type nameDescriptionStorage requirements
INTNormal size integer4 bytes

In MySQL, INT stands for standard integer, which It can be 1, 100, 4, -10 etc. It cannot be 1.2, 5/3 etc. Integers can be zero, positive and negative.

MySQL INT data type can be signed or unsigned.

##INT-2147483648(-2 2147483647(-204294967295(-2
TypeSignedUnsigned
Minimum valueMaximum valueMinimum valueMaximum value
16)16)32 )

Use INT

in the column because the integer type represents an exact number, So it is usually used as the primary key of the table. In addition, the INT column can have the AUTO_INCREMENT attribute, so that every time a record is added, the int column data will automatically grow with the same step size.

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

When you insert a non-zero or zero value into an AUTO_INCREMENT column, the column will accept NULL values. Additionally, the sequence is reset to the next value after the inserted value.

Let's look at an example of a table using an integer column with the AUTO_INCREMENT attribute.

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

CREATE TABLE items (
    item_id INT AUTO_INCREMENT PRIMARY KEY,
    item_text VARCHAR(255)
);

You can use INT or INTEGER on CREATE TABLE because they are equal. Whenever a new row is inserted 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.

INSERT INTO items(item_text)
VALUES('laptop'), ('mouse'),('headphone');

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

SELECT 
    *
FROM
    items;

mysql int how many bytes

After that, insert a new row, explicitly specifying the value of item_id.

INSERT INTO items(item_id,item_text)
VALUES(10,'Server');

Since the current value of the item_id column is 10, the sequence will be reset to 11. If a new row is inserted, the AUTO_INCREMENT column will use 11 as the next value.

INSERT INTO items(item_text)
VALUES('Router');

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

SELECT 
    *
FROM
    items;

mysql int how many bytes

Note: Since MySQL 5.1, the AUTO_INCREMENT column only accepts positive values. AUTO_INCREMENT columns do not support negative values.

Extension

After adding the AUTO_INCREMENT constraint, each value in the field is automatically increased. Therefore, it is impossible for this field to have the same value. Normally, AUTO_INCREMENT is used as a constraint on the id field, and the id field is used as the primary key of the table.

[Related recommendations:

mysql video tutorial]

The above is the detailed content of mysql int how many bytes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn