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 INT, TINYINT, SMALLINT, MEDIUMINT, BIGINT (integer types)

MySQL provides several data types for storing integers:

  1. 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);
    
  2. 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);
    
  3. 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);
    
  4. 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);
    
  5. 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.

  1. MySQL Integer Data Types Comparison:

    • Description: MySQL provides various integer data types to store whole numbers with different ranges. Choosing the right type is crucial for efficient storage and performance.
    • Code:
      -- Example using different integer types
      CREATE TABLE int_table (
          col_tinyint TINYINT,
          col_smallint SMALLINT,
          col_mediumint MEDIUMINT,
          col_int INT,
          col_bigint BIGINT
      );
      
  2. Using MySQL TINYINT for Boolean Values:

    • Description: TINYINT can be used to represent boolean values, with 0 for false and 1 for true. It's more space-efficient than other integer types.
    • Code:
      -- Example using TINYINT for boolean values
      CREATE TABLE bool_table (
          is_active TINYINT(1)
      );
      
  3. Working with MySQL Integer Types in Queries:

    • Description: Querying involves using standard SQL operators and functions to filter and manipulate integer data.
    • Code:
      -- Example query with integer types
      SELECT * FROM int_table WHERE col_int > 100;
      
  4. MySQL TINYINT Signed vs Unsigned:

    • Description: TINYINT can be signed (allowing negative values) or unsigned (for non-negative values only). Choose based on the nature of your data.
    • Code:
      -- Example using unsigned TINYINT
      CREATE TABLE unsigned_table (
          value_unsigned TINYINT UNSIGNED
      );