Skip to content

Instantly share code, notes, and snippets.

@maxik001
Last active March 28, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxik001/04ab4ffd4016c35b8bfaaf7e042b7f0b to your computer and use it in GitHub Desktop.
Save maxik001/04ab4ffd4016c35b8bfaaf7e042b7f0b to your computer and use it in GitHub Desktop.
Create sequence in MySQL
SET GLOBAL log_bin_trust_function_creators = 1;
create database seq;
use seq;
create table `seq` (
`seq_name` varchar(20) NOT NULL,
`last_val` bigint(20) UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (`seq_name`)
) ENGINE=MyISAM
;
insert into seq.seq (seq_name) VALUE ('test_ids');
delimiter //
create function `get_next_seq_val`(`name` varchar(20))
returns bigint(20) NOT DETERMINISTIC
begin
update seq set last_val = last_insert_id(last_val + 1) where seq_name = name;
return last_insert_id();
end
//
delimiter ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment