Oracle sequence and index

Oracle sequence and index

A sequence

1 Sequence definition

A sequence is a database object that can be used by multiple users to generate unique values. It automatically provides unique values ​​and shared objects. It is mainly used to provide primary key values. Loading sequence values ​​into memory can improve access efficiency.

2 Create and use sequence

CREATE SEQUENCE 序列名
[INCREMENT BY n]--每次增长的数值
[START WHTH n]--从哪个值开始
[{
    
    MAXVALUE n} | NOMAXVALUE]
[{
    
    MINVALUE n} | NOMINVALUE]
[{
    
    CYCLE | NOCYCLE}]--是否需要循环
[{
    
    CACHE n | NOCACHE}]--是否缓存登录

Case:
Create the sequence dept_deptid_seq to increase the primary key for the Departments table, without using CYCLE
Insert picture description here

CREATET SEQUENCE seq;
SELECT seq.nextval FROM dual;
INSERT INTO emp VALUES(seq.nextval,'c');

3 Query sequence

(1) View the sequence
Query the data dictionary view USER_SEQUENCES to obtain the sequence definition information, provided that the sequence value is not put into the memory (NOCACHE), if the NOCACHE option is specified, the last_number will be the next valid value in the sequence

SELECT sequence_name,min_value,max_value,increment_by,last_number
FROM user_sequences;

(2) nextval and currval
① nextval returns the next valid value of the sequence, any user can refer to the
current value of the sequence in currval
nextval should be specified before currval , otherwise an error that currval has not been defined in this session will be reported

4 Modify the sequence

Points to note when modifying the sequence
① Must be the owner of the sequence or have alter permission for the sequence
② Only the future sequence value will be changed
③ Changing the initial value of the sequence can only be achieved by deleting the sequence and recreating the sequence

ALTER SEQUENCE 序列名
[INCREMENT BY n]--每次增长的数值
[START WHTH n]--从哪个值开始
[{
    
    MAXVALUE n} | NOMAXVALUE]
[{
    
    MINVALUE n} | NOMINVALUE]
[{
    
    CYCLE | NOCYCLE}]--是否需要循环
[{
    
    CACHE n | NOCACHE}]--是否缓存登录

Insert picture description here

5 Delete sequence

After deleting the sequence, the sequence is no longer referenced

DROP SEQUENCS 序列名;

Secondary index

1 Index definition

(1) An index is a schema object independent of the table and can be stored in a disk or table space different from the table.
(2) If the index is deleted or damaged, it will not affect the table, but will only affect the speed of the query.
(3) Once the index is established, Oracle will automatically maintain it, and Oracle will definitely use the index when
(4) Pass the pointer Speed ​​up the query speed of the Oracle server
(5) Reduce disk I/O by quickly locating data

2 Create an index

(1) Automatic creation: After defining the primary key or unique constraint, the system will automatically create a unique index on the corresponding column
(2) Manual creation: the user can create a non-unique index on its column to speed up the query
(3) grammar

CREATE INDEX 索引名 ON 表名(字段);

(4) When to create an index
① The data value in the column has a wide distribution range
② The column often appears in the where clause or the connection condition
③ The table is frequently accessed and the amount of data is large, and the accessed data accounts for about 2% of the total data- 4%
(5) Under what circumstances do not create an index
① The table is very small
② Columns do not often appear in the where clause or join conditions
③ The total amount of data queried is not 2%-4%
④ The table is frequently updated

3 Query index

You can use the data dictionary views user_indexs and user_ind_columns to view index information
Insert picture description here

4 Delete index

Only the owner of the index or a user with drop any index permission can delete the index, and the delete operation cannot be rolled back

DROP INDEX 索引名;

Three synonyms-synonym

Use synonyms to access the same object: facilitate access to other users’ objects and shorten the length of object names

1 Create synonyms

CREATE [PUBLIC] SYNONYM 名称 FOR 表名

Case:

CREATE  SYNONYM d FOR deparment;
SELECT * FROM d;

2 Delete synonyms

DROP SYNONYM 名称;

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/114904857