MySQL添加可重复执行列
一、information_schema数据库表说明:
SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。
TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。
COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。
STATISTICS表:提供了关于表索引的信息。是show index from schemaname.tablename的结果取之此表。
USER_PRIVILEGES(用户权限)表:给出了关于全程权限的信息。该信息源自mysql.user授权表。是非标准表。
SCHEMA_PRIVILEGES(方案权限)表:给出了关于方案(数据库)权限的信息。该信息来自mysql.db授权表。是非标准表。
TABLE_PRIVILEGES(表权限)表:给出了关于表权限的信息。该信息源自mysql.tables_priv授权表。是非标准表。
COLUMN_PRIVILEGES(列权限)表:给出了关于列权限的信息。该信息源自mysql.columns_priv授权表。是非标准表。
CHARACTER_SETS(字符集)表:提供了mysql实例可用字符集的信息。是SHOW CHARACTER SET结果集取之此表。
COLLATIONS表:提供了关于各字符集的对照信息。
COLLATION_CHARACTER_SET_APPLICABILITY表:指明了可用于校对的字符集。这些列等效于SHOW COLLATION的前两个显示字段。
TABLE_CONSTRAINTS表:描述了存在约束的表。以及表的约束类型。
KEY_COLUMN_USAGE表:描述了具有约束的键列。
ROUTINES表:提供了关于存储子程序(存储程序和函数)的信息。此时,ROUTINES表不包含自定义函数(UDF)。名为“mysql.proc name”的列指明了对应于INFORMATION_SCHEMA.ROUTINES表的mysql.proc表列。
VIEWS表:给出了关于数据库中的视图的信息。需要有show views权限,否则无法查看视图信息。
TRIGGERS表:提供了关于触发程序的信息。必须有super权限才能查看该表
二、存储过程实现
思路:
1.先判断表是否存在;
2.判断列名是否存在该表中;
3.判断null、default是否存在属性中;
4.判断是否需要插入到某个列名之后;
5.拼装sql语句;
6.执行添加字段的sql语句;
1 drop procedure if exists `AddColumnIfNotExists`; 2 delimiter $$ 3 create procedure AddColumnIfNotExists(in tableName varchar(50),in columnName varchar(50),in discription varchar(256), in position varchar(50)) 4 begin 5 declare tableCount int default 0; #用于判断表是否存在 6 declare columnCount int default 0; #用于判断字段是否存在 7 declare stmt varchar(2000); #最终拼接出来的sql语句 8 declare seat int default 0; #用于判断null/default的位置 9 10 SELECT count(*) into @tableCount FROM information_schema.TABLES WHERE TABLE_SCHEMA = (SELECT DATABASE()) AND TABLE_NAME = tableName; 11 SELECT count(*) into @columnCount FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = (SELECT DATABASE()) AND TABLE_NAME = tableName AND COLUMN_NAME = columnName; 12 13 if @tableCount=1 && @columnCount=0 then 14 set @seat = locate('null', discription); 15 if @seat > 0 then #判断是否存在null 16 set @stmt = CONCAT(' ALTER TABLE `',tableName,'` ADD COLUMN `',columnName,'` ',discription); 17 else 18 set @seat = locate('default', discription); 19 if @seat > 0 then #判断是否存在default 20 set @stmt = CONCAT(' ALTER TABLE `',tableName,'` ADD COLUMN `',columnName,'` ',discription, ' NOT NULL'); 21 else 22 set @stmt = CONCAT(' ALTER TABLE `',tableName,'` ADD COLUMN `',columnName,'` ',discription, ' NULL'); 23 end if; 24 end if; 25 26 if position != '' then #判断是否需要插入某个字段的后面 27 set @stmt = CONCAT(@stmt, ' after ', position, ';'); 28 else 29 set @stmt = CONCAT(@stmt, ';'); 30 end if; 31 32 PREPARE stmt1 FROM @stmt; 33 EXECUTE stmt1; 34 DEALLOCATE PREPARE stmt1; 35 select @stmt; 36 end if; 37 end$$ 38 delimiter ;
三、测试
调用方式:call AddColumnIfNotExists(表名, 字段名,属性, 插入到那个字段之后);
1.新建一个员工表
#员工表 CREATE TABLE emp( empno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0 comment '编号', ename VARCHAR(20) NOT NULL DEFAULT "" comment '名字', job VARCHAR(9) NOT NULL DEFAULT "" comment '工作', mgr MEDIUMINT UNSIGNED NOT NULL DEFAULT 0 comment '上级编号' )ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
mysql> desc emp; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | empno | mediumint(8) unsigned | NO | | 0 | | | ename | varchar(20) | NO | | | | | job | varchar(9) | NO | | | | | mgr | mediumint(8) unsigned | NO | | 0 | | +-------+-----------------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
2.新插入字段
(1)无null、无default
mysql> call AddColumnIfNotExists('emp', 'hiredatea', 'DATE comment "入职时间"',''); +-------------------------------------------------------------------------+ | @stmt | +-------------------------------------------------------------------------+ | ALTER TABLE `emp` ADD COLUMN `hiredatea` DATE comment "入职时间" NULL; | +-------------------------------------------------------------------------+ 1 row in set (0.09 sec) Query OK, 0 rows affected (0.13 sec) mysql> desc emp; +-----------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-----------------------+------+-----+---------+-------+ | empno | mediumint(8) unsigned | NO | | 0 | | | ename | varchar(20) | NO | | | | | job | varchar(9) | NO | | | | | mgr | mediumint(8) unsigned | NO | | 0 | | | hiredatea | date | YES | | NULL | | +-----------+-----------------------+------+-----+---------+-------+ 5 rows in set (0.01 sec)
(2).有not null、无default
mysql> call AddColumnIfNotExists('emp', 'sal', 'DECIMAL(7,2) not null comment " 薪水"', ''); +---------------------------------------------------------------------------+ | @stmt | +---------------------------------------------------------------------------+ | ALTER TABLE `emp` ADD COLUMN `sal` DECIMAL(7,2) not null comment "薪水"; | +---------------------------------------------------------------------------+ 1 row in set (0.06 sec) Query OK, 0 rows affected (0.09 sec) mysql> desc emp; +-----------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-----------------------+------+-----+---------+-------+ | empno | mediumint(8) unsigned | NO | | 0 | | | ename | varchar(20) | NO | | | | | job | varchar(9) | NO | | | | | mgr | mediumint(8) unsigned | NO | | 0 | | | hiredatea | date | YES | | NULL | | | sal | decimal(7,2) | NO | | NULL | | +-----------+-----------------------+------+-----+---------+-------+ 6 rows in set (0.01 sec)
(3).无null、有default
mysql> call AddColumnIfNotExists('emp', 'comm', 'DECIMAL(7,2) default 0 comment "红利"', ''); +--------------------------------------------------------------------------------------+ | @stmt| +--------------------------------------------------------------------------------------+ | ALTER TABLE `emp` ADD COLUMN `comm` DECIMAL(7,2) default 0 comment "红利" NOT NULL; | +--------------------------------------------------------------------------------------+ 1 row in set (0.06 sec) Query OK, 0 rows affected (0.09 sec) mysql> desc emp; +-----------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-----------------------+------+-----+---------+-------+ | empno | mediumint(8) unsigned | NO | | 0 | | | ename | varchar(20) | NO | | | | | job | varchar(9) | NO | | | | | mgr | mediumint(8) unsigned | NO | | 0 | | | hiredatea | date | YES | | NULL | | | sal | decimal(7,2) | NO | | NULL | | | comm | decimal(7,2) | NO | | 0.00 | | +-----------+-----------------------+------+-----+---------+-------+ 7 rows in set (0.01 sec)
(4).有not null、有default、插入都某个字段之后
mysql> call AddColumnIfNotExists('emp', 'deptno', 'varchar(20) not null default "" comment "部门编号"', 'job'); +------------------------------------------------------------------------------------------------------+ | @stmt | +------------------------------------------------------------------------------------------------------+ | ALTER TABLE `emp` ADD COLUMN `deptno` varchar(20) not null default "" comment "部门编号" after job; | +------------------------------------------------------------------------------------------------------+ 1 row in set (0.06 sec) Query OK, 0 rows affected (0.13 sec) mysql> desc emp; +-----------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-----------------------+------+-----+---------+-------+ | empno | mediumint(8) unsigned | NO | | 0 | | | ename | varchar(20) | NO | | | | | job | varchar(9) | NO | | | | | deptno | varchar(20) | NO | | | | | mgr | mediumint(8) unsigned | NO | | 0 | | | hiredatea | date | YES | | NULL | | | sal | decimal(7,2) | NO | | NULL | | | comm | decimal(7,2) | NO | | 0.00 | | +-----------+-----------------------+------+-----+---------+-------+ 8 rows in set (0.01 sec)