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.
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 name | Description | Storage requirements |
---|---|---|
INT | Normal size integer | 4 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.
Type | Signed | Unsigned | ||
---|---|---|---|---|
Minimum value | Maximum value | Minimum value | Maximum value | |
-2147483648(-2 | 16) | 2147483647(-216) | 04294967295(-2 | 32) |
Use INT
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;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;
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!