【问题标题】:How do I change the data type for a column in MySQL?如何更改 MySQL 中列的数据类型?
【发布时间】:2010-11-24 07:40:24
【问题描述】:

我想将多列的数据类型从 float 更改为 int。最简单的方法是什么?

目前还没有数据需要担心。

【问题讨论】:

  • 为了明确这一点,即使列已经包含数据,下面的答案(使用ALTER TABLE)实际上也可以工作。但是,将浮点列转换为整数列会导致其中的任何非整数值四舍五入为最接近的整数。

标签: mysql


【解决方案1】:

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

ALTER TABLE tablename MODIFY columnname INTEGER;

这将改变给定列的数据类型

根据您希望修改的列数,最好生成一个脚本,或使用某种 mysql 客户端 GUI

【讨论】:

  • 友情提示 - 列的默认值为 NULLABLE,因此如果您有一个 NOT NULL 列,请不要忘记使用“MODIFY columnname INTEGER NOT NULL”,否则您会将列从 NOT NULL为 NULL。
  • 如果新的列类型不符合要求,alter table 是否会删除表上的数据或执行失败?
  • ALTER TABLE tablename MODIFY columnname INTEGER unsigned;
  • 我认为@Despertars 警告也可能与保留任何 CHARSET 或 COLLATE 规范有关。
【解决方案2】:
alter table table_name modify column_name int(5)

【讨论】:

    【解决方案3】:

    你也可以这样用:

    ALTER TABLE [tablename] CHANGE [columnName] [columnName] DECIMAL (10,2)
    

    【讨论】:

      【解决方案4】:

      如果要将某种类型的所有列更改为另一种类型,可以使用如下查询生成查询:

      select distinct concat('alter table ',
                             table_name,
                             ' modify ',
                             column_name,
                             ' <new datatype> ',
                             if(is_nullable = 'NO', ' NOT ', ''),
                             ' NULL;')
        from information_schema.columns
        where table_schema = '<your database>' 
          and column_type = '<old datatype>';
      

      例如,如果要将列从 tinyint(4) 更改为 bit(1),请像这样运行它:

      select distinct concat('alter table ',
                             table_name,
                             ' modify ',
                             column_name,
                             ' bit(1) ',
                             if(is_nullable = 'NO', ' NOT ', ''),
                             ' NULL;')
        from information_schema.columns
        where table_schema = 'MyDatabase' 
          and column_type = 'tinyint(4)';
      

      得到这样的输出:

      alter table table1 modify finished bit(1)  NOT  NULL;
      alter table table2 modify canItBeTrue bit(1)  NOT  NULL;
      alter table table3 modify canBeNull bit(1)  NULL;
      

      !!不保留唯一约束,但应该使用另一个if-参数轻松修复concat。如果需要,我会留给读者来实现。

      【讨论】:

        【解决方案5】:
        Alter TABLE `tableName` MODIFY COLUMN `ColumnName` datatype(length);
        

        例如:

        Alter TABLE `tbl_users` MODIFY COLUMN `dup` VARCHAR(120);
        

        【讨论】:

          【解决方案6】:

          要更改列数据类型,有 更改 方法和修改方法

          ALTER TABLE student_info CHANGE roll_no roll_no VARCHAR(255);
          
          ALTER TABLE student_info MODIFY roll_no VARCHAR(255);
          

          要更改字段名称,还可以使用 change 方法

          ALTER TABLE student_info CHANGE roll_no identity_no VARCHAR(255);
          

          【讨论】:

            【解决方案7】:

            你使用alter table ... change ...方法,例如:

            mysql> create table yar (id int);
            Query OK, 0 rows affected (0.01 sec)
            
            mysql> insert into yar values(5);
            Query OK, 1 row affected (0.01 sec)
            
            mysql> alter table yar change id id varchar(255);
            Query OK, 1 row affected (0.03 sec)
            Records: 1  Duplicates: 0  Warnings: 0
            
            mysql> desc yar;
            +-------+--------------+------+-----+---------+-------+
            | Field | Type         | Null | Key | Default | Extra |
            +-------+--------------+------+-----+---------+-------+
            | id    | varchar(255) | YES  |     | NULL    |       |
            +-------+--------------+------+-----+---------+-------+
            1 row in set (0.00 sec)
            

            【讨论】:

            • 我相信这是修改而不是改变,但也许两者都有效。
            【解决方案8】:

            https://dev.mysql.com/doc/refman/8.0/en/alter-table.html

            您还可以为列设置默认值,只需在值后添加 DEFAULT 关键字即可。

            ALTER TABLE [table_name] MODIFY [column_name] [NEW DATA TYPE] DEFAULT [VALUE];
            

            这也适用于 MariaDB(测试版 10.2)

            【讨论】:

              【解决方案9】:

              如果要更改列详细信息,设置默认值并添加注释,请使用此

              ALTER TABLE [table_name] MODIFY [column_name] [new data type] 
              DEFAULT [VALUE] COMMENT '[column comment]' 
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2023-04-04
                • 2020-07-27
                • 1970-01-01
                • 2012-02-20
                • 2014-12-03
                • 1970-01-01
                • 2017-05-16
                相关资源
                最近更新 更多