Introduction to MySQL INT Data Type

The MySQL INT data type can store integer values ranging from -2147483648 to 2147483647.

Syntax

The syntax for INT data type is as follows:

INT[(M)]

where M is an optional display width, ranging from 1 to 11, with a default of 11.

Use Cases

The INT data type is very common in MySQL and is used for storing integer values. It is typically used for storing quantities, counters, identifiers, and other integer values.

Examples

Here are two examples of using the INT data type:

Example 1

Create a table named students with an id column and an age column, where id is of integer type for storing unique identifiers for students, and age is of integer type for storing their ages:

CREATE TABLE students (
  id INT(11) NOT NULL AUTO_INCREMENT,
  age INT(11),
  PRIMARY KEY (id)
);

Example 2

Assuming there is a table named orders with an id column and a quantity column, where id is of integer type for storing unique identifiers for orders, and quantity is of integer type for storing order quantities:

CREATE TABLE orders (
  id INT(11) NOT NULL AUTO_INCREMENT,
  quantity INT(11),
  PRIMARY KEY (id)
);

Conclusion

The INT data type is one of the common data types used for storing integers in MySQL. It is suitable for storing quantities, counters, identifiers, and other integer values. When designing a MySQL database, appropriate data types should be chosen based on the actual requirements.