MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
MySQL provides several data types for storing integers:
INT: The INT type is a standard integer with a range of -2147483648 to 2147483647 for signed numbers, or 0 to 4294967295 for unsigned numbers. It uses 4 bytes of storage.
Example:
CREATE TABLE example ( int_column INT );
To insert data:
INSERT INTO example (int_column) VALUES (123456789);
TINYINT: The TINYINT type is a very small integer with a range of -128 to 127 for signed numbers, or 0 to 255 for unsigned numbers. It uses 1 byte of storage.
Example:
CREATE TABLE example ( tinyint_column TINYINT );
To insert data:
INSERT INTO example (tinyint_column) VALUES (123);
SMALLINT: The SMALLINT type is a small integer with a range of -32768 to 32767 for signed numbers, or 0 to 65535 for unsigned numbers. It uses 2 bytes of storage.
Example:
CREATE TABLE example ( smallint_column SMALLINT );
To insert data:
INSERT INTO example (smallint_column) VALUES (32767);
MEDIUMINT: The MEDIUMINT type is a medium-sized integer with a range of -8388608 to 8388607 for signed numbers, or 0 to 16777215 for unsigned numbers. It uses 3 bytes of storage.
Example:
CREATE TABLE example ( mediumint_column MEDIUMINT );
To insert data:
INSERT INTO example (mediumint_column) VALUES (8388607);
BIGINT: The BIGINT type is a large integer with a range of -9223372036854775808 to 9223372036854775807 for signed numbers, or 0 to 18446744073709551615 for unsigned numbers. It uses 8 bytes of storage.
Example:
CREATE TABLE example ( bigint_column BIGINT );
To insert data:
INSERT INTO example (bigint_column) VALUES (9223372036854775807);
Remember to choose the most appropriate integer type based on the range of values you need to store. This can help save storage space and improve performance.
MySQL Integer Data Types Comparison:
-- Example using different integer types CREATE TABLE int_table ( col_tinyint TINYINT, col_smallint SMALLINT, col_mediumint MEDIUMINT, col_int INT, col_bigint BIGINT );
Using MySQL TINYINT for Boolean Values:
-- Example using TINYINT for boolean values CREATE TABLE bool_table ( is_active TINYINT(1) );
Working with MySQL Integer Types in Queries:
-- Example query with integer types SELECT * FROM int_table WHERE col_int > 100;
MySQL TINYINT Signed vs Unsigned:
-- Example using unsigned TINYINT CREATE TABLE unsigned_table ( value_unsigned TINYINT UNSIGNED );