#修改字段名称以及备注
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change()
BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name ='test'/*表名*/ AND column_name = 'columns_test'/*字段名*/)
THEN
alter table test/*表名*/CHANGE COLUMN `columns_test` `columns_test`/*字段名*/ BIT NOT NULL/*警告:此处填写原本的正确的数据类型,如填写错误,整列数据完蛋*/ COMMENT '测试'/*备注*/;
ELSE
SELECT CONCAT('columns_test'/*字段名*/,'此字段不存在,无法修改');
END IF;
#如需继续修改其他字段,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#新建唯一约束脚本规则
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS(SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name = 'test'/*表名*/ AND column_name = 'columns_test'/*字段名*/)
THEN
IF NOT EXISTS (SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE table_schema=CurrentDatabase AND table_name = 'test'/*表名*/ AND column_name = 'columns_test' /*字段名*/AND constraint_name='un_uq'/*约束名*/) THEN
ALTER TABLE `test`/*表名*/ ADD UNIQUE KEY `un_uq`/*约束名*/ (columns_test/*字段名*/);
ELSE
SELECT CONCAT('un_uq'/*约束名*/,'此约束已经存在');
END IF;
ELSE
SELECT CONCAT('要创建约束的字段','columns_test','不存在');
END IF;
END//
DELIMITER ;
CALL schema_change();
删除唯一约束脚本规则
#删除索引
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS (SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE table_schema=CurrentDatabase AND table_name = 'test'/*表名*/ AND constraint_name='un_uq'/*约束名*/)
THEN
#在MySQL中并没有提供修改索引的直接指令,一般情况下,我们需要先删除掉原约束,再根据需要创建一个同名的约束,从而变相地实现修改约束操作
ALTER TABLE `test`/*表名*/ DROP KEY un_uq/*约束名*/;
ELSE
SELECT CONCAT('un_uq'/*约束名*/,'此约束不存在');
END IF;
#如需继续删除其他约束,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#新增表
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF NOT EXISTS(SELECT * FROM information_schema.TABLES WHERE table_schema=CurrentDatabase AND table_name='test1'/*表名*/)
THEN
#新增表sql
CREATE TABLE `test1` (
`idtest` INT(11) NOT NULL,
`columns_test` VARCHAR(100) NOT NULL COMMENT '测试',
PRIMARY KEY (`idtest`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
ELSE
SELECT CONCAT('test1'/*表名*/,'此表已存在');
END IF;
#如需继续新增其他表,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#新增字段
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change()
BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name ='test'/*表名*/ AND column_name = 'columns_test'/*字段名*/)
THEN
ALTER TABLE test/*表名*/ADD COLUMN `columns_test`/*字段名*/ VARCHAR(100) NOT NULL COMMENT '测试';
ELSE
SELECT CONCAT('columns_test'/*字段名*/,'此字段已经存在');
END IF;
#如需继续新增其他字段,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#修改字段数据类型
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change()
BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name ='test'/*表名*/ AND column_name = 'columns_test'/*字段名*/)
THEN
ALTER TABLE test/*表名*/MODIFY COLUMN `columns_test`/*字段名*/ BIT NOT NULL COMMENT '测试';
ELSE
SELECT CONCAT('columns_test'/*字段名*/,'此字段不存在,无法修改');
END IF;
#如需继续修改其他字段,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#删除字段
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change()
BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name ='test'/*表名*/ AND column_name = 'columns_test'/*字段名*/)
THEN
ALTER TABLE test/*表名*/DROP COLUMN `columns_test`/*字段名*/;
ELSE
SELECT CONCAT('columns_test'/*字段名*/,'此字段不存在,无法删除');
END IF;
#如需继续删除其他字段,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#新增索引
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS(SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name = 'test'/*表名*/ AND column_name = 'columns_test'/*字段名*/)
THEN
IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = 'test'/*表名*/ AND index_name = 'index_test'/*索引名*/) THEN
ALTER TABLE `test`/*表名*/ ADD INDEX index_test/*索引名*/ (`columns_test`/*字段名*/);
ELSE
SELECT CONCAT('index_test'/*索引名*/,'此索引已经存在');
END IF;
ELSE
SELECT CONCAT('要创建索引的字段','columns_test','不存在');
END IF;
#如需继续新增其他索引,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#修改索引
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = 'test'/*表名*/ AND index_name = 'index_test'/*索引名*/)
THEN
#在MySQL中并没有提供修改索引的直接指令,一般情况下,我们需要先删除掉原索引,再根据需要创建一个同名的索引,从而变相地实现修改索引操作
ALTER TABLE `test`/*表名*/ DROP INDEX index_test/*索引名*/;
ALTER TABLE `test`/*表名*/ ADD INDEX index_test/*索引名*/ (`columns_test`/*字段名*/);
ELSE
SELECT CONCAT('index_test'/*索引名*/,'此索引不存在');
END IF;
#如需继续修改其他索引,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#删除索引
USE DataBase;/*库名*/
DROP PROCEDURE IF EXISTS schema_change;
DELIMITER //
CREATE PROCEDURE schema_change() BEGIN
DECLARE CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;
IF EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = 'test'/*表名*/ AND index_name = 'index_test'/*索引名*/)
THEN
#在MySQL中并没有提供修改索引的直接指令,一般情况下,我们需要先删除掉原索引,再根据需要创建一个同名的索引,从而变相地实现修改索引操作
ALTER TABLE `test`/*表名*/ DROP INDEX index_test/*索引名*/;
ELSE
SELECT CONCAT('index_test'/*索引名*/,'此索引不存在');
END IF;
#如需继续删除其他索引,请在这里复制以上 IF NOT----END IF部份,并修改
END//
DELIMITER ;
CALL schema_change();
#创建函数
USE `DataBaseFunc`;
DROP FUNCTION IF EXISTS `first_func`;#在创建前先删除已有函数
DELIMITER $$
#函数实体
CREATE FUNCTION first_func(param1 VARCHAR(5),parmam2 VARCHAR(5),param3 VARCHAR(10))
RETURNS TINYINT
BEGIN
RETURN 1;
END$$
DELIMITER ;
#创建存储过程
USE `DataBaseProc`;
DROP PROCEDURE IF EXISTS `proc_test`;#在创建前删除已有存储过程
DELIMITER $$
#存储过程实体
CREATE PROCEDURE proc_test (OUT count_num INT)
READS SQL DATA
BEGIN
SELECT COUNT(*) INTO count_num FROM test;
END$$
DELIMITER ;