Home > Database > Mysql Tutorial > body text

mysql删除字段的方法总结_MySQL

WBOY
Release: 2016-06-01 13:51:17
Original
1144 people have browsed it

判断字段是否存在的方法总结如下:

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 $$<br><br>DROP PROCEDURE IF EXISTS `test`.`p_drop_uuid_uuname`$$<br><br>CREATE DEFINER=`root`@`%` PROCEDURE `p_drop_uu`()<br>BEGIN<br>    declare _db_name char(30);<br>    declare _tb_name char(30);<br>    declare _col_name char(30);<br>    declare no_more_row tinyint(1);<br>    <br>    declare cur_uuid cursor for <br>        select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME <br>            from information_schema.COLUMNS <br>                where COLUMN_NAME='uu';<br>    declare continue handler for not found set no_more_row=1;<br>    set no_more_row=0; -- 判断是否结束的标志位<br>    open cur_uuid;<br>        repeat <br>            fetch cur_uuid into _db_name,_tb_name,_col_name;-- 取记录<br>            -- select _db_name,_tb_name,_col_name;<br>            set @_dt = concat("alter table ", _db_name,".", _tb_name, " drop column uu");<br>            -- 在存储过程中,想把一个变量当作SQL执行,只有用prepare;<br>            prepare s1 from @_dt;<br>            execute s1;<br>            deallocate prepare s1;<br>            until no_more_row<br>        end repeat;<br>    close cur_uuid;<br>    END$$
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!