bitsCN.com
Oracle DB序列
序列是一个用于创建整数值的数据库对象。可以创建序列,然后再用其生成编号。
序列具有如下特点:
• 可以自动生成唯一编号
• 是一个可共享的对象
• 可用于创建主键值
• 替换应用程序代码
• 如果将序列高速缓存到内存中,则访问序列值的效率会有所提高
序列是用户创建的数据库对象,可由多个用户共享来生成整数。
可以通过定义一个序列来生成唯一值,或者回收编号后重新使用相同的编号。
序列的常见用途是创建主键值,每行的主键值必须是唯一的。序列由内部Oracle 例行程序按递增(或递减)方式生成。由于可以减少编写生成序列的例行程序所需的应用程序代码量,因此使用该对象可以节省一些时间。
序列号的存储和生成与表无关。因此,同一序列可以用于多个表。
CREATE SEQUENCE 语句:语法
定义一个可以自动生成序号的序列:
CREATE SEQUENCE sequence[INCREMENT BY n][START WITH n][{MAXVALUE n | NOMAXVALUE}][{MINVALUE n | NOMINVALUE}][{CYCLE | NOCYCLE}][{CACHE n | NOCACHE}];
通过使用CREATE SEQUENCE语句可以自动生成序号。
在该语法中:
sequence:是序列生成器的名称
INCREMENT BY n: 指定序列号之间的间隔,其中n 是一个整数(如果省略此子句,则序列按1 递增)
START WITH n: 指定要生成的第一个序列号(如果省略此子句,则序列从1 开始)
MAXVALUE n: 指定序列可以生成的最大值
NOMAXVALUE 指定10^27 作为递增序列的最大值,而-1 作为递减序列的最大值(这是默认选项)
MINVALUE n :指定最小的序列值
NOMINVALUE :指定1 作为递增序列的最小值,而-(10^26) 作为递减序列的最小值(这是默认选项)
创建序列
• 创建一个名为DEPT_DEPTID_SEQ的序列,将其用作DEPARTMENTS 表的主键。
• 不使用CYCLE选项。
sys@TEST0924> CREATE SEQUENCE dept_deptid_seq 2 INCREMENT BY 10 3 START WITH 120 4 MAXVALUE 9999 5 NOCACHE 6 NOCYCLE 7 ;Sequence created.
CYCLE | NOCYCLE :指定在达到最大值或最小值之后,序列是否继续生成值(NOCYCLE是默认选项)
CACHE n | NOCACHE :指定Oracle Server 预先分配并保留在内存中的值的数量(默认情况下,Oracle Server 会高速缓存20 个值)
示例中创建了一个名为DEPT_DEPTID_SEQ的序列,用作DEPARTMENTS 表的DEPARTMENT_ID 列。该序列从120 开始,不允许高速缓存,也不进行循环。
请勿在序列用于生成主键值时使用CYCLE选项,除非有一个可靠的机制与序列循环相比可以更快地清除旧行。
注:序列与表没有关系。通常,应按序列的预期用途来命名序列。但是,序列可以在任何地方使用,而与其名称无关。
NEXTVAL和CURRVAL伪列
• NEXTVAL会返回下一个可用的序列值。每次被引用时NEXTVAL都会返回一个唯一值,即使对于不同用户也是如此。
• CURRVAL会获得当前序列值。
• 只有对序列发出NEXTVAL之后,CURRVAL才能包含值。
在创建一个序列之后,该序列会生成可以在表中使用的序号。通过使用NEXTVAL和CURRVAL伪列可以引用序列值。
NEXTVAL伪列用于从指定的序列中提取连续的序列号。必须用序列名来限定NEXTVAL。
在引用sequence .NEXTVAL 时,就会生成新的序列号,还会将当前的序列号放在CURRVAL中。
CURRVAL伪列用于引用当前用户刚刚生成的序列号。但是,必须先用NEXTVAL在当前用户的会话中生成一个序列号,然后才能引用CURRVAL。必须用序列名来限定CURRVAL。
在引用sequence .CURRVAL 时,会显示返回给用户进程的最后一个值。
使用NEXTVAL和CURRVAL的规则
可以在下列上下文中使用NEXTVAL和CURRVAL:
• 不是子查询一部分的SELECT 语句的SELECT 列表
• INSERT 语句中子查询的SELECT 列表
• INSERT 语句的VALUES 子句
• UPDATE 语句的SET 子句
不能在下列上下文中使用NEXTVAL和CURRVAL:
• 视图的SELECT 列表
• 带有DISTINCT 关键字的SELECT 语句
• 带有GROUP BY 、HAVING 或ORDER BY 子句的SELECT 语句
• SELECT 、DELETE 或UPDATE 语句中的子查询
• CREATE TABLE或ALTER TABLE 语句中的DEFAULT表达式
使用序列
• 在位置ID 2500 中插入一个名为“Support”的新部门:
sys@TEST0924> INSERT INTO hr.departments(department_id, department_name,location_id) 2 VALUES (dept_deptid_seq.NEXTVAL, 'Support', 2500);1 row created.
它使用DEPT_DEPTID_SEQ序列生成280的新部门编号。
• 使用sequence_name.CURRVAL查看DEPT_DEPTID_SEQ序列的当前值:
sys@TEST0924> SELECT dept_deptid_seq.CURRVAL FROM dual; CURRVAL---------- 280
假定现在要聘用某些雇员作为新部门的工作人员。在要对所有新雇员执行的INSERT语句中可以包含以下代码:
INSERT INTO employees (employee_id, department_id, ...)VALUES (employees_seq.NEXTVAL, dept_deptid_seq .CURRVAL, ...);
注:上面的示例假设已经创建一个名为 EMPLOYEE_SEQ的序列来生成新雇员编号。
高速缓存序列值
• 将序列值高速缓存在内存中,这样可以更快地对这些值进行访问。
• 在发生以下情况时,序列值会出现间断:
– 发生回退
– 系统崩溃
– 序列已用于其它表中
可以将序列高速缓存在内存中,以便于更快地对这些值进行访问。当首次引用序列时,序列值会被填充到高速缓存中。因此会从高速缓存序列中检索每次请求的下一个新序列值。用完最后一个序列值之后,就会在下一次请求序列时将序列的另一个高速缓存拖入到内存中。
序列中的间断
虽然序列生成器会发出没有间断的序号,但是此操作的发生与提交或回退有关。因此,如果你回退一条包含序列的语句,则会丢失相应的序号。
另一个可能导致序列出现间断的事件是系统崩溃。如果序列值已高速缓存在内存中,那么在系统崩溃时就会丢失一些值。
因为序列不直接与表相关联,所以同一序列可用于多个表。但是,如果同一序列用于多个表,则每个表的序号可能会有间断。
修改序列
更改增量值、最大值、最小值、循环选项或高速缓存选项:
sys@TEST0924> ALTER SEQUENCE dept_deptid_seq 2 INCREMENT BY 20 3 MAXVALUE 999999 4 NOCACHE 5 NOCYCLE;Sequence altered.
如果序列达到MAXVALUE 限制,则序列不会再分配额外的值,此时你会收到一条错误消息,指明序列超出了MAXVALUE 。要继续使用该序列,可以使用ALTER SEQUENCE语句修改该序列。
语法
ALTER SEQUENCE sequence[INCREMENT BY n ][{MAXVALUE n | NOMAXVALUE}][{MINVALUE n | NOMINVALUE}][{CYCLE | NOCYCLE}][{CACHE n | NOCACHE}];
在此语法中,sequence 是序列生成器的名称。
修改序列的准则
• 你必须是序列的所有者或拥有序列的ALTER权限。
• 修改只会影响以后生成的序列号。
• 如果要从另一编号处重新开始,则必须删除原有序列后重新创建。
• 在修改过程中会执行一些验证操作。
• 要删除序列,请使用DROP 语句:
sys@TEST0924> DROP SEQUENCE dept_deptid_seq;Sequence dropped.
• 要修改一个序列,你必须是此序列的所有者或拥有序列的ALTER权限。要删除一个序列,你必须是此序列的所有者或拥有此序列的DROP ANY SEQUENCE权限。
• ALTER SEQUENCE 语句只会影响以后生成的序列号。
• 使用ALTER SEQUENCE 语句不能更改START WITH选项。如果要从另一编号处重新开始,则必须删除原有序列后重新创建。
• 在修改过程中会执行一些验证操作。例如,不能强制实施一个小于当前序列号的新MAXVALUE 。
ALTER SEQUENCE dept_deptid_seqINCREMENT BY 20MAXVALUE 90NOCACHENOCYCLE;• 错误:sys@TEST0924> ALTER SEQUENCE dept_deptid_seq 2 INCREMENT BY 20 3 MAXVALUE 99 4 NOCACHE 5 NOCYCLE;ALTER SEQUENCE dept_deptid_seq*ERROR at line 1:ORA-04009: MAXVALUE cannot be made to be less than the current value
bitsCN.com

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software