MySQL indexing and transaction storage engine MyISAM and InnoDB (theory + practice papers)

The concept of index

Database indexing and similar books in the catalog

  • In a book without reading the whole book, you can use the directory to quickly find the information
  • Contents of the book is a list of words, which indicate the page numbers of each word

Database Index

  • In the database, index the database program without having to scan the entire table, you can find the required data in which
  • Database index | a is a table - a list of logical data page pointer column or several columns of set values, and these values ​​of physical identification

Index role

  • After setting the appropriate index, fast database using a variety of positioning technology, can greatly accelerate the rate of inquiry
  • Especially when the table is large, or a plurality of tables related to the query, the query can use the index to speed up the times 3 F
  • I0 is possible to reduce the cost of the database, and may also reduce the cost of sorting the index database
  • The connection between the table and the table of accelerate - by creating a unique index to ensure that data table data CD -
  • When using grouping and sorting, grouping and sorting can greatly reduce the time

Category Index

General index

  • This is the most basic index types, and it does not limit the uniqueness and the like

Unique index

  • This index and the previous "general index" is basically the same, but with one difference: all values ​​of the index list can only occur once, that must be unique

Primary key

  • Primary key is a unique index, but it must be designated as the "PRIMARY KEY"

Full-text index

  • MySQL version 3.23.23 from the start to support full-text indexing and full-text search. In MySQL, the full-text index of the index type FULLTEXT, full-text index can be created on columns of type VARCHAR or TEXT

Single and multi-column index Index

  • Indexes can be created on a separate index, the index can be created on multiple columns

Creating an index based on the principle of

  • Primary key, foreign key must have an index
  • The amount of data of more than 300 rows of the table should be indexed
  • Tables are often connected with other tables, in connection field should be indexed
  • Uniqueness field is not too bad for indexing
  • Too frequently updated fields unsuitable for creating an index
  • Often appear in the Where clause field, especially in the field of large tables should be indexed
  • The index should be based on highly selective field
  • Index should be built on a small field, even for large text fields long field, not to build index

Method of creating an index

  • According to business needs, after selecting the appropriate index, you can create an index using the CREATE INDEX
  • CREATE INDEX with the keyword each index can create various types of indexes

Create a regular index

CREATE INDEX <索引的名字> ON tablename(列的列表);

Examples of the general index

CREATE INDEX salary index ON IT salary(薪资);

Create a unique index

CREATE UNIQUE INDEX <索引的名字> ON tablename (列的列表);

Examples unique index

CREATE UNIQUE INDEX salary_unique_index ON IT_salary(姓名);

Create a primary key index

CREATE TABLE tablename ( [..], PRIMARY KEY (列的列表) );
ALTER TABLE tablename ADD PRIMARY KEY (列的列表); //添加表结构的方式创建主键索引

Create a primary key index example

ALTER TABLE IT_ salary ADD PRIMARY KEY (员工ID);

View Index

SHOW INDEX FROM tablename;
SHOW KEYS FROM tablename;

View Indexing Case

SHOW INDEX FROM IT_salary;
SHOW KEYS FROM IT_salary;

The concept Affairs

  • A transaction is a mechanism, a sequence of operations comprises a set of database operations command and all commands to be submitted as a whole along with the request to undo or system, i.e., a set of database commands are either executed or not executed
  • A transaction is a logical unit of work indivisible when performing concurrent operations on the database system, the control unit is the smallest transaction
  • For multi-user database system while scenes operations, such as banks, insurance companies and securities trading systems, etc.
  • By the integrity of the transaction in order to ensure data consistency

Features ACID transactions

Atomicity (Atomicity)

  • A transaction is a complete operation, each element of the transaction can not be divided (atoms)
  • All elements of the transaction must be committed or rolled back as a whole
  • If any element in the transaction fails, the entire transaction fails

Consistency (Consistency)

  • When the transaction is completed, the data must be in a consistent state: Before the beginning of the transaction, the data stored in the database in a consistent state; in the ongoing transaction, data may be in an inconsistent state; When the transaction is completed successfully, the data must go back again known consistent state

Isolation. (Isolation)

  • All data can be modified concurrent transactions are isolated from each other, indicating that the transaction must be independent, it should not in any way affect or depend on other matters
  • Access the data after the end of the transaction the transaction to modify the data can use the same data in another transaction before the start of the data access, or use the same data in another

Persistent (Durability)

  • Transaction durability means that regardless of whether the system is a failure, the result of the transaction are permanent
  • Once the transaction is committed, the transaction's effect will be permanently retained in the database

Operation of its services

  • Under identify cases MySQL transaction is automatically submitted when sq | when the statement is submitted to the transaction will be automatically submitted
  • Manual transaction method for controlling
    • Transaction command control
    • Set transaction mode using the set

