mysql實現sequence
-----mysql 建立sequence
-- 建立隨機數表
drop table if exists test.sequence_test_hsg;
create table test.sequence_test_hsg (
seqname VARCHAR(50) NOT NULL, -- 序列名稱
currentval INT NOT NULL, -- 當前值
incrementval INT NOT NULL DEFAULT 1, -- 步長(跨度)
PRIMARY KEY (seqname)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入隨機數資訊
INSERT INTO test.sequence_test_hsg VALUES ('sq_ryid', '1000000000', '1');
-- 查詢
SELECT * FROM test.sequence_test_hsg;
-- 建立獲取當前隨機數函式
-- DROP FUNCTION currval;
DELIMITER $
CREATE FUNCTION currval (seq_name VARCHAR(50))
RETURNS INTEGER
BEGIN
DECLARE current INTEGER;
SET current = 0;
SELECT currentval INTO current
FROM test.sequence_test_hsg
WHERE seqname = seq_name;
RETURN current;
END$
DELIMITER ;
-- 取下一個值函式
-- DROP FUNCTION nextval;
DELIMITER $
CREATE FUNCTION nextval (seq_name VARCHAR(50))
RETURNS INTEGER
CONTAINS SQL
BEGIN
UPDATE test.sequence_test_hsg
SET currentval = currentval + incrementval
WHERE seqname = seq_name;
RETURN currval(seq_name);
END$
DELIMITER ;
-- 查詢
select currval('sq_ryid');
select nextval('sq_ryid');
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69990899/viewspace-2742908/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 「crudapi」零程式碼實現訂單序列號SequenceAPI
- sequence to sequence模型模型
- 崑崙分散式資料庫Sequence功能及其實現機制分散式資料庫
- MySQL MVCC實現原理MySqlMVC
- MySQL實現Split用法MySql
- MySQL——索引實現原理MySql索引
- mysql實現nextVal功能MySql
- Keras版Sequence2Sequence對對聯實戰——自然語言處理技術Keras自然語言處理
- Sequence recognition
- MySQL事務實現原理MySql
- MySQL排名函式實現MySql函式
- Mysql表分割槽實現MySql
- mysql索引底層實現MySql索引
- MySQL分析函式實現MySql函式
- MySQL實現分組排序MySql排序
- Rainbow Bracket SequenceAIRacket
- Increasing Sequence with Fixed OR
- PostgreSQL 序列(Sequence)SQL
- ORACLE SEQUENCE用法Oracle
- PostgreSQL sequence (一)SQL
- python sequence序列Python
- uvm的sequence
- MySQL怎麼實現主從同步和Django實現MySQL讀寫分離MySql主從同步Django
- 論文閱讀:Sequence to sequence learning for joint extraction of entities and relations
- MySQL高可用架構:mysql+keepalived實現MySql架構
- MySQL全面瓦解23:MySQL索引實現和使用MySql索引
- 13、MySQL Case-MySQL分析函式實現MySql函式
- Elasticsearch實現Mysql的Like效果ElasticsearchMySql
- MySQL 如何實現資料更新MySql
- MySQL 如何實現資料插入MySql
- mysql多表查詢如何實現MySql
- keepalived+MySQL實現高可用MySql
- MySQL主從分離實現MySql
- MySQL索引底層實現原理MySql索引
- mysql實現主從複製MySql
- HaProxy 實現 MySQL 負載均衡MySql負載
- Python實現MySQL連線池PythonMySql
- 使用mysql_markdown_win工具實現生成mysql文件MySql