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 Data Types

MySQL supports a variety of data types in several categories: numeric types, date and time types, string (character and byte) types, and spatial types. Here's an overview of the most commonly used ones:

  1. Numeric Types: Numeric types are used to store numeric data.

    • INT: A standard integer. The range for signed integers is -2147483648 to 2147483647, and for unsigned integers, it is 0 to 4294967295.

    • TINYINT: A small integer. The range for signed integers is -128 to 127, and for unsigned integers, it is 0 to 255.

    • SMALLINT: A small integer. The range for signed integers is -32768 to 32767, and for unsigned integers, it is 0 to 65535.

    • MEDIUMINT: A medium-sized integer. The range for signed integers is -8388608 to 8388607, and for unsigned integers, it is 0 to 16777215.

    • BIGINT: A large integer. The range for signed integers is -9223372036854775808 to 9223372036854775807, and for unsigned integers, it is 0 to 18446744073709551615.

    • FLOAT and DOUBLE: For floating-point numbers.

    • DECIMAL: For precise fixed-point numbers, often used for financial data.

  2. Date and Time Types: These types are used to store date and time.

    • DATE: For date values in 'YYYY-MM-DD' format.

    • TIME: For time values in 'hh:mm:ss' format.

    • DATETIME: For combined date and time values in 'YYYY-MM-DD hh:mm:ss' format.

    • TIMESTAMP: Similar to DATETIME, but with automatic updating for record creation and modification.

    • YEAR: For year values.

  3. String Types: String types are used to store string data.

    • CHAR: Fixed-length character data, up to 255 characters.

    • VARCHAR: Variable-length character data, up to 65535 characters.

    • TEXT: For long text data.

    • BINARY and VARBINARY: For binary string data.

    • BLOB: For binary large object data, such as images or other binary files.

    • ENUM: For columns that have a known, fixed set of values.

  4. Spatial Types: Spatial types are used to store geometric and geographical information.

    • GEOMETRY: A type that can store any type of spatial data.

    • POINT: A point in a 2D space.

    • LINESTRING: A curve in a 2D space.

    • POLYGON: A polygon in a 2D space.

Each of these types has its own strengths and weaknesses, and is best suited to certain types of data. Choosing the right data type for your data is important to ensure the functionality and performance of your database.

  1. MySQL Numeric Data Types:

    • Description: Numeric data types in MySQL include INTEGER, FLOAT, DOUBLE, DECIMAL, etc. They are used to store various numerical values with different precision and scale.
    • Code:
      -- Example using numeric types
      CREATE TABLE numeric_table (
          int_col INT,
          float_col FLOAT,
          double_col DOUBLE,
          decimal_col DECIMAL(10, 2)
      );
      
  2. MySQL String Data Types:

    • Description: String data types like VARCHAR, CHAR, TEXT, etc., are used to store character-based data, with variations in length and storage characteristics.
    • Code:
      -- Example using string types
      CREATE TABLE string_table (
          varchar_col VARCHAR(255),
          char_col CHAR(10),
          text_col TEXT
      );
      
  3. MySQL Data Type Conversion:

    • Description: MySQL supports implicit and explicit data type conversion. Implicit conversion occurs automatically, while explicit conversion is done using functions like CAST or CONVERT.
    • Code:
      -- Example of explicit conversion
      SELECT CAST(int_col AS VARCHAR) FROM numeric_table;
      
  4. MySQL Data Types for Indexing:

    • Description: Indexes can be applied to various data types, including numeric and string types, to improve query performance.
    • Code:
      -- Example of indexing
      CREATE INDEX idx_numeric_col ON numeric_table(int_col);
      
  5. MySQL Spatial Data Types:

    • Description: Spatial data types, such as POINT, LINESTRING, POLYGON, are used for storing geometric and geographic data.
    • Code:
      -- Example using spatial types
      CREATE TABLE spatial_table (
          location POINT
      );
      
  6. MySQL JSON Data Type Usage:

    • Description: The JSON data type is used to store JSON-formatted data, allowing for efficient storage and querying of JSON documents.
    • Code:
      -- Example using JSON type
      CREATE TABLE json_table (
          data JSON
      );
      
  7. MySQL ENUM Data Type Examples:

    • Description: ENUM is used to store a set of predefined values. It can be helpful for columns with a limited set of possible values.
    • Code:
      -- Example using ENUM
      CREATE TABLE enum_table (
          status ENUM('active', 'inactive', 'pending')
      );
      
  8. MySQL SET Data Type in Queries:

    • Description: SET is used to store a set of values, allowing multiple selections. Queries involve using standard SQL syntax for filtering and manipulation.
    • Code:
      -- Example query with SET
      SELECT * FROM set_table WHERE 'value' IN (col_set);
      
  9. MySQL Data Types for Internationalization:

    • Description: MySQL supports character sets and collations for internationalization. Choose appropriate character sets for different languages.
    • Code:
      -- Example with character set and collation
      CREATE TABLE i18n_table (
          text_col VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci
      );