Go How MySQL sets the auto-grow sequence SEQUENCE

Source: Internet
Author: User

Original address: 70148516

Solution: Because MySQL does not have sequence, so write a table (EMP_SEQ) that stores sequence, and then manually insert a piece of data and finally customize a function to handle the value to grow.

1. Create a table Emp_seq to hold the sequence value:
Description:
  • The Name column is intended to allow the function to perform DML operations better through conditions,
  • Start_value, which stores the value at the beginning of the increment sequence,
  • Increment_value, which stores the value of the self-increment sequence stepping.
    1. Create table Emp_seq (
    2. Name varchar () not Null primary key,
    3. Start_value int not null,
    4. Increment_value int not null default 1
    5. );
2. Insert the data manually:
Description:
  • ' Empno ' The meaning of the name,
  • The first 1, indicating that the sequence starts at 1,
  • The second 1 shows that the sequence grows by 1 each time.
insert into emp_seq values(‘empno‘,1,1);
3. Define the function Nextval:
Description:
  • DELIMITER//-Define the statement terminator, where//preceded by a space, we usually use semicolons; as a statement terminator. However, the created function, is a combination of multiple statements, each statement is also a semicolon, as a statement terminator, if the statement terminator is not redefined, the database will be considered declare I int; Here the semicolon is Terminator, will be an error. So here we redefine a statement terminator//until end;//ends the entire code.
  • function custom Functions (user-defined function UDF), which is an extension of the MySQL function,
  • Declare declaring variables
  1. DELIMITER//
  2. Create function nextval (str varchar) returns integer
  3. Begin
  4. declare i int;
  5. Set i= (select Start_value from emp_seq where name=str);
  6. Update Emp_seq
  7. Set Start_value=i+increment_value
  8. where name=str;
  9. return i;
  10. End
  11. //

4. Restore the default statement terminator:
Description:

DELIMITER; --Define the statement terminator (where delimiter is followed by a semicolon), which is the semicolon as the statement terminator. Because the function has already been set up, so here to redefine the Terminator, in order to make it easier to execute SQL statements later, we usually use the SQL statement, used to use a semicolon as a terminator.

    1. DELIMITER;

5. For more convenient execution of SQL commands, I copied the code into Notepad and saved the file to the D-Packing directory, renamed Emp_seq.sql 6. Executes an external SQL script command without prompting, stating that the build was successful. How to execute external SQL script commands, sir: MySQL executes commands for external SQL script files 7. After the SQL script is successfully imported, we need to verify, enter MySQL, and enter the database, I am jsd170101

OK, sequential execution of the SQL statement to get the sequence, from a clear view, the result starts from 1, each time you increment 1

8. Here's how to apply a DML statement in my database that already has empno=6 data, here I let start_value grow to 7, and then insert a message: View the results:

Go How MySQL sets the auto-grow sequence SEQUENCE

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.