幾個實用的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='字段名';

 

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