当前位置:网站首页>Kingbasees database administrator's Guide -- 15.2 Manage sequence
Kingbasees database administrator's Guide -- 15.2 Manage sequence
2022-07-21 14:55:00 【Thousands of sails passed by the side of the sunken boat_】
Catalog
You can perform the creation sequence 、 Modify the sequence 、 Tasks such as using sequences and deleting sequences .
A sequence is a database object , Used to automatically generate a set of rules ( Increase or decrease ) Serial numbers that vary continuously , It is generally applied to the primary key column of a table .
Use CREATE SEQUENCE Statement to create a sequence .
Use ALTER SEQUENCE Statement modification sequence .
For users , The available resources in the sequence are serial numbers , Serial numbers can be accessed by multiple users and incremented or decremented .
Sequence as a database object , The relevant information is stored in the data dictionary .
If a sequence is no longer needed , have access to DROP SEQUENCE Statement to delete the sequence .
Parent theme : Manage views 、 Sequences and synonyms
15.2.1. About sequence
A sequence is a database object , Used to automatically generate a set of rules ( Increase or decrease ) Serial numbers that vary continuously .
Sequences are generally applied to primary key columns of tables , Can be used by multiple users , You can also generate primary keys for multiple tables . imagine , If there is no sequence , You can only write programs to generate sequential values , In the case of using sequences , When inserting data into a table , The primary key column is directly assigned with the serial number , Ensure that the primary key column has no duplicate values , You can also get more reliable primary key values .
Parent theme : Manage sequence
15.2.2. Create sequence
Use CREATE SEQUENCE Statement to create a sequence .
To create a sequence in a pattern , You must have CREATE SEQUENCE System permissions . To create a sequence in another user's schema , Must possess CREATE ANY SEQUENCE Authority . for example , The following statement creates a sequence , Used to student Tabular stu_nmb The primary key column generates a four digit student number :
CREATE SEQUENCE stu_sequence INCREMENT BY 1 START WITH 1000 MINVALUE 1000 MAXVALUE 9999 CACHE 100 NOCYCLE;
As can be seen from the above example , You can control the sequence through various option parameters , Indicates whether the sequence is ascending or descending 、 Starting value of serial number (START WITH)、 minimum value (MINVALUE) And maximum (MAXVALUE) And the increasing range between sequence values (INCREMENT BY) wait .
among NOCYCLE Option means that the sequence will not generate more values after reaching the maximum or minimum value . If you want the serial number to be recycled , Please use CYCLE Options .
CACHE Clause preallocation 100 Serial numbers are stored in memory . Every time the user uses the sequence , You need to query the sequence once , When stored in memory, it can greatly speed up the acquisition of serial numbers , When the last serial number in the cache is used , The database will read another set of serial numbers into the cache .
If you choose to cache a set of serial numbers , Then the database may skip this group of serial numbers . for example , When the instance closes abnormally ( for example , When an instance fails or issues SHUTDOWN ABORT When the sentence is ), Cached but unused serial numbers will be lost . Besides , Used but unsaved serial numbers will also be lost .
Parent theme : Manage sequence
15.2.3. Modify the sequence
Use ALTER SEQUENCE Statement modification sequence .
To change the sequence , Your schema must contain this sequence , You must have ALTER Object permissions , Or it must have ALTER ANY SEQUENCE System permissions . You can modify the sequence by modifying the definition parameters of the generated value of the sequence .
The following example changes stu_sequence The growth rate of the sequence is 2, The serial number of recycling and the number of caches are changed to 20:
ALTER SEQUENCE stu_sequence INCREMENT BY 2 CYCLE CACHE 20;
meanwhile , You should pay attention to the rationality of the value when modifying the sequence , For example, the following statement is obviously unreasonable , Because the starting value (START WITH) yes 1000, The following modification attempts to change the minimum value to 2000:
ALTER SEQUENCE stu_sequence INCREMENT BY 2 MINVALUE 2000 MAXVALUE 8000 CYCLE CACHE 20;
Parent theme : Manage sequence
15.2.4. Usage sequence
For users , The available resources in the sequence are serial numbers , Serial numbers can be accessed by multiple users and incremented or decremented .
To use sequences , Your schema must contain this sequence , Or you must have been granted another user sequence SELECT Object permissions . Once the sequence is created , The sequence can be used by multiple users ( Have SELECT Object permissions ) Access and increment , Without waiting .
Once the sequence is generated , The user can be in SQL Used in statement CURRVAL and NEXTVAL Pseudo column to access the value of the sequence .
To use the current sequence value , Please be there. SQL Reference... In a statement seq_name.CURRVAL.
Parent theme : Manage sequence
15.2.4.1. Use NEXTVAL Generate serial number
Once the sequence is created , Users can use SQL Statement from CURRVAL and NEXTVAL Get the value of the sequence in the pseudo column .
CURRVAL and NEXTVAL Pseudo columns are like pointers to sequences ,NEXTVAL Every time you get a value , The pointer moves back one position , This can be used CURRVAL Get the current serial number . namely , Its significance is as follows :
CURRVAL Returns the current sequence value ;
NEXTVAL Returns the next available serial number .
As described above ,NEXTVAL and CURRVAL Not reserved words or keywords , Can be in SELECT、INSERT or UPDATE etc. SQL Used as a pseudo column name in the statement . When using the serial number for the first time , Must visit first NEXTVAL Pseudo column . To generate and use serial numbers , Please quote SQL Statement NEXTVAL, The following example , From pseudo table dual Check the serial number in :
SELECT stu_sequence.NEXTVAL FROM dual;
The execution result of this statement is :
NEXTVAL ---------- 2000
for example , Suppose there are new student registrations , The serial number can be directly referenced in the value list :
INSERT INTO student VALUES (stu_sequence.NEXTVAL, ' Zhang Li ',18,' Senior high school grade two ',2,'[email protected]',SYSDATE);
When you need to update the serial number of a classmate , Can be in UPDATE Of the statement SET Reference serial number in Clause . for example :
UPDATE student SET student_id = stu_sequence.NEXTVAL WHERE student_id = 2022;
Parent theme : Usage sequence
15.2.4.2. Use CURRVAL Serial number
To use the current sequence value , Please be there. SQL Reference... In a statement seq_name.CURRVAL.
Use CURRVAL Before ,seq_name.NEXTVAL Must have been referenced in the user session ( In current or previous transactions ) At least once . You can quote as many times as you need CURRVAL, Include multiple references in the same statement . In reference to NEXTVAL Before , The next serial number will not be generated . Continue with the previous example , You will insert new student data , To register :
INSERT INTO student VALUES (stu_sequence.CURRVAL, ' Liu Yi ',17,' First year of high school ',4,'[email protected]',SYSDATE); INSERT INTO student VALUES (stu_sequence.CURRVAL, ' Zhao Wen ',19,' First year of high school ',5,'[email protected]',SYSDATE);
Suppose that given in the previous section INSERT Statement generates a new sequence number 2022, Then the two lines inserted in the statement in this section are inserted with sequence number 2022 The line of . Of course , If quoted CURRVAL The column of defines the primary key constraint , The reference of this method will restrict the error .
Parent theme : Usage sequence
15.2.5. Cache serial number
Caching serial numbers can improve access efficiency .
Accessing serial numbers in the sequence cache is faster than reading them from disk .
When an application accesses a sequence in the sequence cache , The serial number will be read quickly .
When the sequence is read into the cache , The specified number of serial numbers has been generated and stored in the cache .
Parent theme : Manage sequence
15.2.5.1. About cache serial numbers
Accessing serial numbers in the sequence cache is faster than reading them from disk . The sequence cache consists of entries , Each entry can hold multiple serial numbers of a single sequence .
Follow these guidelines to quickly access all serial numbers :
Please make sure that enough serial numbers are cached in the sequence , Ensure that applications use all sequences concurrently .
In case of high concurrency, the number of serial numbers in the cache should be increased .
Parent theme : Cache serial number
15.2.5.2. Number of entries in the sequence cache
When an application accesses a sequence in the sequence cache , The serial number in the cache will be read quickly .
If the sequence accessed by the application is not in the cache , Then before using the serial number , The sequence must first be read from disk into the cache . If an application uses many sequences concurrently , Then the sequence cache may not be large enough , Cannot accommodate all sequences . under these circumstances , Accessing the serial number may often require reading the disk . For quick access to all sequences , Please make sure there are enough serial numbers in the cache , This ensures that applications use all sequences concurrently .
Parent theme : Cache serial number
15.2.5.3. Number of serial numbers in the cache
When the sequence is read into the cache , The specified number of serial numbers has been generated and stored in the cache .
Then you can quickly access these values . The number of serial numbers stored in the cache is determined by CREATE SEQUENCE Statement CACHE Parameter determination , The default value for this parameter is 1. CREATE SEQUENCE Sentence creation seq_test2 Sequence , Of this sequence 50 Values are stored in SEQUENCE In cache :
CREATE SEQUENCE seq_test2 CACHE 50;
Parent theme : Cache serial number
15.2.6. Delete sequence
If a sequence is no longer needed , have access to DROP SEQUENCE Statement to delete the sequence .
Users can delete any series they create . To delete the sequence of other users , Must possess DROP ANY SEQUENCE System permissions . After the sequence is deleted , Relevant information is no longer retained in the data dictionary .
for example , The following statement deletes the stu_sequence Sequence :
DROP SEQUENCE stu_sequence;
Parent theme : Manage sequence
边栏推荐
- 使用Express写接口
- Umb10f-asemi patch rectifier bridge umb10f
- Selenium webdriver -- junit4 annotation
- 深拷贝函数
- Qt Opengl 给两个立方体设置两中不同颜色
- SPARK Parquet嵌套类型的向量化支持以及列索引(column index)
- conda升级tensorflow-gpu=2.5.0及cudn,cudatoolkit版本
- [JS] console command you don't know
- Indiscriminate use of "termination", the signal pours on the street
- Win64 驱动内核编程-31.枚举与删除映像回调
猜你喜欢
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
Recruit | "one brain cloud research circle" recruit new members
自定义类型:结构体(一)
[错题]对角线
算术运算符2(阁瑞钛伦特软件-九耶实训)
Share some advanced debugging and usage skills of vscode scenarios
代码实现层序遍历二叉树(C语言)
Recommendation Book Evolutionary Psychology: the mystery of magic behind Liu Genghong and Wang Xinling
Assessment | are you a social terrorist?
代码实现求二叉树结点数和叶子结点数(C语言)
随机推荐
Win64 驱动内核编程-31.枚举与删除映像回调
微带线拉开7H你就觉得很稳了?
Win64 驱动内核编程-32.枚举与删除注册表回调
How can the reading club not be an emotional hostage?
卡通3d 虚拟形象制作产业应用
苹果公司继续向俄罗斯提供零部件以保证保修期内的产品替换
在node.js项目中安装配置mysql模块并进行增删改查
[200 opencv routines] 235 Principal component analysis for feature extraction (sklearn)
[kaggle] how to effectively avoid oom (out of memory) and long alchemy process
What happens when the self incrementing ID of the database is used up?
测试前不校准,真实结果知多少?
CONDA upgrade tensorflow gpu=2.5.0 and cudn, cudatoolkit version
当删则删,这种电容本不该出现
Express与中间件
【OpenCV 例程200篇】235. 特征提取之主成分分析(sklearn)
7.19模拟赛总结
leetcode:42. 接雨水
金仓数据库KingbaseES数据库管理员指南--17数据库调度概念
Hutoo --- 日期时间工具-DateUtil
Umb10f-asemi patch rectifier bridge umb10f