一、mysql体系结构

mysql如何存储树状结构 mysql存储结构体_mysql如何存储树状结构

  • Connectors:用来与客户端应用程序建立连接的数据库接口
  • Management Services & Utilities:系统管理和服务控制相关的辅助工具
  • Connection Pool:负责处理与用户访问有关的各种用户登录、线程处理、内存和进程缓存需求
  • Sql Interface:提供从用户接受命令并把结果返回给用户的机制
  • Parser:对SQL语句进行语法分析和解析,构造一个用来执行查询的数据结构
  • Optimizer:优化查询语句,以保证数据检索动作的效率达到或者非常接近最最优。使用一种“选取-投影-联结”策略来处理查询,即先根据有关的限制条件进行选取(Select 操作)以减少将要处理的元组个数,再进行投影以减少被选取元组列的属性字段的个数,最后根据连接条件生产最终的查询结果
  • Caches & Buffers:保证使用频率最高的数据或结构能够以最有效率的方式被访问,缓存的类型有:表缓存、记录缓存、键缓存、权限缓存、主机名缓存等。

二、mysql的存储引擎

1、关系数据库中数据的存储是以表的形式存储的,所以存储引擎简而言之就是指表的类型

2、使用MySQL命令“show engines;”,即可查看MySQL服务实例支持的存储引擎

3、

InnoDB存储引擎

InnoDB存储引擎的特点:

  • 支持外键(Foreign Key)
  • 支持事务(Transaction):如果某张表主要提供OLTP支持,需要执行大量的增、删、改操作(insert、delete、update语句),出于事务安全方面的考虑,InnoDB存储引擎是更好的选择。
  • 最新版本的MySQL已经开始支持全文检索。

MyISAM存储引擎

MyISAM存储引擎的特点:

  • MyISAM具有检查和修复表的大多数工具。
  • MyISAM表可以被压缩
  • MyISAM表最早支持全文索引
  • 但MyISAM表不支持事务
  • 但MyISAM表不支持外键(Foreign Key)。
  • 如果需要执行大量的select语句,出于性能方面的考虑,MyISAM存储引擎是更好的选择。

MySQL默认的默认的存储引擎是InnoDB。

三、SQL语句的种类

  • DDL (数据定义语句)操作的是表结构(create,alter,drop ,truncate)
  • 数据定义语言- Data Definition Language
  • 用来定义数据库的对象,如数据表、视图、索引等
  • DML (数据操纵语句)操作数据(update,insert,delete)
  • 数据处理语言- Data Manipulation Language
  • 在数据库表中更新,增加和删除记录
  • 如update,insert,delete --- 增删改
  • DCL (数据控制语句)
  • 数据控制语言– Data Control Language
  • 指用于设置用户权限和控制事务语句
  • 如grant,revoke,if…else,while,begin transaction
  • DQL (数据查询语句)
  • 数据查询语言– Data Query Language
  • select
  • TCL(事务控制语句)     commit; rollback;

 四、mysql字符集

        在DOS窗口的字符编码格式是GBK,数据库库MySQL安装时编码格式设置的是UTF-8

set names gbk;

        作用:该命令告诉数据库服务器客户端的编码格式是gbk. mysql服务器就会处理编码格式的转换。

五、SQL脚本文件

  • 将常用的SQL语句封装为一个SQL脚本文件,使用source 命令运行该SQL脚本文件中的所有SQL语句(SQL脚本文件的扩展名一般为sql)。
  • 输入MySQL命令:source init.sql即可执行init.sql脚本文件

六、MySQL存储过程

        1. 定义时,封装了若干条语句。

        2. 调用时,这些封装体执行。

没有返回值。

1、创建存储过程的语法:

        create procedure procedurename()
        begin
                    -- sql 语句
        end

//设置分隔符

delimiter $

//创建存储过程

mysql> create procedure p1()
    -> begin
    -> select empno from emp;
    -> end$
Query OK, 0 rows affected (0.00 sec)

//查看已有的过程

show procedure status;

//调用存储过程

mysql> call p1()$
+-------+
| empno |
+-------+
|     1 |
|     2 |
|  7934 |
|  7369 |
|  7566 |

//删除存储过程

drop procedure p1$
Query OK, 0 rows affected (0.01 sec)

2、变量的声明与赋值

语法:declare 变量名 变量类型 [default 默认值]

存储过程是可以编程的。

意味着可以使用变量,表达式,控制结构,来完成复杂的功能

mysql> delimiter $
mysql> create procedure p2()
    -> begin
    ->    declare age int  default 18;
    ->    declare height int default 175;
    ->    select concat('年龄:', age, '身高', height) from dual;
    -> end$
Query OK, 0 rows affected (0.01 sec)

mysql> call p2()$
+---------------------------------------+
| concat('年龄:', age, '身高', height)         |
+---------------------------------------+
| 年龄:18身高175                               |
+---------------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> create procedure p3()
    -> begin
    ->     declare age  int  default 20;
    ->     declare height int default 175;
    ->     set age := age+20;
    ->     select concat('20年后的年龄:' , age, '身高:', height) from dual;
    -> end$
Query OK, 0 rows affected (0.01 sec)

