mysql 的视图 触发器 事务 存储过程 内置函数 流程控制 索引

视图

    1 什么是视图

    视图就是通过查询得到一张虚拟表,然后保存下来,下次用的直接使用即可。

  2 为什么要使用视图

    如果要频繁使用一张虚拟表,可以不用重复查询

  3如何使用视图

    创建视图

    create view teacher2course as  

    select * from teacher inner join course

      on teacher.tid = course.teacher_id;

    删除

    drop view teacher2course;

  强调
    1、在硬盘中,视图只有表结构文件,没有表数据文件
    2、视图通常是用于插叙,尽量不要修改视图中的数据

触发器

  01 什么是触发器

      在满足对某张表数据的增、删、改的情况下,自动触发的功能称之为触发器

  02 为何要用触发器?

    触发器专门针对我们对某一张表数据增insert、删delete、改update的行为,这类行为一旦执行就会触发触发器的执行,即自动运行另外一段sql代码

  03 创建触发器语法

    # 针对插入

    create trigger tri_after_insert_t1 after insert on 表名 for each row

    begin

      sql代码。。。。

    end

     # 针对删除

     create trigger tri_after_delete_t1 after delete on 表名 for each row

    begin

      sql代码

    end

    # 针对修改

    create trigger tri_after_update_t1 after update on 表名 for each row

    begin

      sql 代码

    end

  04 案例

 1 CREATE TABLE cmd (
 2     id INT PRIMARY KEY auto_increment,
 3     USER CHAR (32),
 4     priv CHAR (10),
 5     cmd CHAR (64),
 6     sub_time datetime, #提交时间
 7     success enum ('yes', 'no') #0代表执行失败
 8 );
 9 
10 CREATE TABLE errlog (
11     id INT PRIMARY KEY auto_increment,
12     err_cmd CHAR (64),
13     err_time datetime
14 );
15 
16 delimiter $$
17 create trigger tri_after_insert_cmd after insert on cmd for each row
18 begin
19     if NEW.success = 'no' then
20         insert into errlog(err_cmd,err_time) values(NEW.cmd,NEW.sub_time);
21     end if;
22 end $$
23 delimiter ;
View Code

事务

  01 什么是事务

    开启一个事务可以包含一些sql语句,这些sql语句要么同时成功,要么一个都别想成功,称之为事务的原子性

  02 事务的作用

   转账等,屏蔽因网络传输部分失效而带来的影响

  03 如何用

 1 create table user(
 2 id int primary key auto_increment,
 3 name char(32),
 4 balance int
 5 );
 6 
 7 insert into user(name,balance)
 8 values
 9 ('wsb',1000),
10 ('egon',1000),
11 ('ysb',1000);
12 
13 #原子操作
14 start transaction;
15 update user set balance=900 where name='wsb'; #买支付100元
16 update user set balance=1010 where name='egon'; #中介拿走10元
17 update user set balance=1090 where name='ysb'; #卖家拿到90元
18 commit;
19 
20 #出现异常,回滚到初始状态
21 start transaction;
22 update user set balance=900 where name='wsb'; #买支付100元
23 update user set balance=1010 where name='egon'; #中介拿走10元
24 uppdate user set balance=1090 where name='ysb'; #卖家拿到90元,出现异常没有拿到
25 rollback;
26 commit;
27 mysql> select * from user;
28 +----+------+---------+
29 | id | name | balance |
30 +----+------+---------+
31 |  1 | wsb  |    1000 |
32 |  2 | egon |    1000 |
33 |  3 | ysb  |    1000 |
34 +----+------+---------+
35 3 rows in set (0.00 sec)
View Code

存储过程

  01 存储过程

    存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql

  02 三种开发模型

    1、

        应用程序:只需要开发应用程序的逻辑
    mysql:编写好存储过程,以供应用程序调用

    优点:开发效率,执行效率都高
     缺点:考虑到人为因素、跨部门沟通等问题,会导致扩展性差

    2、
     应用程序:除了开发应用程序的逻辑,还需要编写原生sql
     mysql:

     优点:比方式1,扩展性高(非技术性的)
    缺点:
    1、开发效率,执行效率都不如方式1
    2、编写原生sql太过于复杂,而且需要考虑到sql语句的优化问题


    3、
    应用程序:开发应用程序的逻辑,不需要编写原生sql,基于别人编写好的框架来处理数据,ORM
    mysql:

    优点:不用再编写纯生sql,这意味着开发效率比方式2高,同时兼容方式2扩展性高的好处
    缺点:执行效率连方式2都比不过

   03 创建存储过程

delimiter $$
create procedure p1(
    in m int,
    in n int,
    out res int
)
begin
    select tname from teacher where tid > m and tid < n;
    set res=0;
end $$
delimiter ;
View Code

   # 如何用存储过程

    #1、直接在mysql中调用
    set @res=10

    call p1(2,4,10);
    #查看结果
    select @res;

    #2 python中使用

# import pymysql
#
# conn=pymysql.connect(
#     host='127.0.0.1',
#     port=3306,
#     user='root',
#     password='123',
#     charset='utf8',
#     database='db42'
# )
#
# cursor=conn.cursor(pymysql.cursors.DictCursor)
#
# cursor.callproc('p1',(2,4,10)) #@_p1_0=2,@_p1_1=4,@_p1_2=10
#
#
# print(cursor.fetchall())
#
# cursor.execute('select @_p1_2;')
# print(cursor.fetchone())
#
# cursor.close()
# conn.close()



# import pymysql
#
# conn=pymysql.connect(
#     host='127.0.0.1',
#     port=3306,
#     user='root',
#     password='123',
#     charset='utf8',
#     database='db44'
# )
#
# cursor=conn.cursor(pymysql.cursors.DictCursor)
#
# cursor.callproc('p6',(100,)) #@_p5_0 = 100
#
# cursor.execute('select @_p6_0')
# print(cursor.fetchone())
#
# cursor.close()
# conn.close()
View Code

    #3、事务的使用

View Code

 函数

  1、强调:mysql内置的函数只能在sql语句中使用

   

mysql> select date_format(sub_time,'%Y-%m'),count(id) from blog group by date_format(sub_time,'%Y-%m');

  

流程控制

select
case
when name = 'egon' then
    name
when name = 'alex' then
    concat(name,'_BIGSB')
else
    concat(name,'_SB')
end

from emp;

索引

  01 为什么要用索引

    对于一个应用来说,对数据库的读写比例基本上是10:1,即读多写少

      而且对于写来说极少出现性能问题,大多数性能问题都是慢查询

     提到加速查,就必须用到索引


  02 什么是索引
    索引就相当于书的目录,是mysql中一种专门的数据结构,称为key,
    索引的本质原理就是通过不断地缩小查询范围,来降低io次数从而提升查询性能
    强调:一旦为表创建了索引,以后的查询都会先查索引,再根据索引定位的结果去找数据


  03 索引的影响
    1、在表中有大量数据的前提下,创建索引速度会很慢,
    2、在索引创建完毕后,对表的查询性能会大幅度提升,但是写性能会降低


  04 聚集索引(primary key)
    特点:叶子节点存放的一整条数据

  05 辅助索引(unique,index)
    特点:
    如果是按照这个字段创建的索引,
    那么叶子节点存放的是:{名字:名字所在那条记录的主键的值}
    覆盖索引:只在辅助索引的叶子节点中就已经找到了所有我们想要的数据

    

    select name from user where name='egon';
    select age from user where name='egon';

猜你喜欢

转载自www.cnblogs.com/seanliang/p/9036164.html