MySQL——索引视图事务,存储引擎MyLSAM和InnoDB(实战篇!)

索引的作用

设置了合适的索引之后,数据库利用各种快速的定位技术,能够大大加快查询速率;
特别是当表很大时,或者查询涉及到多个表时,使用索引可使查询加快成千倍;
可以降低数据库的IO成本,并且索引还可以降低数据库的排序成本;
通过创建唯一性索引保证数据表数据的唯一性;
可以加快表与表之间的连接;
在使用分组和排序时,可大大减少分组和排序时间;

1,进入数据库,创建数据库,创建表

[[email protected] ~]# mysql -uroot -p     ##进入数据库
Enter password:     ##输入密码

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

mysql> create database school;    ##创建数据库school
Query OK, 1 row affected (0.00 sec)

mysql> use school;       ##使用数据库
Database changed
mysql> create table info(        ##创建表
        -> id int(4) not null primary key auto_increment,   ##设置主键,自动增加
        -> name varchar(10) not null,       ##名字类型varchar不为空
        -> address varchar(50) default ‘nanjing‘,    ##默认南京
        -> age int(3) not null);
Query OK, 0 rows affected (0.02 sec)

2,向表中插入数据

mysql> insert into info (name,address,age) values (‘zhangsan‘,‘beijing‘,20),(‘lisi‘,‘shanghai‘,22);
##插入数据
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

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

mysql> desc info;               ##查看表结构
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(4)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| address | varchar(50) | YES  |     | nanjing |                |
| age     | int(3)      | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

3,使用create,alter,创建表直接定义三种方式创建索引(普通索引,唯一性索引)

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)

4,创建两个表,进行关联,多表查询

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)

5,全文索引,组合索引

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)

事务的特点

原子性:
事务是一个完整的操作,事务的各元素是不可分的(原子的)
事务中的所有元素必须作为一个整体提交或回滚
如果事务中的任何元素失败,则整个事务将失败
一致性:
当事务完成时,数据必须处于一致状态:在事务开始之前,数据库中存储的数据处于一致状态;在正在进行的事务中,数据可能处于不一致的状态;当事务成功完成时,数据必须再次回到已知的一致状态
隔离性:
对数据进行修改的所有并发事务是彼此隔离的,这表明事务必须是独立的,他不应以任何方式依赖于或影响其他事务
修改数据的事务可以在另一个使用相同数据的事务开始之前访问这些数据,或者再另一个使用相同的数据的事务结束之后访问这些数据
持久性:
事务持久性指不管系统是否发生故障,事务处理的结果都是永久的
一旦事务被提交,事务的效果会被永久的保留在数据库中

1,开启事务,往表中插入数据

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)

2,利用另一个终端查看是否成功插入

[[email protected] ~]# 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)

3,使用回滚,返回保存的节点

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)

4,使用commit提交事务

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)

5,使用另一个终端查看

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)

6,另一种事务的操作方式

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)

存储引擎MyLSAM和InnoDB

1,查看系统默认存储引擎

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

2,修改MySQL配置文件,设置默认的存储引擎

[[email protected] ~]# 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

[[email protected] ~]# systemctl restart mysqld.service ##重启MySQL服务

3,进入数据库

[[email protected] ~]# 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)

谢谢阅读!

原文地址:https://blog.51cto.com/14080162/2453442

时间: 2024-11-10 14:12:13

MySQL——索引视图事务,存储引擎MyLSAM和InnoDB(实战篇!)的相关文章

MySQL索引与事务,视图,存储引擎MylSAM和lnnoDB

MySQL索引与事务,视图,存储引擎MylSAM和lnnoDB 索引的概念 数据库中的索引与书籍中的目录类似: 1.-本书中,无须阅读整本书,利用目录就可以快速查找所需信息2.书中的目录是一个词语列表,其中注明了包含各个词的页码 数据库索引: 1.在数据库中,索弓|使数据库程序无须对整个表进行扫描,就可以在其中找到所需数据2.数据库中的索弓|是某个表中一-列或者若干列值的集合,以及物理标识这些值的数据页的逻辑指针清单 索引的作用: 1.设置了合适的索引之后,数据库利用各种快速的定位技术,能够大大

MySQL中常用的存储引擎(MyISAM 和InnoDB)

 数据库存储引擎是数据库底层软件组件,数据库管理系统(DBMS)使用存储引擎进行创建.查询.更新和删除数据操作.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以获得特定的功能.现在许多的数据库管理系统都支持多种不同的存储引擎.MySQL的核心就是存储引擎. 使用 SHOW ENGINES; 命令查看MySQL支持的存储引擎,  Support 列表示某种引擎是否可以使用,YES表示可以,NO表示不可以,DEFAULT表示当前默认存储引擎. mysql> SH

