数据库中的索引与书籍中的目录类似
数据库索引
普通索引
唯一性索引
主键
全文索引
单列索引与多列索引
根据企业需求选择了合适的索引之后,可使用CREATE INDEX创建索引
CREATE INDEX加上各个索引关键字便可创建各个类型的索引
创建普通索引
CREATE INDEX <索引的名字> ON tablename(列名)
mysql> create index index_name on info(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
创建唯一性索引
CREATE UNIQUE INDEX <索引的名字> ON tablename(列名)
mysql> create unique index unique_index_addr on info(addr);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
创建主键索引
(1)创建表的时候
CREATE TABLE tablename ([...],PRIMARY KEY (列名));
mysql> create table a (id int,primary key (id));
Query OK, 0 rows affected (0.02 sec)
(2)表已经存在的时候
ALTER TABLE tablename ADD PRIMARY KEY (列名);
mysql> alter table test add primary key(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
创建全局索引
mysql> create fulltext index full_addr on test01 (addr);
Query OK, 0 rows affected, 1 warning (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 1
创建组合索引
mysql> create index index_id_name on test01 (id,name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
索引查看(使用两种方法结果一样)
SHOW INDEX FROM tablename
SHOW KEYS FROM tablename
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_index_addr | 1 | addr | A | 2 | NULL | NULL | YES | BTREE | | |
| info | 1 | index_name | 1 | name | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
mysql> show keys 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_index_addr | 1 | addr | A | 2 | NULL | NULL | YES | BTREE | | |
| info | 1 | index_name | 1 | name | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
原子性(Atomicity)
一致性(Consistency)
隔离性(Isolation)
持久性(Durability)
默认情况相爱MySQL的事务是自动提交的,当sql语句提交时事务便自动提交
手动对事务进行控制的方法
事务处理命令控制事务
提交事务操作
#输入begin命令,进入事务处理项,它是在内存上执行的
mysql> begin;
Query OK, 0 rows affected (0.01 sec)
#创建一条记录
mysql> insert into info (name,addr) values ('cheng','hz');
Query OK, 1 row affected (0.00 sec)
#提交事务
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
#查看记录存在表中
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 9 | cheng | hz |
+----+----------+------+
3 rows in set (0.00 sec)
回滚事务操作
#输入begin命令,进入事务处理项
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
#创建一条记录
mysql> insert into info (name,addr) values ('wangwu','nj');
Query OK, 1 row affected (0.00 sec)
#查看存在表上
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 7 | wangwu | nj |
+----+----------+------+
3 rows in set (0.00 sec)
#存档
mysql> savepoint a;
Query OK, 0 rows affected (0.00 sec)
#再创建一条记录
mysql> insert into info (name,addr) values ('cheng','hz');
Query OK, 1 row affected (0.01 sec)
#查看存在表上
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 7 | wangwu | nj |
| 8 | cheng | hz |
+----+----------+------+
4 rows in set (0.00 sec)
#执行回滚操作,回滚到存档 a
mysql> rollback to a;
Query OK, 0 rows affected (0.00 sec)
#查看数据表,上一条加的记录已被撤销
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 7 | wangwu | nj |
+----+----------+------+
3 rows in set (0.00 sec)
#继续执行回滚操作,回滚到begin位置,此时事务处理结束,再次进行需要重新开始
mysql> rollback;
Query OK, 0 rows affected (0.02 sec)
#查看表记录均已被撤销
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
+----+----------+------+
2 rows in set (0.00 sec)
使用set命令进行控制
关闭自动提交
mysql> set autocommit=0;
Query OK, 0 rows affected (0.01 sec)
#创建一条记录
mysql> insert into info (name,addr) values ('xiaoming','nj');
Query OK, 1 row affected (0.00 sec)
#查看数据表
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 9 | cheng | hz |
| 11 | xiaoming | nj |
+----+----------+------+
4 rows in set (0.00 sec)
#保存
mysql> commit;
Query OK, 0 rows affected (0.02 sec)
#查看数据表
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 9 | cheng | hz |
| 11 | xiaoming | nj |
+----+----------+------+
4 rows in set (0.00 sec)
开启自动提交
mysql> set autocommit=1;
Query OK, 0 rows affected (0.00 sec)
#创建一条记录
mysql> insert into info (name,addr) values ('xiaohong','sz');
Query OK, 1 row affected (0.01 sec)
#查看数据表
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 9 | cheng | hz |
| 11 | xiaoming | nj |
| 12 | xiaohong | sz |
+----+----------+------+
5 rows in set (0.00 sec)
#开启自动提交后是无法执行回滚操作的
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
#查看数据还在表中
mysql> select * from info;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | zhangsan | nj |
| 2 | lisi | bj |
| 9 | cheng | hz |
| 11 | xiaoming | nj |
| 12 | xiaohong | sz |
+----+----------+------+
5 rows in set (0.00 sec)
视图是一张虚拟的表,数据不存在视图中,真实表的映射数据,利用条件筛选、分组、排序等产生出一个结果集,并且做成持久化保存
视图能够方便友好的查询数据,基本不占用空间
真实数据变化,视图数据会同时变化
视图的作用:
视图命令
create view 视图表名 查看命令
mysql> create view view_info as select id,name from info;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from view_info;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 9 | cheng |
| 11 | xiaoming |
| 12 | xiaohong |
+----+----------+
5 rows in set (0.02 sec)
存储过程多用于软件开发方向,防止代码在网络传输过程中被截获,做了安全性保障
原始状态:代码中需要嵌入sql语句,通过连接驱动,把sql语句作为参数传递给mysql(数据库)进行执行
存储过程是写在数据库中,并不是程序中
程序是通过调用存储过程名称去触发sql操作
优点:代码量优化,传输安全,网络优化