Command Transaction Control Transactions

  • begin: start a transaction
  • commit: commit a transaction
  • savepoint <tag name> marking / Archive
  • rollback: rollback a transaction

Use the set command to control

  • et autocommit = 0: disable automatic submission
  • set autocommit = 1: enable auto-commit

Storage Engine Concepts

  • Data in MySQL using different technologies stored in the file, each technology uses a different storage mechanism, indexing techniques, lock level and ultimately provide different functions and capabilities of these different technologies and supporting functions in MySQL called storage engine
  • MySQL storage engine is stored way data is stored in the file system or storage format
  • MySQL currently two commonly used storage engine
    • MyISAM (lightweight)
    • InnoDB
  • MySQL storage engine in MySQL database server is the component responsible for performing the actual data for the database operation 1/0
  • One of the major advantages of using a special storage engine is to provide only the desired specific characteristics of the application, the database system overhead smaller, more potent and higher performance database
  • MySQL system, the engine is stored on the file system, to save the storage engine is transmitted to the data before the data file, after the storage format for storage in accordance with the respective storage engine

MyISAM Introduction

  • MyISAM storage engine is a relational database system MySQL default storage engine version prior to 5.5, the predecessor of the ISAM
  • ISAM is a well-defined and time-tested form of data management in the design to take into account the number of times the database is much larger than the update query
  • ISAM features
    • ISAM speed read operation is performed quickly,
    • It does not support transaction processing
    • And do not take up a lot of memory and storage resources
    • Can not fault tolerant
  • Data storage location:
    • Table definition file
    • Table data storage file
    • Table index file

MyISAM features

  • It does not support transactions
  • In the form of table-level locking, locking the entire data table is updated
  • Database block each other in the process of reading and writing
    • It will block the user data in the process of reading the data written
    • It will block user data in the process of writing the read data
  • Can be set by key_buffer_size cache index, improve access performance and reduce disk IO pressure
    • But the cache will only cache index files are not cached data
  • Employed alone MyISAM storage engine to write or read data, the process speed is fast and relatively small footprint
  • It MyISAM storage engine does not support foreign key constraints, only supports full-text indexing
  • Each MyISAM storage on disk into three files, each - - the name of a file begins with the name of the table, the extension indicates the file type
  • MyISAM storage on disk file
    • frm file stores the table
    • Extended data file named .MYD (MYData)
    • Extension index file is .MYI (MYIndex)

Examples of suitable production scenarios MyISAM

  • We do not need to support the company's business affairs
  • Unilateral generally read more data services, write data unilateral or more business
  • MyISAM storage engine to read and write data are relatively frequent scenes not suitable for relatively low read-write concurrent access business
  • Relatively small traffic data modification
  • Data services - consistency requirement is not very high service server relatively poor hardware resources,

InnoDB features introduced

  • Support Services: Support four transaction isolation level
  • Row-level locking, but it will still be a full table scan read and write table-level locking obstruction associated with the transaction isolation level
  • In a highly efficient caching feature: to cache index, the data can also be cached
  • The primary key table stored in cluster 3
  • Support partition, table space, similar to the oracle database
  • Support foreign key constraints, does not support full-text indexing 5.5 before, 5.5 after version supports full-text indexing
  • Hardware resource requirements is still relatively high occasions

InnoDB applicable production scene analysis

  • Support business needs Affairs
  • Row-level locking has a good ability to adapt to high concurrency, but need to make sure that the query is done by index
  • Business data is frequently updated scenarios, such as: forums, microblogging and other
  • Business data consistency are higher, such as: banking
  • Large memory hardware, use Innodb better caching capabilities to improve memory utilization, reduce disk I0 pressure

Based enterprises to choose the storage engine

  • We need to consider each different storage engine which provides the core functionality and application scenarios
  • Support of fields and data types
    • All primers | engines support common data types
    • But not all engines support other field types, such as binary objects
  • Lock Type: different storage engines support different levels of locking
    • Table lock
    • Line Lock

Configuring Storage Engine

  • After selecting the appropriate storage engine in the enterprise, you can modify the
  • Modify the
    • View configurable database storage engine
    • View table storage engine being used
    • Storage configuration for the selected type of engine
  • Use show engines view supported by the system storage engine
  • View storage engine used by the table
    • Method 1: show table status from the library name where name = 'name;
    • Method 2: show create table name;

Modify the storage engine

  • Method 1: alter table modification;
    • alter table table_ name engine=引擎;
  • Method 2: Modify the my.cnf, specify the default storage engine and restart the service
    • default-storage-engine=InnDB
  • Method 3: Specify the storage engine when creating a table create table
    • create table 表名 (字段) engine= 引擎
  • Method 4: Mysql convert table format conversion engine storage
    • Mysql convert table format -user=root -password=密码
    • sock=/tmp/mysql.sock-engine=引擎 库名 表名

