1.数据库利用各种快速定位技术,能够大大加快查询速率
2.当表很大或查询涉及到多个表时,可以成千上万倍地提高查询速度
3.可以降低数据库的IO成本,并且还可以降低数据库的排序成本, IO:输入(写入、更改数据),输出(读取数据)
4.通过创建唯一性索引保证数据表数据的唯一性
5.可以加快表与表之间的连接
6.在使用分组和排序时,可大大减少分组和排序时间
1、表的主键、外键必须有索引
2、数据量超过300行的表应该有索引
3、经常与其他表进行连接的表,在连接字段上应该建立索引
4、唯一性太差的字段不适合建立索引
5、更新太频繁地字段不适合创建索引
6、经常出现在 Where子句中的字段,特别是大表的字段,应该建立索引
7、索引应该建在选择性高的字段上
8、索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引
最基本的索引,没有唯一性之类的限制
创建方式:
直接创建
mysql> create index index_name on ky06(chengji);
mysql> mysql> show index from ky06;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ky06 | 0 | PRIMARY | 1 | id | A | 5 | NULL | NULL | | BTREE | | |
| ky06 | 1 | index_name | 1 | chengji | A | 5 | NULL | NULL | YES | BTREE | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
创建表结构时创建
mysql> create table nice(id int(3) primary key auto_increment,name varchar(10),money int(255),index index_id (id));
Query OK, 0 rows affected (0.00 sec)
mysql> show index from nice;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| nice | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| nice | 1 | index_id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
修改表结构时创建
mysql> alter nice add index index_name(name);
show index from nice;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| nice | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| nice | 1 | index_id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| nice | 1 | index_name | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
是一种特殊的唯一索引,指定为primary key,一个表只能有一个主键,与唯一索引的区别是不允许有空值(非空且唯一)
创建表结构时创建
create table xiu(id int(3) primary key auto_increment,name varchar(10),money int(255));
修改表结构时创建
mysql> create table vip(id int(2),name varchar(10));
Query OK, 0 rows affected (0.00 sec)
mysql> alter table vip add primary key(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
与普通索引的区别是索引列的所有值只能出现一次,即必须唯一
直接创建
mysql> create unique index index_id on vip(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from vip;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| vip | 0 | index_id | 1 | id | A | 2 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
创建表结构时创建
mysql> create table svip (id int(2) ,name varchar(10),money int(255),unique index index_id(id));
Query OK, 0 rows affected (0.01 sec)
mysql> show index from svip;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| svip | 0 | index_id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
可以是单列上创建的索引,也可以是在多列上创建的索引,遵循最左原则,从左往右依次执行。
mysql> create table vvip (id int(2) ,name varchar(10),money int(255), index index_id(id,name));
Query OK, 0 rows affected (0.01 sec)
mysql> show index from vvip;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| vvip | 0 | index_id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
| vvip | 0 | index_id | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
创建表时创建全文索引(必须为字符型字段)
mysql> create table ssk (id int(2) ,name varchar(10),money int(255),fulltext(name));
Query OK, 0 rows affected (0.02 sec)
mysql> show index from ssk;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ssk | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
修改表的方式添加全文索引
mysql> alter table vip add fulltext index_ab(name);
Query OK, 0 rows affected, 1 warning (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show index from vip
-> ;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| vip | 0 | index_id | 1 | id | A | 2 | NULL | NULL | YES | BTREE | | |
| vip | 1 | index_ab | 1 | name | NULL | 2 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
方法一:
mysql> drop index index_ab on vip;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
方法二:
mysql> alter table svip drop index index_id;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
MySQL事务默认是自动提交的,当SQL语句提交时事务便自动提交
BEGIN或START TRANSACTION
COMMIT
ROLLBACK
SAVEPOINT identifier
RELEASE SAVEPOINT identifier
ROLLBACK TO identifier
SET TRANSACTION
事务处理命令控制事务
BEGIN:开始一个事务
COMMIT:提交一个事务
ROLLBACK:回滚一个事务
使用set命令进行控制
set autocommit=0:禁止自动提交
set autocommit=1:开启自动提交
事务的操作
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into vip (id,name,money) values(3,'xiye',169);
Query OK, 1 row affected (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
MySQL存储引擎是 MySQL数据库服务器中的组件,负责为数据库执行实际的数据I/O操作
使用特殊存储引擎的主要优点之一在于:
仅需提供特殊应用所需的特性;
数据库中的系统开销较小;
具有更有效和更高的数据库性能。
MySQL系统中,存储引擎处于文件系统之上,在数据保存到数据文件之前会传输到存储引擎,之后按照各个存储引擎的存储格式进行存储
MyISAM不支持事务,也不支持外键
访问速度快
对事务完整性没有要求
MyISAM在磁盘.上存储成三个文件
●.frm文件存储表定义
●数据文件的扩展名为.MYD (MYData)
●索引文件的扩展名是.MYI (MYIndex)
表级锁定形式,数据在更新时锁定整个表
数据库在读写过程中相互阻塞 会在数据写入的过程阻塞用户数据的读取 也会在数据读取的过程中阻塞用户的数据写入
数据单独写入或读取,速度过程较快且占用资源相对
MyIAM支持的存储格式
●静态表
●动态表
●压缩表
适用场景:
公司业务不需要事务的支持
单方面读取或写入数据比较多的业务
MyISAM存储引擎数据读写都比较频繁场景不适合
使用读写并发访问相对较低的业务
数据修改相对较少的业务
对数据业务-致性要求不是非常高的业务
服务器硬件资源相对比较差
特点
方法一:alter table修改
mysql> alter table 库名 engine=MyISAM;
方法二:修改my.cnf配置文件,指定默认存储引擎并重启服务
vim my.cnf
default-storage-engine=InnoDB
方法三:create table创建表时指定存储引擎
mysql> create table engine moon(id int) engine=MyISAM;