几个实用的mysql脚本

本文介绍如何通过存储过程批量修改数据库中多个表的指定字段,并提供了查询特定表和字段的方法。适用于需要大规模数据更新及维护的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、修改数据库中所有表的某个同名字段列

CREATE  PROCEDURE `存储过程名`()
begin
-- 定义循环条件
declare flag int default 0; 
-- 保存表名
declare tname varchar(50);
-- 查询数据库super_star中含有school_code列的表,如果区分大小写使用binary column_name = 'school_code'
declare result cursor for select table_name from  information_schema.columns where table_schema = '数据库' and  table_name like '表';
-- 退出循环
declare continue handler for sqlstate '02000' set flag = 1; 
-- 打开游标
open result;
    while flag <> 1 do
    -- 游标指向下一个位置,可以有多个数据,比如fetch result into tname,ttype,...;
    fetch result into tname;
        -- 拼接字符串表名sql,根据需要使用concat函数连接
        -- set @execsql = concat('select * from  ',tname,' where school_code = ',oldimei,' ;'); 
         set @execsql = concat("update ",tname," set 字段 = '3636000538'"); 
				-- set @execsql = concat("delete from  ",tname); 
        prepare stmt from @execsql;
        execute stmt;
    end while;
end

2、表和字段查询

-- 查询有数据的表
select TABLE_NAME from information_schema.tables 
where table_rows>0 and table_name like '表名';

-- 根据表名查表
select table_name, table_comment  from information_schema.tables  
where table_schema='数据库' and table_type='base table'
and table_comment like '表名'

-- 根据表查字段
select column_name 字段编码,column_comment 字段名称
from information_schema.columns
where table_schema='数据库' and table_name = '表名' 
and column_name='字段名';

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值