MySQL implements a solution similar to Oracle sequence

MySQL implements a solution similar to Oracle sequence

MySQL implements Oracle-like sequences

Oracle generally uses sequences to process primary key fields, while MySQL provides increment to achieve similar purposes.

However, in actual use, it is found that MySQL's auto-increment has many disadvantages: it is impossible to control the step size, start index, loop, etc.; if the database needs to be migrated, the primary key is also a big problem.

This article records a solution for simulating Oracle sequences, focusing on ideas and code secondarily.

The use of Oracle sequences is nothing more than using the .nextval and .currval pseudo columns. The basic idea is:

1. Create a new table in MySQL to store sequence names and values;

2. Create a function to get the value in the sequence table;

The details are as follows:

The table structure is:

drop table if exists sequence;  
create table sequence (  
  seq_name VARCHAR(50) NOT NULL, -- sequence name current_val INT NOT NULL, -- current value increment_val INT NOT NULL DEFAULT 1, -- step length (span)  
  PRIMARY KEY (seq_name)  
);

Implementing a simulation of currval

create function currval(v_seq_name VARCHAR(50))  
returns integer 
begin 
  declare value integer;  
  set value = 0;  
  select current_value into value  
  from sequence 
  where seq_name = v_seq_name;  
  return value;  
end;

Function usage: select currval('MovieSeq');

Implementing the simulation scheme of nextval

create function nextval (v_seq_name VARCHAR(50))  
return integer 
begin 
 update sequence 
 set current_val = current_val + increment_val  
 where seq_name = v_seq_name;  
 return currval(v_seq_name);  
end;

The function is used as: select nextval('MovieSeq');

Function that increases the value of a setting

create function setval(v_seq_name VARCHAR(50), v_new_val INTEGER)  
returns integer 
begin 
 update sequence 
 set current_val = v_new_val  
 where seq_name = v_seq_name;  
return currval(seq_name);

Similarly, functions for step size operations can be added, which will not be described here.

Pay attention to the syntax, the database fields should correspond to

use bvboms; 
DELIMITER $$ 
create function setval(v_seq_name VARCHAR(50), v_new_val INTEGER) 
returns integer 
begin 
 update sequence 
 set current_val = v_new_val 
 where seq_name = v_seq_name; 
return currval(seq_name);
end $$
DELIMITER $$

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Detailed explanation of the solution for real-time synchronization from MySQL to Oracle
  • Summary of commonly used multi-table modification statements in Mysql and Oracle
  • Summary of the differences between MySQL and Oracle (comparison of functional performance, selection, SQL when using them, etc.)
  • Import backup between mysql database and oracle database
  • The implementation process of extracting oracle data to mysql database
  • Comparative Analysis of High Availability Solutions of Oracle and MySQL
  • Comparison of the differences between MySQL and Oracle: six triggers
  • Comparison of the differences between MySQL and Oracle: Five: Stored Procedures & Functions
  • MySQL and Oracle differences comparison three functions
  • Detailed explanation of the misunderstanding between MySQL and Oracle

<<:  How to implement MySQL master-slave replication based on Docker

>>:  JavaScript to achieve simple tab bar switching case

Recommend

Node implements search box for fuzzy query

This article example shares the specific code for...

Teach you how to implement Vue3 Reactivity

Table of contents Preface start A little thought ...

How to use JS to implement waterfall layout of web pages

Table of contents Preface: What is waterfall layo...

Summary of methods for cleaning Mysql general_log

Method 1: SET GLOBAL general_log = 'OFF';...

js to achieve simulated shopping mall case

Friends who are learning HTML, CSS and JS front-e...

CentOS 7.9 installation and configuration process of zabbix5.0.14

Table of contents 1. Basic environment configurat...

Implementation of building custom images with Dockerfile

Table of contents Preface Introduction to Dockerf...

Specific usage instructions for mysql-joins

Table of contents Join syntax: 1. InnerJOIN: (Inn...

Linux uses iftop to monitor network card traffic in real time

Linux uses iftop to monitor the traffic of the ne...

How to use Linux locate command

01. Command Overview The locate command is actual...

MYSQL database GTID realizes master-slave replication (super convenient)

1. Add Maria source vi /etc/yum.repos.d/MariaDB.r...

A simple method to regularly delete expired data records in MySQL

1. After connecting and logging in to MySQL, firs...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...

Detailed explanation of MySQL master-slave replication process

1. What is master-slave replication? The DDL and ...

Ideas and practice of multi-language solution for Vue.js front-end project

Table of contents 1. What content usually needs t...