遇到错误#1060-重复“论坛启用”列

wooyq4lh  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(298)

这有什么问题?

ALTER TABLE `groups`
ADD COLUMN `forum_enabled`  enum('0','1') CHARACTER SET latin1 COLLATE 
latin1_swedish_ci NOT NULL DEFAULT '0' AFTER `admindeco`;

ALTER TABLE `user_badges`
ADD UNIQUE INDEX `user_id, badge_id`;

ALTER TABLE `furniture`
ADD COLUMN`behaviour_data`  int(11) NOT NULL DEFAULT 0 AFTER 
`interaction_type`;

ALTER TABLE `users`
MODIFY COLUMN `rank_vip`  int(1) NULL DEFAULT 1 AFTER `rank`;

ALTER TABLE `server_settings`
CHANGE COLUMN `variable` `key`  varchar(255) CHARACTER SET latin1 COLLATE 
latin1_swedish_ci NOT NULL DEFAULT 'server.variable' FIRST ;

    ALTER TABLE `catalog_deals`
    DROP COLUMN `page_id`,
    DROP COLUMN `cost_credits`,
    DROP COLUMN `cost_pixels`,
    MODIFY COLUMN `items`  text CHARACTER SET latin1 COLLATE 
    latin1_swedish_ci 
    NOT NULL AFTER `id`,
    ADD COLUMN `room_id`  int(11) NOT NULL AFTER `name`;

    SET FOREIGN_KEY_CHECKS=0;

我收到以下错误:
sql查询:alter table groups add column forum\u enabled enum('0','1')字符集latin1 collate latin1\u瑞典语\u ci not null admindeco后的默认值“0”;mysql说:文档#1060-重复的列名'forumŠu enabled'

tmb3ates

tmb3ates1#

从错误消息来看,您试图在第一条语句中添加的列似乎已经存在于表中。
一个简单的解决方案是在添加列之前删除它,如:

ALTER TABLE `groups` 
DROP COLUMN `forum_enabled`;

这可以与 ADD 命令,如:

ALTER TABLE `groups`
    DROP COLUMN `forum_enabled`,
    ADD COLUMN `forum_enabled`  enum('0','1') ...
;

还可以动态检查列是否存在,然后在需要时删除它,如:

IF EXISTS (
    SELECT 1 
    FROM information_schema.columns
    WHERE 
        table_name = 'groups' 
        AND column_name = 'forum_enabled'
        AND table_schema = DATABASE()
) THEN
    ALTER TABLE `groups` 
    DROP COLUMN `forum_enabled`;
END IF;

相关问题