29. Database Advanced section

Advanced Database section

1. The view (view)

There is a table or view is a virtual table query results tables composed of benefits used are:

  • When using a multi-table query, sql statement can be very complex, each time to write again be quite troublesome, so you can view saved us by writing sql reuse.
  • Another effect is that we can use different views to open access to different data, some want to show demonstrated limited

Note: Because it is a virtual table, the data view is actually derived from other other tables, so the data in the view does not appear on the hard disk

Create a view

CREATE [OR REPLACE] VIEW view_name [(column_list)]
AS select_statement

2. Trigger (trigger)

Mysql program was a trigger associated with a table, this table when a certain event occurs at some point in time will automatically trigger the implementation of appropriate procedures

When using point in time represents the incident before before | after the incident after use

Events include update, delete, insert

Trigger contains two objects

  • old: update, delete available
  • new: update, insert available

Can be used: when the data table is modified, automatically recording some data, perform some sql statement

3. Transaction (transaction)

The transaction is a combination of a series of sql statements, as a whole.

Transaction features:

  1. Atomic, refers to this transaction sql statement as a whole, it can not be split, either perform or all fail
  2. Consistency, after the end of transaction execution, the associated table must be correct, it does not send data confusion
  3. Isolation, isolation between transactions, data not affect each other, even if the operation of the same table, essentially lock The lock granularity is divided into several different isolation levels
  4. Persistent, after the successful implementation of transaction data will be stored permanently, can not be restored

Using transactions:

  1. start transaction: open things, sql statement after this will be in the same transaction, and does not modify the database immediately
  2. commit: commit the transaction, so that the things in the sql data to perform operations immediately,
  3. rollback: rolls back the transaction, is to cancel this thing, this thing would not have any data in the database of effects, such as when a transaction occurs during the execution of this abnormal situation can be used to roll back the transaction rollback
  4. savepoint: savepoint, roll back can be specified to a certain fixed position in the ROLLBACK, which is part of the rollback

4. stored procedure (Procedure)

A stored procedure is a set of arbitrary sql statement set in mysql, call the stored procedure will execute all sql statement comprising; a similar function in python

Set variables, call when called with set to call a stored procedure

5. Functions

sql language, in addition to built-in functions can customize function

Create a function by create, including the name of the function, the function body, the return type of the value and return value

6. Backup and Recovery

Use mysqldump.exe comes to backup, you can back up a specific list, library, or all of the library to a file

Content mysqldump -u username -p password to backup (database table 1 table 2) ....> file path ....

Data recovery:

  • Not logged mysql: mysql <file path
  • Already logged in MySQL: source file path

7. Process Control

Including some of the sql syntax:

if then / case (selection execution) / declare (defined variable) / while, loop, repeat (loop)

Match query syntax of regular usage condition is determined by adding a regular regexp matching filter, be careful not to add a similar \ w such symbols

Guess you like

Origin www.cnblogs.com/yellowcloud/p/11202198.html