How do I add more members to my ENUM-type column in MySQL?

前端 未结 7 1300
自闭症患者
自闭症患者 2020-12-02 08:05

The MySQL reference manual does not provide a clearcut example on how to do this.

I have an ENUM-type column of country names that I need to add more countries to. W

相关标签:
7条回答
  • It's possible if you believe. Hehe. try this code.

    public function add_new_enum($new_value)
      {
        $table="product";
        $column="category";
             $row = $this->db->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = ? AND COLUMN_NAME = ?", array($table, $column))->row_array();
    
        $old_category = array();
        $new_category="";
        foreach (explode(',', str_replace("'", '', substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE']) - 6)))) as $val)
        {
            //getting the old category first
    
            $old_category[$val] = $val;
            $new_category.="'".$old_category[$val]."'".",";
        }
    
         //after the end of foreach, add the $new_value to $new_category
    
          $new_category.="'".$new_value."'";
    
        //Then alter the table column with the new enum
    
        $this->db->query("ALTER TABLE product CHANGE category category ENUM($new_category)");
      }
    

    Before adding new value

    After adding new value

    0 讨论(0)
  • 2020-12-02 08:15
    ALTER TABLE
        `table_name`
    MODIFY COLUMN
        `column_name2` enum(
            'existing_value1',
            'existing_value2',
            'new_value1',
            'new_value2'
        )
    NOT NULL AFTER `column_name1`;
    
    0 讨论(0)
  • 2020-12-02 08:17

    Your code works for me. Here is my test case:

    mysql> CREATE TABLE carmake (country ENUM('Canada', 'United States'));
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SHOW CREATE TABLE carmake;
    +---------+-------------------------------------------------------------------------------------------------------------------------+
    | Table   | Create Table                                                                                                            |
    +---------+-------------------------------------------------------------------------------------------------------------------------+
    | carmake | CREATE TABLE `carmake` (
      `country` enum('Canada','United States') default NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
    +---------+-------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> ALTER TABLE carmake CHANGE country country ENUM('Sweden','Malaysia');
    Query OK, 0 rows affected (0.53 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> SHOW CREATE TABLE carmake;
    +---------+--------------------------------------------------------------------------------------------------------------------+
    | Table   | Create Table                                                                                                       |
    +---------+--------------------------------------------------------------------------------------------------------------------+
    | carmake | CREATE TABLE `carmake` (
      `country` enum('Sweden','Malaysia') default NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
    +---------+--------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    

    What error are you seeing?

    FWIW this would also work:

    ALTER TABLE carmake MODIFY COLUMN country ENUM('Sweden','Malaysia');

    I would actually recommend a country table rather than enum column. You may have hundreds of countries which would make for a rather large and awkward enum.

    EDIT: Now that I can see your error message:

    ERROR 1265 (01000): Data truncated for column 'country' at row 1.

    I suspect you have some values in your country column that do not appear in your ENUM. What is the output of the following command?

    SELECT DISTINCT country FROM carmake;

    ANOTHER EDIT: What is the output of the following command?

    SHOW VARIABLES LIKE 'sql_mode';

    Is it STRICT_TRANS_TABLES or STRICT_ALL_TABLES? That could lead to an error, rather than the usual warning MySQL would give you in this situation.

    YET ANOTHER EDIT: Ok, I now see that you definitely have values in the table that are not in the new ENUM. The new ENUM definition only allows 'Sweden' and 'Malaysia'. The table has 'USA', 'India' and several others.

    LAST EDIT (MAYBE): I think you're trying to do this:

    ALTER TABLE carmake CHANGE country country ENUM('Italy', 'Germany', 'England', 'USA', 'France', 'South Korea', 'Australia', 'Spain', 'Czech Republic', 'Sweden', 'Malaysia') DEFAULT NULL;
    0 讨论(0)
  • 2020-12-02 08:18

    FYI: A useful simulation tool - phpMyAdmin with Wampserver 3.0.6 - Preview SQL: I use 'Preview SQL' to see the SQL code that would be generated before you save the column with the change to ENUM. Preview SQL

    Above you see that I have entered 'Ford','Toyota' into the ENUM but I am getting syntax ENUM(0) which is generating syntax error Query error 1064#

    I then copy and paste and alter the SQL and run it through SQL with a positive result.

    SQL changed

    This is a quickfix that I use often and can also be used on existing ENUM values that need to be altered. Thought this might be useful.

    0 讨论(0)
  • 2020-12-02 08:18

    Here is another way...

    It adds "others" to the enum definition of the column "rtipo" of the table "firmas".

    set @new_enum = 'others';
    set @table_name = 'firmas';
    set @column_name = 'rtipo';
    select column_type into @tmp from information_schema.columns 
      where table_name = @table_name and column_name=@column_name;
    set @tmp = insert(@tmp, instr(@tmp,')'), 0, concat(',\'', @new_enum, '\'') );
    set @tmp = concat('alter table ', @table_name, ' modify ', @column_name, ' ', @tmp);
    prepare stmt from @tmp;
    execute stmt;
    deallocate prepare stmt;
    
    0 讨论(0)
  • 2020-12-02 08:34

    In MYSQL server version: 5.0.27 i tried this and it worked fine for me check in your version

    ALTER TABLE carmake
         MODIFY `country` ENUM('Japan', 'USA', 'England', 'Australia', 'Germany', 'France', 'Italy', 'Spain', 'Czech Republic', 'China', 'South Korea', 'India', 'Sweden', 'Malaysia');
    
    0 讨论(0)
提交回复
热议问题