Examples

Creating an index

mysql> create index index_age on info (age);   ##创建普通索引
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;   ##查看表中的索引
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY   |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | index_age |            1 | age         | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index index_age on info;   ##删除表中的索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;        ##查看表中的索引         
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.01 sec)

mysql> create unique index unique_name on info (name); ##创建唯一性索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;  ##查看表中索引
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY     |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | unique_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index unique_name on info;  ##删除表中的索引
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;          ##查看表中的索引
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> alter table info add unique index index_name (name);   ##使用alter插入表索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;   ##查看表中的索引
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

Full-text index, a composite index

mysql> select * from info;     ##查看表内容
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> show index from info;    ##查看表的索引
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> create fulltext index full_addr on info (address);      ##以address创建全文索引
Query OK, 0 rows affected, 1 warning (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show index from info;  ##查看表索引
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | full_addr  |            1 | address     | NULL      |           2 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

mysql> select * from user;     ##查看user表内容
+----+--------+-------+-------+
| id | name   | score | hobby |
+----+--------+-------+-------+
|  1 | test01 |    88 |     3 |
|  2 | stu01  |    99 |     2 |
|  3 | wangwu |    77 |     3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)

mysql> create index index_name_score on user (name,score);    ##创建name和score的组合索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from user;   ##查看表索引
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY          |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_score      |            1 | score       | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            1 | name        | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            2 | score       | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)

Two tables, association, multi-table query

mysql> create table user(     ##创建user表
        -> id int(4) not null primary key auto_increment,   ##设置主键和自动增加
        -> name varchar(10) not null,
        -> score decimal not null,
        -> hobby int(2) not null default '1',  ##默认1
        -> index index_score (score));    ##设置索引score
Query OK, 0 rows affected (0.01 sec)

mysql> desc user;  ##查看表结构
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int(4)        | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)   | NO   |     | NULL    |                |
| score | decimal(10,0) | NO   | MUL | NULL    |                |
| hobby | int(2)        | NO   |     | 1       |                |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> insert into user (name,score,hobby) values ('test01',88,1),('stu01',99,2),('wangwu',77,3); 
##向表中插入数据
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user;    ##查看表内容
+----+--------+-------+-------+
| id | name   | score | hobby |
+----+--------+-------+-------+
|  1 | test01 |    88 |     1 |
|  2 | stu01  |    99 |     2 |
|  3 | wangwu |    77 |     3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)

mysql> create table hob(     ##创建hob表
        -> id int(2) not null primary key,
        -> hob_name varchar(10) not null);
Query OK, 0 rows affected (0.00 sec)

mysql> desc hob;   ##查看表结构
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(2)      | NO   | PRI | NULL    |       |
| hob_name | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into hob (id,hob_name) values (1,'看书'),(2,'运动'),(3,'听歌');   ##插入表数据
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from hob;   ##查看表内容
+----+----------+
| id | hob_name |
+----+----------+
|  1 | 看书     |
|  2 | 运动     |
|  3 | 听歌     |
+----+----------+
3 rows in set (0.00 sec)

mysql> select * from user inner join hob on user.hobby=hob.id;   ##关联user和hob两张表
+----+--------+-------+-------+----+----------+
| id | name   | score | hobby | id | hob_name |
+----+--------+-------+-------+----+----------+
|  1 | test01 |    88 |     1 |  1 | 看书     |
|  2 | stu01  |    99 |     2 |  2 | 运动     |
|  3 | wangwu |    77 |     3 |  3 | 听歌     |
+----+--------+-------+-------+----+----------+
3 rows in set (0.00 sec)

mysql> select user.name,hob.hob_name from user inner join hob on user.hobby=hob.id;
##去除其他内容显示name和hob_name内容
+--------+----------+
| name   | hob_name |
+--------+----------+
| test01 | 看书     |
| stu01  | 运动     |
| wangwu | 听歌     |
+--------+----------+
3 rows in set (0.00 sec)

mysql> select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id; ##设置简易名称
+--------+----------+
| name   | hob_name |
+--------+----------+
| test01 | 看书     |
| stu01  | 运动     |
| wangwu | 听歌     |
+--------+----------+
3 rows in set (0.00 sec)

mysql> create view view_user as select u.name,h.hob_name from user u inner join hob h on u.hobby
##创建视图
Query OK, 0 rows affected (0.00 sec)

mysql> select * from view_user;  ##查看视图
+--------+----------+
| name   | hob_name |
+--------+----------+
| test01 | 看书     |
| stu01  | 运动     |
| wangwu | 听歌     |
+--------+----------+
3 rows in set (0.00 sec)

