跳至内容
wiki
用户工具
登录
站点工具
工具
显示页面
修订记录
反向链接
最近更改
媒体管理器
网站地图
登录
最近更改
媒体管理器
网站地图
您的足迹:
分享:技术:mysql:使用function实现类似oracle里的sequence序列
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== 使用function实现类似oracle里的sequence序列 ====== ===== 1.登录mysql进入具体的数据库下 ===== 用工具登录,或者linux下脚本:mysql -u username -p 输入密码,再use database ===== 2.创建function ===== <code> DELIMITER ;; CREATE DEFINER=`root`@`%` FUNCTION `_nextval`(n varchar(50)) RETURNS int(11) begin declare _cur int; set _cur=(select current_value from sequence where name= n); update sequence set current_value = _cur + increment_num where name=n; return _cur; end ;; DELIMITER ; </code> 如果这时候报错 <code> ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) </code> ,有可能是一个系统变量没有打开,执行以下命令,再重新创建function即可 <code> show variables like 'log_bin_trust_function_creators';#OFF set global log_bin_trust_function_creators=1;#修改mysql环境变量 show variables like 'log_bin_trust_function_creators';#ON </code> ===== 3.创建sequence表 ===== <code> CREATE TABLE `sequence` ( `NAME` varchar(50) NOT NULL, `CURRENT_VALUE` int(10) unsigned NOT NULL, `INCREMENT_NUM` int(10) unsigned NOT NULL DEFAULT '1', PRIMARY KEY (`NAME`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='序列表'; </code> ===== 4.插入sequence数据 ===== 每条记录指向一张表的序列,name为表名 <code> insert into sequence(name,current_value,increment_num) values('be_acco_capital_mode',1,1); </code> ===== 5.查询序列(自增) ===== <code> select * from sequence;#查看当前值,为1 select _nextval('be_acco_capital_mode') from dual;#查询表名对应的序列,_nextval的参数为对应的表名 select * from sequence;#查看下一个值,为2 </code>
分享/技术/mysql/使用function实现类似oracle里的sequence序列.txt
· 最后更改: 2016/04/08 09:42 由
gxx
页面工具
显示页面
修订记录
反向链接
回到顶部