Home > Database > Mysql Tutorial > body text

mysql在增加列前进行判断该列是否存在_MySQL

WBOY
Release: 2016-06-01 13:28:34
Original
1364 people have browsed it

bitsCN.com

mysql在增加列前进行判断该列是否存在

 

Mysql没有直接的语法可以在增加列前进行判断该列是否存在,需要写一个存储过程完成同样任务,

下面例子是:在T_DT_HOMEWORK表中增加一列RECOMMEND_RECORD列 

 

1create procedure add_col_homework() BEGIN 2IF EXISTS (SELECT column_name FROM information_schema.columns WHERE column_name = 'T_DT_HOMEWORK' AND column_name = 'RECOMMEND_RECORD')3THEN 4   ALTER TABLE `T_DT_HOMEWORK`  DROP COLUMN `RECOMMEND_RECORD`;5END IF; 6   ALTER TABLE `T_DT_HOMEWORK` ADD COLUMN  `RECOMMEND_RECORD`  varchar(20) CHARACTER   SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL AFTER `RECOMMEND_ORG_CODE`;7END;89drop procedure if exists add_col_homework;
Copy after login

 


bitsCN.com
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!