-- 新建表时判断表是否存在 CREATE TABLE If Not Exists `表名` ( `id` int(10) NOT NULL AUTO_INCREMENT , `plan_id` int(10) NULL , `bet_num` varchar(255) NULL , `vr_win_data` varchar(255) NULL COMMENT , PRIMARY KEY (`id`) ); -- 新建字段时判断字段是否存在 CREATE PROCEDURE sp_update_888() BEGIN IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = '表名' AND column_name = '字段名') THEN ALTER TABLE 表名 ADD 字段名 int(1) default 0 ; END IF; end; call sp_update_888(); drop procedure IF EXISTS sp_update_888; -- 新建索引时判断索引是否存在 CREATE PROCEDURE sp_update_888() BEGIN IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=DATABASE() AND table_name = '表名' AND index_name = '索引名') THEN ALTER TABLE `chenrui_lottery_open_time` ADD INDEX 索引名( `索引字段` ); END IF; end; call sp_update_888(); drop procedure IF EXISTS sp_update_888;