mysql> update user set hobby=3 where name='test01';  ##修改user表中内容
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from view_user;   ##查看视图,即视图就是表的一个链接
+--------+----------+
| name   | hob_name |
+--------+----------+
| stu01  | 运动     |
| test01 | 听歌     |
| wangwu | 听歌     |
+--------+----------+
3 rows in set (0.00 sec)

Transaction instance

Open affairs, inserting data into the table

mysql> select * from info;   ##查看表内容数据
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> insert into info (name,address,age) values ('wangwu','hangzhou',30);##插入数据

mysql> begin;   ##开启事务
Query OK, 0 rows affected (0.00 sec)

mysql> insert into info (name,address,age) values ('zhaoliu','hangzhou',31);  ##插入数据
Query OK, 1 row affected (0.00 sec)

mysql> savepoint a;   ##设置保存节点a
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;   ##查看表数据
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

mysql> insert into info (name,address,age) values ('tianqi','hangzhou',32);  ##继续插入数据
Query OK, 1 row affected (0.00 sec)

mysql> savepoint b;   ##设置保存节点b
Query OK, 0 rows affected (0.00 sec)

mysql> insert into info (name,address,age) values ('heiba','hangzhou',32);      ##继续插入数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;  ##查看表内容
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
|  5 | tianqi   | hangzhou |  32 |
|  6 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
6 rows in set (0.00 sec)

Use another terminal to see if successfully inserted

[root@master2 ~]# mysql -uroot -p  ##进入数据库
Enter password:     ##输入密码

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use school;   ##使用数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from info;   ##查看表内容,此时并没有提交
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)

Rollback, return the saved node

mysql> rollback to b;    ##利用回滚到保存节点b
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;   ##查看表数据
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
|  5 | tianqi   | hangzhou |  32 |
+----+----------+----------+-----+
5 rows in set (0.00 sec)

mysql> rollback to a;  ##回滚到保存节点a
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;   ##查看表数据
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

mysql> rollback;     ##回滚到初始,退出事务状态
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;  ##查看表数据
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)

Use commit to commit the transaction

mysql> begin;    ##开启事务
Query OK, 0 rows affected (0.00 sec)

mysql> insert into info (name,address,age) values ('heiba','hangzhou',32);  ##插入数据
Query OK, 1 row affected (0.00 sec)

mysql> commit;   ##提交事务
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;  ##查看表数据
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

Use another terminal to view

mysql> select * from info;   ##查看表数据
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

Another transaction mode of operation

mysql> set autocommit=0;   ##设置不自动提交事务
Query OK, 0 rows affected (0.00 sec)

mysql> update info set address='beijing' where name='heiba';  ##修改表数据
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from info;   ##查看表信息
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | beijing  |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

##另一个终端查看
mysql> select * from info;  ##查看表信息,并没有修改
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

mysql> set autocommit=1;   ##开启自动提交事务
Query OK, 0 rows affected (0.00 sec)

##另一个终端查看
mysql> select * from info;   ##查看表数据,此时就已经修改
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | beijing  |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

Storage Engine

View the default storage engine

mysql> show engines;   ##查看默认存储引擎innodb
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |

mysql> show create table info;   ##查看创建的表的存储引擎innodb

| info  | CREATE TABLE "info" (
    "id" int(4) NOT NULL AUTO_INCREMENT,
    "name" varchar(10) NOT NULL,
    "address" varchar(50) DEFAULT 'nanjing',
    "age" int(3) NOT NULL,
    PRIMARY KEY ("id"),
    UNIQUE KEY "index_name" ("name"),
    FULLTEXT KEY "full_addr" ("address")
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

Modify the MySQL configuration file, set the default storage engine

[root@localhost ~]# vim /etc/my.cnf ##修改配置文件

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysql.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
default-storage-engine=Myisam   ##添加默认存储引擎为Myisam

[root@master2 ~]# systemctl restart mysqld.service ##重启MySQL服务

Access to the database

[root@master2 ~]# mysql -uroot -p   ##进入数据库
Enter password:    ##输入密码

mysql> use school;    ##使用数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table a ( id int );    ##创建一个a表
Query OK, 0 rows affected (0.00 sec)

mysql> show create table a;   ##查看表默认的存储引擎Myisam
+-------+-------------------------------------------------------------------------------------+
| Table | Create Table                                                                        |
+-------+-------------------------------------------------------------------------------------+
| a     | CREATE TABLE "a" (
    "id" int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table a engine=innodb;  ##修改表的存储引擎为innodb
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table a;  ##查看表的存储引擎innodb
+-------+-------------------------------------------------------------------------------------+
| Table | Create Table                                                                        |
+-------+-------------------------------------------------------------------------------------+
| a     | CREATE TABLE "a" (
    "id" int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Guess you like

Origin blog.51cto.com/14473285/2457902