"In-depth understanding of Mysql" reading combing three

Click to view the collection

index

The index is used to quickly find out the row that has a certain value in a certain column. If the index is not applicable, mysql must start from the first data and then read the entire table until it finds the relevant row. The larger the table, the longer it takes.
But the index is not the more the better, try to use the conditional column as the index. This can maximize the execution efficiency of sql. And try to use a unique column as an index. For example, if gender is used as the index, the result is only male/female, which is not very helpful to the query, but will increase the consumption of disk space. When using a string as an index, you must specify a length, otherwise if the string is too long, a lot of disk space will be wasted.
InnoDB and MyISAM default ships are BTREE indexes. You can create an index on a single column or multiple columns. And after Mysql 5.7, virtual column index can be used to realize the function of functional index. And also supports prefix index. Spatial index (implemented with RTREE). Full-text index
The syntax for creating a virtual column is:
alter table user add user_name varchar(20) generated always as (related functions such as DATE (time))
Mysql8 adds an invisible index, which means that the index is hidden. The purpose of this is to test the impact on actual applications after removing the index. The statement
create index index name on indicates that (index column) Invisible
can be changed back to visible through the following statement
alter table table name index index name visible

view

The view is a virtual table. The view does not exist in the database, but is dynamically generated each time it is used. The view is easier to use, because the user does not need to care about the table structure behind. It is also safer, as users who use views can only query partial result sets (allowed). Moreover, the view also has the advantage of data independence. Even if the column name in the table changes, the increase of the column will not affect the user's call. Just change the view.
Whether the view can be updated is related to the defined SQL statement. The view cannot be updated in the following situations:
1. Contains aggregate functions (sum count max min), distinct, group by, having, union, union all
2. Constant attempts
3. Select contains sub-queries
4. Join 5. From
a non-updatable The view or the table without update permission
6. The subquery in where refers to the table behind from
with [CASCADED | LOCAL] check option statement determines whether the record is not allowed to meet the view condition when the view is updated.
If you do not write the content in [], the default is CASCADE (ie with check option = with CASCADED |check option)
CASCADED: Must meet the conditions of this view and other conditions that refer to this view
LOCAL: Only need to meet the conditions of this view to
create View statement
Create view View name as query statement with [CASCADED | LOCAL] check option To
change the view, you can use alter or create or replace to
delete the view and use drop

Stored procedures and functions

The difference between a stored procedure and a function is that the parameter of the stored procedure can be in out inout and the parameter of the function can only be in (no need to write), 2 defaults to in out. And the function has a return value but the stored procedure does not.
Stored procedures and functions cannot be changed using create or replace, but can only be modified through alter function/procedure.
Stored procedures can be called through call

Variable creation and assignment

You can create variables by declare name type
and assign values ​​by set name =?

Define conditions and processing

Define condition:
declare condition name condition for [sql-error-code | sqlState | SQLWARNING | NOT FOUND | SQLEXCEPTION]
SQLWARNING: SQLSTATE with 01 beginning with
NOT FOUND: SQLSTATE with beginning with 02
SQLEXCEPTION: 01 SQLSTATE with beginning of 02
definition processing:
declare [Continue | EXIT] Handler for [sql-error-code | sqlState | SQLWARNING | NOT FOUND | SQLEXCEPTION | Condition name]
Continue: Skip the statement
when an error is encountered and continue execution EXIT: Exit when an error is encountered
Insert picture description here

Use of the cursor

You can use the cursor to loop through the result set.
Insert picture description here
Insert picture description here

Process control

1. IF statement
2. CASE statement
3. LOOP statement
4. LEAVE statement
5. ITERATE statement
6. REPEAT statement
7. WHILE statement

Event scheduler

The event scheduler can perform certain operations on the database at a custom time interval.
For example,
create event event scheduler name on schedule every 10 minute do truncate table name
Clear the table every 10 minutes

trigger

Triggers are operations performed before or after insert, delete, and update operations. This feature that triggers it can assist applications in ensuring data integrity on the database side.
The statement
create trigger trigger name [After | Before] [Insert | Delete | Update] on the operation performed by the table for Each Row
Begin END After and Before respectively represent the execution of the trigger before and after the insert/delete/update operation. When the statement is executed, Insert Into... on duplicate key update ... statement. Will execute before insert trigger->before update trigger->after update trigger



Guess you like

Origin blog.csdn.net/qq_30033509/article/details/114414200