Where can I start learning about Oracle? Here, I will take you to the door of Oracle. One chapter a day, seven days of entry is not a dream.
1. Synonyms
1.1 Solution
The solution is a collection of database objects. To distinguish each set, you need to name the set. Its name is the case name.
The solution contains various objects, such as tables, views, indexes, synonyms, sequences, and database links.
Generally, a user corresponds to a solution. When a user is created, a solution with the same name as the user name is created, which serves as the default solution for the user.
If the user scheme of the table is not specified when accessing a table, the system automatically adds the default solution name before accessing the table.
The complete name of an object in the database is "solution name. Object Name", instead of "User Name. Object Name"
1.2 synonym Overview
Synonym is an alias of a solution object. It is often used to simplify objects and improve object access security.
Synonyms do not occupy the actual storage space. Only the definition of synonyms is saved in the database dictionary.
Objects that can create synonyms include tables, views, synonyms, sequences, stored procedures, and other objects.
Two types of synonyms can be created in Oracle: public synonyms and solution synonyms.
1.3 public synonyms and solution Synonyms
Public synonym)
PUBLIC synonyms are owned by a special user group, and all users in the database can use PUBLIC synonyms.
The data dictionary view created by SYS is an example of a public synonym.
Schema synonym)
Solution synonyms are used by the users who created them, also known as private synonym. Users can control whether other Users have the right to use their own solution synonyms.
Solution synonyms are often used in application development to provide naming solutions for application development.
When the code references an undefined table, view, synonym, sequence, function, and other objects, Oracle checks whether referenced objects exist in the three locations in the following order.
Objects owned by the current user
Synonym for a scheme owned by the current user
Public Synonym
If the name of the object is not found in none of the three places, an error message is prompted, such as ORA-00942: Table or view does not exist"
1.4 create Synonyms
Synonym for creating a solution
You must have the create synonym system permission to CREATE solution synonyms in your own solution.
You must have the create any synonym system permission to CREATE a scheme SYNONYM in other schemes.
Syntax
CREATE [or replace] SYNONYM synonym_name [schema.] object;
Where, or replace indicates that if a synonym already exists, the new synonym will be overwritten.
Create Synonym
Create a public Synonym
To CREATE a public synonym, you must have the create public synonym system permission.
Syntax
CREATE [or replace] public synonym synonym_name [schema.] object;
1.5 Use synonyms
Usage Synonym
Solution synonyms can be divided into solution users and other users.
Solution users use their own solution Synonyms
The solution user has all the permissions of the object, so the synonym of the object can be used like the original object.
Synonym
Use public synonyms
Unlike solution synonyms, if you use public synonyms to access objects in other schemes, you do not need to add a solution name before the public synonyms.
If you are not authorized to access the object referenced by this public synonym, you still cannot use this public Synonym
Synonyms of solutions created by other users using another user
Add the solution name as the prefix before the solution synonym to use the solution synonym created by other users.
Access objects in other schemes using their own scheme Synonyms
Create a scheme synonym pointing to objects in other schemes in your own scheme
1.6 Delete Synonyms
After a synonym is deleted, the basic object referenced by the synonym is not affected.
Delete solution Synonyms
You can delete ANY scheme synonyms in your own scheme. However, to delete scheme synonyms in other schemes, you must have the permission to drop any synonym.
Syntax
Drop synonym synonym_name
Delete A public Synonym
To delete a public synonym, you must have the permission to drop public synonym.
Syntax
Drop public synonym synonym_name
2. Sequence
2.1 Sequence Overview
A sequence is a named SEQUENCE number generator that can generate a series of sequential integers in a serial manner.
Similar to views and constraints, sequences do not occupy actual storage space, but only store sequence definitions in data dictionaries.
Reason for introducing sequence or main purpose of Sequence
Primary Key and foreign key value Application Requirements
Sequential Number Application Requirements
Sequence Generation and definition content
2.2 create Sequence
To CREATE a SEQUENCE in your own solution, you must have the create sequence system permission.
You must have the create any sequence system permission to CREATE sequences in other scenarios.
2.3 use sequence
NEXTVAL and CURRVAL Columns
The NEXTVAL column returns the next value generated by the sequence.
The CURRVAL column returns the current value generated by the sequence.
Sequence Initialization
Before you reference the CURRVAL column for the first time, you must reference the NEXTVAL column once to initialize the value of the sequence. Otherwise, an error occurs.
2.4 change sequence
You must have the alter sequence system permission to change the SEQUENCE in your solution.
You must have the alter any sequence system permission to change the SEQUENCE in other scenarios.
The start value in the sequence cannot be modified. to modify the start value, you must delete the sequence and recreate it.
Change sequence includes
Modify MAXVALUE and MINVALUE
Modify the INCREMENT Value
Modify the number of sequences in the cache
Syntax:
Alter sequence sequnce_name
[Increment by n2]
[{MAXVALUE n3 | NOMAXVALUE}]
[{MINVALUE n4 | NOMINVALUE}]
[{CACHE n5 | NOCACHE}]
[{CYCLE | NOCYCLE}]
When you need to delete the SEQUENCE in your own scheme, you must have the permission to DROP the SEQUENCE system.
When you need to delete sequences from other schemes, you must have the permission to drop any sequence system.
Syntax
Drop sequence sequence_name
2.5 Delete Sequence
When you need to delete the SEQUENCE in your own scheme, you must have the permission to DROP the SEQUENCE system.
When you need to delete sequences from other schemes, you must have the permission to drop any sequence system.
Syntax
Drop sequence sequence_name
3. View
3.1 view Overview
A view is a logical table defined by a SELECT subquery statement. It is a "virtual table" that only defines data but does not have data"
View is a way to view and operate table data.
Advantages of a view
Provides various data representations
Provides the security of some data
Hide data complexity
Simplified query statement
Execute special query
Save complex queries
3.2 Create a view
To CREATE a VIEW in the current solution, you must have the create view system permission.
You must have the create any view system permission when creating a VIEW in other scenarios.
The view owner must be explicitly authorized to access all basic objects referenced in the view definition.
Syntax:
CREATE [or replace] [FORCE] VIEW view_name
[(Alias1, alias2..)]
AS select_statement
[With check option [CONSTRAINT constraint]
[With read only];
Parameter description
Or replace if the view already exists, this option will re-create the view
FORCE creates a view regardless of whether the base table exists.
View_name specifies the name of the created view.
Alias specifies the expression or column alias selected by the View query
SELECT statement when select_statement creates a view
When using the view, CHECK whether the involved data can pass the WHERE condition of the SELECT subquery. Otherwise, the operation is not allowed and an error message is returned.
Views created with read only can ONLY be used for queries, but cannot be used to change data. This clause cannot coexist with the order by clause.
Create common view
Use the with read only option to create a READ-ONLY view
Use the with check option to create a constraint View
3.3 force create View
Normally, creating a view fails if the basic table does not exist.
However, if the statement used to create a view has no syntax errors, you only need to use the FORCE option to create the view. This forcibly created view is called a view with compilation errors.
In this case, the view is in an invalid state and cannot be executed. However, after the basic table is created, the view can run normally.
3.4 perform DML operations on the connection View
All DML operations performed on the view will be completed on the data of the basic table.
Updating views (including UPDATE, DELETE, and INSERT) is subject to certain restrictions, that is, not all views can perform all DML operations.
Some connection views can be updated, but some cannot be updated.
Updatable connection View
Does not contain centralized operators (UNION, union all, INTERSECT, MINUS, etc)
Does not contain the DISTINCT keyword
Does not include the group by, order by, connect by, or start with clause.
Does not contain subqueries
Does not contain grouping Functions
The column to be updated is not defined by the column expression.
All not null columns in the table belong to this view.
You also need to follow the update standard, that is, you can only update the "key-value save table"
Key Value saving table
If the primary key (primary key and unique key) of a base table in the Connected View still exists in its view, the base table is called a key-value storage table.
3.5 query view information
Use the USER_UPDATABLE_COLUMNS data dictionary to query updatable columns of a view.
Use the USER_VIEWS data dictionary to query the definition information of a view.
3.6 delete a view
You can delete various views in the current mode. To delete views in other schemes, you must have the permission to drop any view.
Syntax
Drop view view_name
4. Index
4.1 index Overview
By creating indexes on one or more columns in a table, you can quickly access the data, reduce hard disk I/O operations during query, and accelerate data retrieval.
Similar to other solution objects with independent storage structures, indexes occupy the actual storage space.
Once an index is created and the DML operation is performed on the table, Oracle automatically maintains the index.
If you think of a table as a book, the index function is similar to the directory in the book.
4.2 create an index
Even if an index is created in a table, Oracle does not mechanically use an index for all queries on the table, but determines whether to use an index based on the specific query conditions.
Before running a query statement, Oracle generally needs to optimize it. The purpose of optimization is to find the best way to run the query statement.
The optimizer compares the resource overhead required for full table scan with the resource overhead required for using indexes. If the resource overhead required for full table scan is more cost-effective, indexes are not used.
The resources required for full table scan are closely related to the data volume in the table. Generally, when the amount of data in a table reaches a certain value, the optimizer will consider using indexes.
4.3 Index classification
Single Column index and Composite Index
An index can be composed of one or more columns. The column used to create an index is called an "index column"
A single column index is an index created based on a single column. A composite index is an index created based on multiple columns.
Unique and non-unique Indexes
A unique index is an index with duplicate index values. A non-unique index is an index with duplicate index values.
Whether it is a unique index or a non-unique index, the index column can be NULL.
By default, the index created by Oracle is a non-unique index.
Standard (B-tree index, B-tree) index
When you use the create index statement to CREATE an INDEX, the default created INDEX is the B-tree INDEX.
B-tree indexes can be single-column or composite indexes, unique indexes, or non-unique indexes.
B-tree indexes organize and store index data by B-Tree Structure
Bitmap Index
A base number is the number of distinct values that a column may have. For example, the base of the Sex column is 2 (Gender can only be male or female), and the base of the MaritalStatus column is 3 (marital status can only be unmarried, married, or divorced)
For columns with a small base, the efficiency of B-tree Indexing is relatively low.
For columns with a small base and limited fixed values (such as gender, marital status, administrative region, and title), you should create Bitmap indexes for these columns to accelerate query efficiency.
When the ratio of the base number of a column to the total number of rows in the table is less than 1%, we recommend that you create a bitmap index on the column.
Creating a single bitmap index on a table is meaningless. The role of a bitmap index is to combine it with other Bitmap indexes.
Function Index
In Oracle, you can not only create indexes for the columns in a table, but also create indexes for functions or expressions that contain columns. Such indexes are called "function indexes"
Based on the base of the result of a function or expression, the function index can be either a common B-tree index or a bitmap index.
4.4 create various indexes
Unique Index
Create a unique index idx_emp_ename in the ename column of the emp table. After creation, duplicate values are not allowed in the ename column of the table.
Composite Index
If the WHERE clause in the SELECT statement references all or most columns in the composite index, the composite index can significantly improve the query speed.
When creating such indexes, pay attention to the sequence of columns used in the definition. Generally, the most frequently accessed columns should be placed at the beginning of the list.
Bitmap Index
Because the values of the job column and deptno column in the emp table are limited, and you often need to query, count, and summarize these columns, you should create Bitmap indexes based on these columns.
Function Index
To improve the execution efficiency of query statements that contain functions (including SQL built-in functions or user-defined functions) and expressions in the query conditions, you can create function indexes as appropriate.
When creating a function index, Oracle first evaluates the function values or expression values that contain the index column, sorts the results after the evaluation, and finally stores the results in the index.
4.5 merge and recreate Indexes
As the table is constantly updated, more and more storage fragments are generated in the table index, which will negatively affect the efficiency of the index.
You can clear fragments in two ways: Merge indexes and recreate indexes.
4.6 monitor index usage
Whether the created index can work effectively depends on whether the index is used by Oracle during SQL statement execution.
Oracle provides a simple way to monitor index usage, that is, to record index usage in the dynamic performance view of the v $ object_usage data dictionary.
Procedure
Make the index under monitoring. query v $ object_usage to check the monitoring status of the index.
Execute SQL statements to view the usage of monitored indexes
Disable index monitoring
4.7 View index information
Use the user_indexes data dictionary view to view all the index information in a table.
INDEX_NAME: indicates the index name.
INDEX_TYPE: indicates the index type. FUNCTION-based normal indicates FUNCTION-based B-tree indexes, BITMAP indicates BITMAP indexes, and NORMAL indicates normal B-tree indexes.
UNIQUENES: indicates whether the column is a unique index.
4.8 indexing principles
Generally, you do not need to create an index for a table with a small amount of data.
For tables with a large data volume, if the number of records frequently to be queried is less than 15% of the number of all records in the table, you can consider creating an index for the table.
Indexes should be created for columns with no duplicate column values.
B-tree indexes should be created for columns with a large value range (such as the ename column); for columns with a small value range (such as the sex column), bitmap indexes should be created.
An index should be created for columns that contain many NULL values but often need to query all records with non-NULL values.
You cannot create indexes on columns of LONG or BLOB or other big object data types.
If you only need to perform read-only operations on the table in most cases, you can create more indexes for the table to increase the query speed.
If you need to update a table in most cases, you should create fewer indexes to increase the update speed.
Summary
A solution is a collection of database objects. A user generally corresponds to a solution.
Synonym is an alias of a solution object. It is often used to simplify objects and improve object access security.
PUBLIC synonyms are owned by a special user group, and all users in the database can use PUBLIC synonyms.
A sequence is a named SEQUENCE number generator that can generate a series of sequential integers in a serial manner.
A view is a logical table defined by a SELECT subquery statement. a view is a "virtual table" only defined without data ". View is a way to view and operate table data.
Using a view has many advantages, such as providing various data representations, providing security for some data, hiding data complexity, simplifying query statements, executing special queries, and saving complex queries.
Creating indexes on one or more columns in a table can provide a fast access path for data retrieval, reduce hard disk I/O operations during query, and accelerate data retrieval.
For multiple tables that frequently require join queries, creating indexes on the columns used for join queries can significantly increase the query speed.
If you only need to perform read-only operations on the table in most cases, you can create more indexes for the table to increase the query speed.
From happy pig's column