MySQL transaction processing
- Transaction is to put a group of SQL statements in the same batch to execute
- If an SQL statement fails, all SQL in the batch will be cancelled
Transaction characteristics
- Atomicity
- Consistency
- Isolation
- Durability
MySQL transaction realization method
SET AUTOCOMMIT
- Use the SET statement to change the automatic submission mode
SET AUTOCOMMIT = 0; # 关闭自动提交模式 SET AUTOCOMMIT = 1; # 开启自动提交模式
- The default is automatic submission in MySQL
- Turn off auto-commit first when using transactions
note
开启事务
SHOW VARIABLES LIKE 'event_scheduler';
设置数据库支持事务
SET GLOBAL event_scheduler=ON;
MySQL transaction implementation method
START TRANSACTION
Start a transaction, mark the starting point of the transaction
COMMIT
Submit a transaction to the database
ROLLBACK
Roll back the transaction and return the data to the initial state of the transaction
SET AUTOCOMMIT = 1;
Enable automatic submission of MySQL database
MySQL transaction processing steps
Database index
effect
- Improve query speed to ensure the uniqueness of data
- Can accelerate the connection between the table and the table to achieve the referential integrity between the table and the table
- When using grouping and sorting clauses for data retrieval, the time for grouping and sorting can be significantly reduced
- Full-text search field for search optimization
classification
- Primary key index (PRIMARY KEY)
- Unique index (UNIQUE)
- Regular Index (INDEX)
- Full text index (FULLTEXT
Primary key index
A certain attribute or combination of attributes can uniquely identify a record
Such as: student table (student number, name, class, gender, etc.), the student number is uniquely identified and can be used as the primary key
Features
- The most common index types
- Ensure the uniqueness of data records
- Determine the location of specific data records in the database
Unique index (UNIQUE)
effect
Avoid duplication of values in a data column in the same table
The difference with the primary key index
There can be only one primary key index
and multiple unique indexes
Regular Index (INDEX)
effect: Quickly locate specific data
note
Both index and key keywords can be set for regular indexes, which
should be added to the search condition field. It is
not advisable to add too many regular indexes, which will affect data insertion, deletion and modification operations
Full text index (FULLTEXT)
effect: Quickly locate specific data
note
Can only be used for data tables of MyISAM type
Can only be used for CHAR, VARCHAR, TEXT data column types
Suitable for large data sets
Manage index
Create index
- Add when creating table
- Append after table creation
ALERT TABLE 表名 ADD 索引类型(数据列名)
Delete index
DROP INDEX 索引名 ON 表名
ALTER TABLE 表名 DROP INDEX 索引名
ALTER TABLE 表名 DROP PRIMARY KEY
View index
SHOW INDEX(或KEYS) FROM 表名
Index guidelines
- Indexes are not as many as possible
- Don't index frequently changing data
- It is recommended not to add indexes for tables with small data volumes
- The index should generally be added to the search condition field
MySQL backup
Database backup necessity
Ensure that important data is not lost
Data transfer
MySQL database backup method
mysqldump backup tool
Database management tools, such as SQLyog
directly copy database files and related configuration files
mysqldump database backup
effect
Dump database,
collect database for backup,
transfer data to another SQL server (not necessarily MySQL service)
mysqldump -h 主机名 –u 用户名 –p [options] 数据库名
[ table1 table2 table3 ] > path/filename.sql
# 备份myschool数据库如:
> mysqldump -u root -p myschool > d:/myschool.sql
EnterPassword: *****
mysqldump common options
MySQL database recovery
method one
Use SOURCE syntax
SOURCE /path/db_name.sql;
/path/ is an absolute path, and must be a file that the mysql running user has permission to read
SOURCE Execute in the MySQL command line
Method two
use mysql client
mysql –u root –p dbname < /path/db_name.sql;
Export and import data using SQL statements
Export
SELECT * INTO OUTFILE 'file_name'
FROM tbl_name
Import
LOAD DATA INFILE 'file_name '
INTO TABLE tbl_name[FIELDS]