Table of contents
View: A logically related collection of data extracted from a table.
Sequence: Provide regular numerical values
Index: Improve query efficiency
Query sql related to database objects
It is not easy to organize, please read carefully, I hope it will be helpful to you
View: A logically related collection of data extracted from a table.
• A view is a virtual table.
• Views are built on existing tables, and these tables on which views are built are called base tables.
• The statement that provides data content to the view is a SELECT statement, and the view can be understood as a stored SELECT statement.
• Views provide users with another representation of base table data
The role of views
• Control data access • Simplify queries
• Avoid repeated access to the same data
Related syntax
create or replace view empview
as
select employee_id emp_id,last_name name,department_name
from employees e,departments d
Where e.department_id = d.department_id
• You can use the WITH READ ONLY option to block DML (add, delete, modify) operations on the view
• Any DML operation will return an Oracle server error
Delete view
drop view view name;
Sequence: Provide regular numerical values
A database object that can be used by multiple users to generate unique values
• Automatically provide unique values • Shared objects
• Mainly used to provide primary key values • Loading sequence values into memory can improve access efficiency
CREATE SEQUENCE sequence
[INCREMENT BY n] --The value of each increase
[START WITH n] --Which value to start from
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n| NAMEVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}]; -- Whether to cache login
Create sequence
create sequence dept_id_seq
start with 200
increment by 10
maxvalue 10000;
Precautions
• Loading sequence values into memory improves access efficiency
• Sequences appear cracked when:
– Rollback – System exception – Multiple tables using the same sequence at the same time
• If the sequence values are not loaded into memory (NOCACHE), you can use the table USER_SEQUENCES to view the current valid values of the sequence.
delete sequence
• Use the DROP SEQUENCE statement to delete a sequence
• After deletion, the sequence cannot be referenced again
DROP SEQUENCE dept_deptid_seq;
Sequence dropped.
Index: Improve query efficiency
• A table-independent schema object that can be stored on a different disk or tablespace than the table
• If the index is deleted or damaged, it will not affect the table. It only affects the query speed.
• Once the index is created, Oracle Management System automatically maintains it, and Oracle Management System determines when to use the index. Users do not need to specify which index to use in the query statement
• When a table is deleted, all indexes based on the table will be automatically deleted
• Accelerate query speed of Oracle server through pointers
• Reduce disk I/O by quickly locating data
Create index:
• Automatic creation: After defining a PRIMARY KEY or UNIQUE constraint, the system automatically creates a unique index on the corresponding column.
• Manual creation: Users can create non-unique indexes on other columns to speed up queries
createindex()
CREATE INDEX emp_last_name_idx
ON employees(last_name);
Index created.
Situations suitable for index building:
• 1. Frequently query fields as where conditional statements
• 2. Related fields need to be indexed
• 3. Sorting fields can be indexed
• 4. The grouping field can be indexed (because the premise of grouping is sorting)
• 5. Statistical fields can be indexed (such as .count(), max())
• The data values in the column are spread over a wide range
• Columns often appear in WHERE clauses or join conditions
• The table is frequently accessed and has a large amount of data. The accessed data accounts for about 2% to 4% of the total data.
Situations not suitable for indexing:
• The table is small
• Column does not often appear as join condition or in WHERE clause
• The queried data is larger than 2% to 4%
• Table updated frequently
• 1. Frequently updated fields are not suitable for indexing
• 2. Fields not used in the where condition are not suitable for indexing
• 3. It can be determined that the table data is relatively small and does not require indexing
• 4. Fields with duplicate data and relatively even distribution are not suitable for indexing (such as gender, true and false values)
• 5. Columns participating in column calculations are not suitable for index building
Delete index
• Use the DROP INDEX command to delete an index
• Delete index UPPER_LAST_NAME_IDX
• Only the owner of the index or a user with DROP ANY INDEX permission can delete an index.
• Deletion operations are not rollable
Query sql related to database objects
–2: Table
-- View all tables under the current user
select * from user_tables;
-- View all tables under the specified user
select a.TABLE_NAME from all_tables a where a.OWNER = upper('IBP_AUTH');
-- View tables whose names contain log characters
select object_name,object_id from user_objects where instr(object_name,'LOG')>0;
-- Check the creation time of a table
select object_name,created from user_objects where object_name=upper('&table_name');
select object_name,created from user_objects where object_name=upper('表名');
-- Check the size of a table
select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&table_name');
-- View the tables placed in the memory area of ORACLE
select table_name,cache from user_tables where instr(cache,'Y')>0;
–3: index
Check the number and category of indexes
select index_name,index_type,table_name from user_indexes order by table_name;
-- View the indexed fields
select * from user_ind_columns where index_name=upper('&index_name');
-- Check the size of the index
select sum(bytes)/(1024*1024) as "size(M)" from user_segments
where segment_name=upper('&index_name');
–4: serial number
View the serial number, last_number is the current value
select * from user_sequences;
–5: View
View the name of the view
select view_name from user_views;
-- View the select statement that created the view
select view_name,text_length from user_views;
set long 2000; --Explanation: The size of set long can be set according to the text_length value of the view
select text from user_views where view_name=upper('&view_name');
select text from user_views where view_name=upper('V_CUST_GRADE_ALL');
–6: synonyms
View synonym names
select * from user_synonyms;
--Create synonyms
create synonym table_name for user2.table_name;
-- Solving the problem of insufficient permissions (switch to user2)
GRANT CREATE SYNONYM TO user1;
–7 Constraints
View the constraints of a table
select constraint_name, constraint_type,search_condition, r_constraint_name
from user_constraints where table_name = upper('&table_name');
–8 Stored functions and procedures
View the status of functions and procedures
select object_name,status from user_objects where object_type='FUNCTION';
select object_name,status from user_objects where object_type='PROCEDURE';
-- View the source code of functions and procedures
select text from all_source where owner=user and name=upper('&plsql_name');
–9: View the table creation statement
SELECT DBMS_METADATA.GET_DDL('TABLE','TB_TRANSA_CASH_TRADING_INFO')FROM DUAL; (Table name TABLE_NAME must be capitalized)