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 CHAR, VARCHAR, TEXT, ENUM, SET (string type)

MySQL provides several data types for storing strings:

  1. CHAR: The CHAR type is used to store fixed-length character strings. The length can be any value from 0 to 255. You must specify the length of the CHAR column upon creation. Right-padding of spaces is applied for values shorter than the specified length.

    Example:

    CREATE TABLE example (
        char_column CHAR(10)
    );
    

    To insert data:

    INSERT INTO example (char_column) VALUES ('abc');
    

    This will actually store 'abc ', as CHAR(10) reserves 10 characters for every entry.

  2. VARCHAR: The VARCHAR type is used to store variable-length character strings. The length can be any value from 0 to 65535. You must specify the maximum length of the VARCHAR column upon creation.

    Example:

    CREATE TABLE example (
        varchar_column VARCHAR(10)
    );
    

    To insert data:

    INSERT INTO example (varchar_column) VALUES ('abc');
    

    Unlike CHAR, VARCHAR does not pad values with spaces. The above example will store 'abc' as is.

  3. TEXT: The TEXT type is used to store long strings of character data. The maximum length is 65535 characters.

    Example:

    CREATE TABLE example (
        text_column TEXT
    );
    

    To insert data:

    INSERT INTO example (text_column) VALUES ('This is a long string of text.');
    
  4. ENUM: The ENUM type is used to store one of a predefined set of strings. It's very efficient and compact.

    Example:

    CREATE TABLE example (
        enum_column ENUM('Small', 'Medium', 'Large')
    );
    

    To insert data:

    INSERT INTO example (enum_column) VALUES ('Medium');
    
  5. SET: Similar to ENUM, the SET data type allows you to input one or more values from a predefined list, up to 64 total.

    Example:

    CREATE TABLE example (
        set_column SET('Red', 'Green', 'Blue')
    );
    

    To insert data:

    INSERT INTO example (set_column) VALUES ('Red,Green');
    

Remember to choose the most appropriate string type based on the nature of data you need to store. VARCHAR is generally good for most purposes, but CHAR can be more efficient for very short strings, TEXT for very long ones, and ENUM and SET for cases where you have a known set of values.

  1. MySQL String Data Types Comparison:

    • Description: MySQL offers various string data types to store character-based data. Understanding the differences between these types is crucial for efficient data storage and retrieval.
    • Code:
      -- Example using VARCHAR
      CREATE TABLE example_table (
          column_varchar VARCHAR(255),
          column_char CHAR(255),
          column_text TEXT
      );
      
  2. When to Use MySQL TEXT Data Type:

    • Description: Use the TEXT data type for large amounts of variable-length text data, such as long paragraphs or documents.
    • Code:
      -- Example using TEXT
      CREATE TABLE text_table (
          document TEXT
      );
      
  3. Working with MySQL ENUM and SET Data Types:

    • Description: ENUM and SET are used for storing static, predefined values. ENUM allows one value from a predefined list, while SET allows multiple values.
    • Code:
      -- Example using ENUM and SET
      CREATE TABLE enum_set_table (
          status ENUM('active', 'inactive', 'pending'),
          tags SET('red', 'green', 'blue')
      );
      
  4. Character Set and Collation in MySQL String Types:

    • Description: MySQL allows specifying character sets and collations for string types to handle different languages and sorting preferences.
    • Code:
      -- Example with character set and collation
      CREATE TABLE charset_collation_table (
          data VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci
      );
      
  5. Working with MySQL ENUM in Queries:

    • Description: Querying ENUM values involves using the predefined options in the WHERE clause or other conditional statements.
    • Code:
      -- Example query with ENUM
      SELECT * FROM enum_set_table WHERE status = 'active';