几个实用的mysql脚本

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='字段名';

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章