• 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$$

  • 相关阅读:
    通过Nginx,Tomcat访问日志(access log)记录请求耗时
    Nginx+Lua+Redis 对请求进行限制
    windows7+eclipse+hadoop2.5.2环境配置
    ubuntu + hadoop2.5.2分布式环境配置
    CentOS6.5上golang环境配置
    curl POST
    .sh 的运行
    CentOS 安装nginx
    Amazon ec2 改成密码登录方式
    SSH 服务器不用密码
  • 原文地址:https://www.cnblogs.com/sunss/p/1957195.html
Copyright © 2020-2023  润新知