MySQL索引与事务、存储引擎MyISAM和InnoDB (理论+实践篇)

索引的概念 数据库中的索引与书籍中的目录类似 在一本书中,无须阅读整本书,利用目录就可以快速查找所需信息 书中的目录是一个词语列表,其中注明了包含各个词的页码 数据库索引 在数据库中,索引使数据库程序无须对整个表进行扫描,就可以在其中找到所需数据 数据库中的索引|是某个表中一-列或者若干列值的集合,以及物理标识这些值的数据页的逻辑指针清单 索引的的作用 设置了合适的索引之后,数据库利用各种快速的定位技术,能够大大加快查询速率 特别是当表很大时,或者查询涉及到多个表时,使用索引可使查询加快成3 F

MySql基础知识、存储引擎与常用数据类型

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文将主要讲解了数据库的一些基础知识,介绍了MySql的存储引擎,最后讲了MySql常用的数据类型 1.基础知识 1.1.数据库概述 简单地说:数据库(Database或DB)是存储.管理数据的容器: 严格地说:数据库是"按照某种数据结构对数据进行组织.存储和管理的容器". 总结:数据永远是数据库的核心. 1.2.关系数据库管理系统 通过"数据库管理系统",数据

MySQL索引与事务详解

MySQL索引与事务详解 一.前言 ? 上一章我们讲解了MySQL的手工编译安装流程以及相关的数据库操作命令(sql语句),本文将要详细介绍MySQL索引与事务的概念及原理,并初步了解MySQL数据库视图概念,简述数据库的存储过程. 二.索引 2.1索引的概念--什么是索引? ? 一般来说,一篇论文,或者说一本书,都有其目录,而目录一般是所有章节的概述,或者说是要点核心,而索引的概念其实也与之类似. ? 索引,顾名思义,就是一个方便用户搜索所需资源的引导,只不过在数据库中,索引一般被认为是一种特

MYSQL 浅谈MyISAM 存储引擎

思维导图 介绍 mysql中用的最多存储引擎就是innodb和myisam.做为Mysql的默认存储引擎,myisam值得我们学习一下,以下是我对<高性能MYSQL>书中提到的myisam的理解,请大家多多指教. 特点 > 不支持事务 证明如下: >> 表记录:t2表的engine是myisam. >> 操作 注意:如果你在数据库进行事务操作,但是事务无法成功,你就要看你的表引擎了,看这种引擎是否支持事务. >> 下面请看innodb中的事务操作 &g

转!!MySQL中的存储引擎讲解(InnoDB,MyISAM,Memory等各存储引擎对比)

MySQL中的存储引擎: 1.存储引擎的概念 2.查看MySQL所支持的存储引擎 3.MySQL中几种常用存储引擎的特点 4.存储引擎之间的相互转化 一.存储引擎: 1.存储引擎其实就是如何实现存储数据,如何为存储的数据建立索引以及如何更新,查询数据等技术实现的方法. 2.MySQL中的数据用各种不同的技术存储在文件(或内存)中,这些技术中的每一种技术都使用不同的存储机制,索引技巧,锁定水平并且最终提供广泛的不同功能和能力.在MySQL中将这些不同的技术及配套的相关功能称为存储引擎. 二.MyS

MySQL存储引擎 - Myisam和Innodb

Mysql有两种存储引擎:InnoDB与Myisam,下表是两种引擎的简单对比   MyISAM InnoDB 构成上的区别: 每个MyISAM在磁盘上存储成三个文件.第一个 文件的名字以表的名字开始,扩展名指出文件类型..frm文件存储表定义.数据文件的扩 展名为.MYD (MYData).索引文件的扩 展名是.MYI (MYIndex). 基于磁盘的资源是InnoDB表空间数据文件和它的日志文件,InnoDB 表的 大小只受限于操作系统文件的大小,一般为 2GB 事务处理上方面: MyISA

MySQL学习笔记(1) - 存储引擎概述

和大多数数据库不同,在MySQL中存在一个存储引擎的概念,针对不同的存储需求可以选择最优的存储引擎. 一.MySQL存储引擎概述 插件式存储引擎是MySQL数据库最重要的特征之一,用户可以根据应用的需要寻找如何存储和索引数据.是否使用事务等.MySQL默认支持多种存储引擎,以适用于不同领域的数据库应用需求,用户可以通过选择选择不同的存储引擎提供应用的效率,提供灵活的存储,用户甚至可以按照自己的需要定制和使用自己的存储引擎,以实现最大程度的可定制性. MySQL支持的存储引擎包括MyISAM.In