Skip to content

Instantly share code, notes, and snippets.

@vorlon001
Forked from maxik001/howto_seq_in_mysql_db
Created March 28, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vorlon001/039b5620e55b3f7c3b3f011990fd291c to your computer and use it in GitHub Desktop.
Save vorlon001/039b5620e55b3f7c3b3f011990fd291c 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