目录
数据库利用各种各样的快速定位技术,能够大大提高查询效率。特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍。
优点
索引查询是数据库中重要的记录查询方法,要不要建立索引以及在那些字段上建立索引都要和实际数据库系统的查询要求结合来考虑。
索引建立的原则:
- mysql> create database auth;
- Query OK, 1 row affected (0.00 sec)
-
- mysql> use auth;
- Database changed
-
- mysql> create table users(id int(10),user_name char(20),user_pass char(50));
- Query OK, 0 rows affected (0.02 sec)
- mysql> insert into users values(1,'aaa','pwd123');
- Query OK, 1 row affected (0.01 sec)
- mysql> insert into users values(2,'bbb','pwd123');
- Query OK, 1 row affected (0.00 sec)
- mysql> insert into users values(3,'ccc','pwd123');
- Query OK, 1 row affected (0.00 sec)
-
- mysql> create index xxx on users(user_name(15)); (直接创建索引)
- Query OK, 0 rows affected (0.02 sec)
- Records: 0 Duplicates: 0 Warnings: 0
xxx :索引名称
users(user_name(15)):users表,user_name表中的列,15索引长度。
查看索引的方式:
- mysql> show create table users\G;
- 或
- mysql> show index from users\G;
- mysql> alter table users add index yyy (user_pass(30));
- Query OK, 0 rows affected (0.01 sec)
- Records: 0 Duplicates: 0 Warnings: 0
备注:
30是定义字符长度,只能比初始值小,不能比初始值大。
- CREATE TABLE table01 (
- id int(11) NOT NULL AUTO_INCREMENT ,
- title char(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
- content text CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
- time int(10) NULL DEFAULT NULL ,
- PRIMARY KEY (id),
- INDEX index_table01_title (title(11))
- );
- mysql> create unique index ddd on t1(id);
- Query OK, 0 rows affected (0.02 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> alter table t1 add unique fff(id);
- Query OK, 0 rows affected, 1 warning (0.01 sec)
- Records: 0 Duplicates: 0 Warnings: 1
- mysql> create table t2 ( id int(10) not null, name char(20), sex char(2), unique ddd (id) );
- Query OK, 0 rows affected (0.00 sec)
(1)创建主索引语法
- mysql> create table t3 (id int(10),name char(20),primary key (id));
- Query OK, 0 rows affected (0.01 sec)
(2)
- mysql> create table t4 (id int(10),name char(20),age int(2),index ggg(id,name,age));
- Query OK, 0 rows affected (0.02 sec)
组合索引查找方式:
mysql> select * from t4 where id='10' and name='zhansan' and age=16;
注意:
在select语句中where依次从左往右执行。若索引组合是index(id,name,age)。在查询语句中也应该是id,name,age的顺序必须和索引顺序一致,否则无效。
(1)直接创建索引语法
- mysql> create fulltext index hhh on t4 (name);
- Query OK, 0 rows affected, 1 warning (0.08 sec)
- Records: 0 Duplicates: 0 Warnings: 1
(2)修改表结构添加全文索引
- mysql> alter table t1 add fulltext xxx(name);
- Query OK, 0 rows affected, 1 warning (0.09 sec)
- Records: 0 Duplicates: 0 Warnings: 1
(3)创建表时,建立全文索引
- mysql> create table t6 (
- -> id int(10) not null,
- -> name char(20) not null,
- -> info varchar (255),
- -> fulltext index t6(info));
- Query OK, 0 rows affected (0.06 sec)
查看索引方法:
- mysql>show index from t1\G;
- 或
- mysql>show keys from t2\G;
- 或
- mysql>show create table t3 \G
索引的删除有如下两种方法:
DROP INDEX 索引名 ON 表名;
ALTER TABLE 表名 DROP INDEX 索引名;
MySQL 事务主要用于处理操作量大,复杂度高的数据。比如说,在人员管理系统中,要删除一个人员,即需要删除人员的基本资料,又需要删除和该人员相关的信息,如信箱,文章等等。这样,这些数据库操作语句就构成一个事务。这是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么都不执行,事务用来管理insert,update,delete语句。
是一个不可分割的工作逻辑单元,在数据库系统上执行并发操作时,事务是最小的控制单元适用于多用户同时操作的数据库系统的场景,如银行、保险公司及证券交易系统等等通过事务的整体性以保证数据的一致性