mysql> call p3()$
+-------------------------------------------------+
| concat('20年后的年龄:' , age, '身高:', height)               |
+-------------------------------------------------+
| 20年后的年龄:40身高:175                                      |
+-------------------------------------------------+

3、流程控制

(1)if/else分支结构if/elseif/else

语法:

  if  condition then
            statement;
        [else 
            statemenbt;]
        end if;

mysql> create procedure p4()
    -> begin
    ->    declare age int default 60;
    ->    if age  >= 70 then
    ->        select '古稀之年' from dual;
    ->    else
    ->        select '风华正茂' from dual;
    ->    end if;
    -> end$
Query OK, 0 rows affected (0.00 sec)

mysql> call p4()$
+----------+
| 风华正茂      |
+----------+
| 风华正茂      |
+----------+

(2)case分支结构

        语法一:

  case 变量名
           when val1  then  statement1;
           [when val2  then  statement2;
            ....]
           [else statementn;]
        end case

mysql> create procedure p5(age int)
    -> begin
    ->     case age
    ->     when 12
    ->        then select '小学毕业';
    ->     when 18
    ->        then select '中学毕业' ;
    ->     when 22
    ->        then select '大学毕业';
    ->     else
    ->        select '努力学习中。。。。';
    ->     end case;
    -> end$
Query OK, 0 rows affected (0.00 sec)

mysql> call p5(22)$
+----------+
| 大学毕业       |
+----------+
| 大学毕业       |
+----------+

语法二:

case
                when 条件表达式1  then  statement1;
                [when 条件表达式2 then  statement2, ] ...
                [else statement3]
        end case;

mysql> create procedure p6(age int)
    -> begin
    ->     case
    ->     when age <=12
    ->        then select '小学阶段。。。。。';
    ->     when  age <= 18
    ->        then select '中学阶段.....' ;
    ->     when age <= 22
    ->        then select '大学阶段......';
    ->     else
    ->        select '大学别业';
    ->     end case;
    -> end$
Query OK, 0 rows affected (0.00 sec)

mysql> call p6(21);
    -> $
+----------------+
| 大学阶段......       |
+----------------+
| 大学阶段......       |
+----------------+

(3)while循环结构

例子:前n项和

mysql> create procedure p7(n int)
    -> begin
    -> declare sum int default 0;
    -> declare num int default 0;
    -> while num <= n do
    ->  set sum := sum+num;
    ->  set num := num+1;
    -> end while;
    -> select concat('1----n的和',sum) from dual;
    -> end$
Query OK, 0 rows affected (0.00 sec)

mysql> call p7(100)$
+--------------------------+
| concat('1----n的和',sum)    |
+--------------------------+
| 1----n的和5050              |
+--------------------------+

(4)repeat循环结构

        语法:  until +循环退出的条件表达式

                repeat
                sql  statement;
                until 条件表达式
                end repeat;

mysql> create procedure p8()
    -> begin
    -> declare sum int default 0;
    -> declare num int default 0;
    ->  repeat
    -> set sum := sum+num;
    -> set num := num+1;
    -> until num > 100
    -> end repeat;
    -> select sum;
    ->  end$
Query OK, 0 rows affected (0.00 sec)

mysql> call p8()$
+------+
| sum  |
+------+
| 5050 |
+------+

(5)存储过程中的参数传递

存储过程的括号里,可以申明参数,

语法:[in|out|inout] 参数名   参数类型

IN

mysql> create procedure p9(width int, height int)
    -> begin
    ->      select concat('面积:', width*height) from dual;
    ->      case
    ->         when  width > height
    ->        then select '挺胖';
    ->         when width < height
    ->         then select '挺瘦';
    ->      else
    ->         select '方的';
    ->      end case;
    -> end$


mysql> call p9(60,50)$
+--------------------------------+
| concat('面积:', width*height)       |
+--------------------------------+
| 面积:3000                           |
+--------------------------------+
1 row in set (0.00 sec)

+------+
| 挺胖    |
+------+
| 挺胖    |
+------+
1 row in set (0.01 sec)

OUT

输出型参数out

1. 输出参数格式:out 变量名 变量类型;

2. 调用:  call  存储过程名(@变量名)

3. 查看存储过程的返回值:  select @变量名;

mysql> create procedure p10(n int, out sum int)
    -> begin
          -- 顺序: 1. declare   2. set 
    ->    declare num int default 0;
    ->    set sum := 0;
    ->    while num <=n  do
    ->       set sum := sum+num;
    ->       set num := num+1;
    ->    end while;
    -> end$
Query OK, 0 rows affected (0.00 sec)

--调用
mysql> call p10(5,@summary)$
Query OK, 0 rows affected (0.00 sec)

--查看输出结果
mysql> select @summary$
+----------+
| @summary |
+----------+
|       15 |
+----------+
1 row in set (0.00 sec)

INOUT

求20年后的年龄

mysql> create procedure p11(inout age int)
    -> begin
    ->    set age := age + 20;
    -> end$
Query OK, 0 rows affected (0.00 sec)

--设置初始年龄
mysql> set @age := 15$
Query OK, 0 rows affected (0.00 sec)

--调用方法
mysql> call p11(@age)$
Query OK, 0 rows affected (0.00 sec)

--查询结果
mysql> select @age$
+------+
| @age |
+------+
|   35 |
+------+