MySQL脚本,支持重复执行

MySQL脚本

一、建表删表

前言:以下脚本语句,使用前建议先进行自测一遍,如有错误及时提醒更正

(一) 新增表

**新增表的脚本可以从Navicat中转储导出,但是要更改下 建表语句 为 :CREATE TABLE IF NOT EXISTS **

1、模板

create table if not exists 数据库名.表名 (具体的建表语句
) engine = 存储引擎 default charset = 字符集 collate = 校对集 comment = '表注释'; 

2、参考样例

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE  IF NOT EXISTS `crr_policy_index`  (
    `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键自增',
    `index_code` varchar(32)   NOT NULL  DEFAULT '' COMMENT '指标唯一编码',
    `policy_id` int(10)   COMMENT '策略ID',
    `index_group` varchar(32)   NOT NULL  DEFAULT ''  COMMENT '分类',
    `version` int(4) NOT NULL DEFAULT 0 COMMENT '版本',
    `index_desc` varchar(128)    DEFAULT ''  COMMENT '描述',
    `index_name` varchar(32)   NOT NULL  DEFAULT ''  COMMENT '指标名称',
    `type` varchar(32)   NOT NULL  DEFAULT '' COMMENT '类型',
    `subject_labels` varchar(32)    DEFAULT '' COMMENT '主体名称',
    `expressions` varchar(128)  DEFAULT '' COMMENT '表达式',
    `options` longtext COMMENT '下拉框内容',
    `threshold_sign` varchar(32)    DEFAULT '' COMMENT '阈值符号',
    `threshold` varchar(32) NOT NULL  DEFAULT '' COMMENT '阈值',
    `modify_time` datetime(0)   DEFAULT now() COMMENT '修改时间',
    `operator_code` varchar(32)  DEFAULT '' COMMENT '操作人',
    PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8 COLLATE = utf8_general_ci comment = '指标策略表' ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

(二)删除表

1、模板

drop table if exists 数据库名.表名;

2、参考样例

drop table if exists crr_policy_index;

二、表字段更改

(一)表字段新增

1、模板

set @v_count = 0;
select count(*) into @v_count from information_schema.columns where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名' and column_name = '字段名';
set @sql = if(@v_count = 0,"增加字段语句","select '表名.字段名 is OK.'");
prepare stmt from @sql;
execute stmt;

2、参考样例

set @v_count = 0;
select count(*) into @v_count from information_schema.columns where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and column_name = 'process_instance';
set @sql = if(@v_count = 0,"alter table crr_policy_index add column process_instance varchar(255) NOT NULL COMMENT '流程实例id';","select '表名.字段名 is OK.'");
prepare stmt from @sql;
execute stmt;

(二)表字段删除或修改

1、模板

set @v_count = 0;
select count(*) into @v_count from information_schema.columns where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名' and column_name = '字段名';
set @sql = if(@v_count = 1,"删除或修改字段语句","select '表名.字段名 is deleted.'");
prepare stmt from @sql;

2、修改字段样例

set @v_count = 0;
select count(*) into @v_count from information_schema.columns where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and column_name = 'process_instance';
set @sql = if(@v_count = 1,"alter table crr_policy_index modify column process_instance varchar(255) NOT NULL COMMENT '流程实例id1';","select 'crr_policy_index.process_instance is deleted.'");
prepare stmt from @sql;
execute stmt;

3、删除字段样例

set @v_count = 0;
select count(*) into @v_count from information_schema.columns where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and column_name = 'process_instance';
set @sql = if(@v_count = 1,"alter table crr_policy_index drop column process_instance ;","select 'crr_policy_index.process_instance is deleted.'");
prepare stmt from @sql;
execute stmt;
-- DEALLOCATE PREPARE stmt; 释放

三、主键新增与删除

(一)新增主键

1、模板

set @v_count = 0;
select count(*) into @v_count from information_schema.table_constraints where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名' and CONSTRAINT_NAME = 'PRIMARY';
set @sql = if(@v_count = 0,"alter table 表名 add primary key (字段列表)","select '表名 PRIMARY KEY is OK.'");
prepare stmt from @sql;
execute stmt;

2、参考样例

set @v_count = 0;
select count(*) into @v_count from information_schema.table_constraints where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and CONSTRAINT_NAME = 'PRIMARY';
set @sql = if(@v_count = 0,"alter table crr_policy_index add primary key (id,index_code)","select 'crr_policy_index PRIMARY KEY is OK.'");
prepare stmt from @sql;
execute stmt;

(二)删除主键

1、模板

set @v_count = 0;
select count(*) into @v_count from information_schema.table_constraints where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名' and CONSTRAINT_NAME = 'PRIMARY';
set @sql = if(@v_count = 1,"alter table 表名 drop primary key ","select '表名 PRIMARY KEY is deleted.'");
prepare stmt from @sql;
execute stmt;

2、参考样例

set @v_count = 0;
select count(*) into @v_count from information_schema.table_constraints where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and CONSTRAINT_NAME = 'PRIMARY';
set @sql = if(@v_count = 1,"alter table crr_policy_index drop primary key ","select '表名 PRIMARY KEY is deleted.'");
prepare stmt from @sql;
execute stmt;

四、索引

(一)新增索引

推荐普通索引名的格式:idx_每列首字母(如果索引字段只有一个,建议索引名为 idx_表名_列名),如:idx_i_t, idx_index_group

推荐索引名的格式:uk_每列首字母(如果索引字段只有一个,建议索引名为 uk_表名_列名),如:uk_i_t, uk_index_group

1、普通索引

(1)模板

set @v_count = 0;
select count(*) into @v_count from information_schema.statistics where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名' and INDEX_NAME = '索引名';
set @sql = if(@v_count = 0,"create index 索引名 on 表名(字段列表)","select '表名 索引名 is OK.'");
prepare stmt from @sql;
execute stmt;

(2)参考样例

set @v_count = 0;
select count(*) into @v_count from information_schema.statistics where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and INDEX_NAME = 'idx_index_group';
set @sql = if(@v_count = 0,"create index idx_index_group1 on crr_policy_index(index_group)","select 'crr_policy_index idx_index_group is OK.'");
prepare stmt from @sql;
execute stmt;
2、唯一索引 加unique

(1)模板

set @v_count = 0;
select count(*) into @v_count from information_schema.statistics where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名' and INDEX_NAME = '索引名';
set @sql = if(@v_count = 0,"create unique index 索引名 on 表名(字段列表)","select '表名 索引名 is OK.'");
prepare stmt from @sql;
execute stmt;

(2)参考样例

set @v_count = 0;
select count(*) into @v_count from information_schema.statistics where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and INDEX_NAME = 'uk_i_t';
set @sql = if(@v_count = 0,"create unique index uk_i_t on crr_policy_index(index_group,type)","select 'crr_policy_index uk_i_t is OK.'");
prepare stmt from @sql;
execute stmt;

(二)修改索引

注意: 在MySQL中并没有提供修改索引的直接指令,一般情况下,我们需要先删除掉原索引,再根据需要创建一个同名的索引,从而变相地实现修改索引操作

(三)删除索引

注意这里是@v_count >= 1(多字段索引时)

(1)模板

set @v_count = 0;
select count(*) into @v_count from information_schema.statistics where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名' and INDEX_NAME = '索引名';
set @sql = if(@v_count >= 1,"alter table 表名 drop index 索引名","select '表名 索引名 is deleted.'");
prepare stmt from @sql;
execute stmt;

(2)参考样例

set @v_count = 0;
select count(*) into @v_count from information_schema.statistics where TABLE_SCHEMA='toolsforcreatetable' and TABLE_NAME='crr_policy_index' and INDEX_NAME = 'uk_i_t';
set @sql = if(@v_count >= 1,"alter table crr_policy_index drop index uk_i_t","select 'crr_policy_index uk_i_t is deleted.'");
prepare stmt from @sql;
execute stmt;

五、表数据增删改

(一)表添加数据

方式一: 先查后加

(1)模板

set @v_count = 0;
select count(*) into @v_count from 表名 where 主键条件;
set @sql = if(@v_count = 0, "insert into 表名(列名) values(列名);", "select '数据已存在.'");
prepare stmt from @sql;
execute stmt;
commit;

(2)参考样例

set @v_count = 0;
select count(*) into @v_count from 表名 where 主键条件;
set @sql = if(@v_count = 0, "INSERT INTO crr_policy_index(id, index_code, policy_id, index_group, version, index_desc, index_name, type, subject_labels, expressions, options, threshold_sign, threshold, modify_time, operator_code) VALUES (1, NULL, NULL, '1', 0, NULL, NULL, '2', NULL, NULL, NULL, NULL, NULL, 'current_timestamp()', NULL);", "select '数据已存在.'");
prepare stmt from @sql;
execute stmt;
commit;
方式二:先删后增

(1)模板

delete from 表名 where 主键条件;
insert into 表名(列名) values (列名);

(2)参考样例

delete from crr_policy_index where id = 1;

INSERT INTO crr_policy_index(id, index_code, policy_id, index_group, version, index_desc, index_name, type, subject_labels, expressions, options, threshold_sign, threshold, modify_time, operator_code) VALUES (1, NULL, NULL, '1', 0, NULL, NULL, '2', NULL, NULL, NULL, NULL, NULL, 'current_timestamp()', NULL);

(二)表删除数据

(1)模板

set @v_count = 0;
select count(*) into @v_count from 表名 where 主键条件;
set @sql = if(@v_count = 1, "delete 语句", "select '数据已删除.'");
prepare stmt from @sql;
execute stmt;
commit;

(2)参考样例

set @v_count = 0;
select count(*) into @v_count from crr_policy_index where id = 1;
set @sql = if(@v_count = 1, "delete from crr_policy_index where id = 1;", "select '数据已删除.'");
prepare stmt from @sql;
execute stmt;
commit;
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 5.7可以使用事件(Event)来实现定时执行SQL脚本的功能。事件是MySQL中的一种特殊对象,可以在指定的时间间隔内自动执行一系列SQL语句。 要使用事件功能,首先需要确保MySQL服务器已启用事件调度器。可以通过在MySQL配置文件中添加以下行来启用事件调度器: ``` event_scheduler=ON ``` 然后,可以使用以下语法创建一个事件: ```sql CREATE EVENT event_name ON SCHEDULE schedule [ON COMPLETION [NOT] PRESERVE] DO event_body; ``` 其中,event_name是事件的名称,schedule是事件的调度规则,event_body是事件要执行SQL语句。 调度规则可以使用以下几种方式之一: - 每隔固定时间执行一次: ```sql EVERY interval ``` 其中,interval可以是一个时间段,例如1 HOUR、1 DAY等。 - 在指定的时间执行一次: ```sql AT timestamp ``` 其中,timestamp是一个具体的日期和时间,例如'2022-01-01 00:00:00'。 - 在指定的时间段内重复执行: ```sql EVERY interval STARTS timestamp ENDS timestamp ``` 其中,interval是一个时间段,timestamp是开始和结束的日期和时间。 以下是一个创建事件的示例: ```sql CREATE EVENT my_event ON SCHEDULE EVERY 1 DAY DO BEGIN -- 执行SQL语句 INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2'); END ``` 这个事件将在每天执行一次,向my_table表中插入一条记录。 注意,创建事件需要具有适当的权限。如果没有足够的权限,可以联系MySQL管理员进行授权。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值