How to insert columns at a specific position in existing table?

前端 未结 8 2094
野趣味
野趣味 2020-12-25 09:54

I created a table with 85 columns but I missed one column. The missed column should be the 57th one. I don\'t want to drop that table and create it again. I\'m looking to ed

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-25 10:33

    SET
        @column_name =(
        SELECT COLUMN_NAME
    FROM
        information_schema.columns
    WHERE
        table_schema = 'database_name' AND TABLE_NAME = 'table_name' AND ordinal_position = 56
    );
    SET
        @query = CONCAT(
            'ALTER TABLE `table_name` ADD `new_column_name` int(5) AFTER ',
            @column_name
        );
    PREPARE
        stmt
    FROM
        @query;
    EXECUTE
        stmt;
    DEALLOCATE
        stmt;
    

提交回复
热议问题