Implement nextval functions in MySQL similar to those in Oracle

Source: Internet
Author: User

We know that sequence is not supported in MySQL. Generally, this field is auto-incrementing by the time the table is created.

For example, create table table_name (ID int auto_increment primary key ,...);

Or alter table table_ame add ID int auto_increment primary key // The field must be set to primary key.

Or reset the starting value of the auto-increment field. alter table table_name auto_increment = N

However, sequence_name.nextval is often used in Oracle, orProgramSelect sequence_name.value from dual. If our development framework must support both Oracle and MySQL. We generally propose sequence. If a similar function is provided in MySQL, it is more convenient to propose it. This is an application scenario. The following describes how to implement a nextval function in MySQL.

1. Create a table first

 1   Create   Table 'Sys _ Sequence '(  2 'Name' Varchar ( 50 ) Not   Null  ,  3 'Current _ value' Int ( 11 ) Not   Null   Default   '  0 '  ,  4 'Credentials' Int ( 11 ) Not   Null   Default   '  1  '  ,  5      Primary   Key  ('Name ')  6 )

 

2. Create a function

 delimiter $ drop function if exists 'currval' $ create definer = 'root' @ '%' function 'currval' (seq_name varchar (50) returns int (11) begin declare value integer; set value = 0; select current_value into value from sys_sequence where name = seq_name; return value; end $ delimiter; delimiter $ 
drop function if exists 'nextval' $ create functioncreate definer = 'root' @ '% 'function' nextval' (seq_name varchar (50 )) returns int (11) Begin update sys_sequence set current_value = current_value + increment where name = seq_name; return currval (seq_name); End
delimiter;
delimiter $
drop function if exists 'setval' $
Create definer = 'root' @ '% 'function' setval '(seq_name varchar (50 ), value integer) returns int (11) Begin update sys_sequence set current_value = value where name = seq_name; return currval (seq_name); End
delimiter;

Test select nextval ('name. Remove the <br> line feed in it, modify the style, and display it.

 

MySQL documentation see http://dev.mysql.com/doc/refman/5.1/zh/index.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.