MySQL定义函数出现This function has none of DETERMINISTIC 错误

今天学了一下关于MySQL的编程。
目的:测试插入100万条数数据。

-- 声明
delimiter $$

-- 定义函数
create function mock_data()
returns int
begin
	declare num int default 1000000;
	declare i int default 0;
		while i < num do
				insert into `user`(`c_id`,`name`,`age`,`phone`,`email`) values(1,concat('用户',i),floor(rand()*100),concat('18',ceil(rand()*999999999)),concat('8523',i,ceil(rand()*100),'@qq.com'));
			set i = i+1;
		end while;
		return i;
end;


-- 运行函数
select mock_data();

再运行定义函数时候,出现了错误提示内容:This function has none of DETERMINISTIC

原因:

这是我们开启了bin-log, 我们就必须指定我们的函数是否是
1 DETERMINISTIC 不确定的
2 NO SQL 没有SQl语句,当然也不会修改数据
3 READS SQL DATA 只是读取数据,当然也不会修改数据
4 MODIFIES SQL DATA 要修改数据
5 CONTAINS SQL 包含了SQL语句

其中在function里面,只有 DETERMINISTIC, NO SQL 和 READS SQL DATA 被支持。如果我们开启了 bin-log, 我们就必须为我们的function指定一个参数。


在MySQL中创建函数时出现这种错误的解决方法:
set global log_bin_trust_function_creators=TRUE;

或者是:

set global log_bin_trust_function_creators=1;

如果想要测试,可以创建表尝试一下。
 

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `c_id` int(11) NOT NULL COMMENT '城市id',
  `name` varchar(255) DEFAULT NULL COMMENT '名称',
  `age` int(11) unsigned NOT NULL DEFAULT '12' COMMENT '年龄',
  `at` timestamp NULL DEFAULT NULL,
  `phone` char(20) DEFAULT '15163628984',
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=MyISAM AUTO_INCREMENT=1002712 DEFAULT CHARSET=utf8


-- 测试索引的作用 对比一下查询时间
select * from `user` where 	`name`='用户999970';

-- 给字段 name 创建普通索引
create index user_name_index on `user`(name);


-- 再次测试查询速度 感受一下质的飞跃
select * from `user` where 	`name`='用户999970';
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值