Introduction to MySQL INTEGER Data Type

MySQL is a popular relational database management system that supports various data types. Among them, INTEGER is a commonly used data type for storing integer data.

Syntax

INTEGER is a data type in MySQL that represents integers with a range from -2147483648 to 2147483647. In MySQL, the INTEGER data type has several variants:

  • INT: Same as INTEGER.
  • TINYINT: Represents integers with a range from -128 to 127.
  • SMALLINT: Represents integers with a range from -32768 to 32767.
  • MEDIUMINT: Represents integers with a range from -8388608 to 8388607.
  • BIGINT: Represents integers with a range from -9223372036854775808 to 9223372036854775807.

Usage

INTEGER data type is commonly used for storing integer data, such as:

  • Storing the age of users.
  • Storing the quantity of orders.
  • Storing the price of products.

Examples

Here are two examples of using the INTEGER data type.

Example 1

Creating a table to store user information, with an ID column and an age column.

CREATE TABLE users (
  id INT PRIMARY KEY,
  age INT
);

Insert one row:

INSERT INTO users (id, age) VALUES (1, 20), (2, 30), (3, 40);

Query all rows from the table:

SELECT * FROM users;

Result:

+----+-----+
| id | age |
+----+-----+
|  1 |  20 |
|  2 |  30 |
|  3 |  40 |
+----+-----+

Example 2

Creating a table to store order information, with an ID column and a quantity column.

CREATE TABLE orders (
  id INT PRIMARY KEY,
  quantity INT
);

Insert a row:

INSERT INTO orders (id, quantity) VALUES (1, 10), (2, 20), (3, 30);

Query the row from the table:

SELECT * FROM orders;

Result:

+----+----------+
| id | quantity |
+----+----------+
|  1 |       10 |
|  2 |       20 |
|  3 |       30 |
+----+----------+

Conclusion

In MySQL, INTEGER data type is a commonly used data type for storing integer data. Depending on the specific requirements and range of data, different variants of INTEGER such as TINYINT, SMALLINT, MEDIUMINT, or BIGINT can be chosen. When using INTEGER data type, it’s important to be aware of the range of values the data type can hold, as well as the syntax rules for defining tables and inserting data.