mysql删除字段的方法总结
生活随笔
收集整理的這篇文章主要介紹了
mysql删除字段的方法总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
判斷字段是否存在的方法總結如下:
1.查找系統表
select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME from information_schema.COLUMNS where COLUMN_NAME='uu';
2.使用describe
describe cdb_posts first
存在第一列返回字段的名稱,不存在就返回null,
刪除方法:
如果刪除的時候涉及的表不多的話,直接:
alter table tb_name drop column col_name;
多的話,可以使用下面的方法:
存儲過程刪除
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`p_drop_uuid_uuname`$$
CREATE DEFINER=`root`@`%` PROCEDURE `p_drop_uu`()
BEGIN
declare _db_name char(30);
declare _tb_name char(30);
declare _col_name char(30);
declare no_more_row tinyint(1);
declare cur_uuid cursor for
select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME
from information_schema.COLUMNS
where COLUMN_NAME='uu';
declare continue handler for not found set no_more_row=1;
set no_more_row=0; -- 判斷是否結束的標志位
open cur_uuid;
repeat
fetch cur_uuid into _db_name,_tb_name,_col_name;-- 取記錄
-- select _db_name,_tb_name,_col_name;
set @_dt = concat("alter table ", _db_name,".", _tb_name, " drop column uu");
-- 在存儲過程中,想把一個變量當作SQL執行,只有用prepare;
prepare s1 from @_dt;
execute s1;
deallocate prepare s1;
until no_more_row
end repeat;
close cur_uuid;
END$$
總結
以上是生活随笔為你收集整理的mysql删除字段的方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 江南百景图灵猫花中眠神秘雕像位置介绍
- 下一篇: 《绝区零》旧城建区南段小卡格车刷新位置一