给你个函数吧。DELIMITER $$DROP FUNCTION IF EXISTS `func_check_db_exists`$$CREATE FUNCTION `func_check_db_exists`(db_name varchar(64)) RETURNS varchar(20) CHARSET utf8 BEGIN if exists(select 1 from information_schema.schemata where schema_name=db_name) then return 'Exists!'; else return 'Not exists!'; end if; END$$DELIMITER ;
问题开始没有描述清楚啊,drop database if exists test2; create database if not exists test2;
drop database if exists test2; create database if not exists test2; 这个方法似乎不可以? exists 在 mysql里为不可识别关键字, 应该是: exit ? test2 这样的数据库名似乎应该加上 `` 在外?我用的 mysql 是 5.0……
看是否有异常或错误消息
BEGIN
if exists(select 1 from information_schema.schemata where schema_name=db_name) then
return 'Exists!';
else
return 'Not exists!';
end if;
END$$DELIMITER ;
我要的是在mysql的黑白框中输入sql语句,新建一个数据库,先判断这个数据库是否存在,如果有就删除这数据库,然后再创建它,否则直接创建
不是用use数据库名;
也不是用show databases;后再判断!
create database if not exists test2;
create database if not exists test2;
这个方法似乎不可以? exists 在 mysql里为不可识别关键字, 应该是: exit ?
test2 这样的数据库名似乎应该加上 `` 在外?我用的 mysql 是 5.0……