一定有你不知道的Mysql-进阶篇-必知必会

一定有你不知道的Mysql-进阶篇-必知必会

  1. 为什么要使用存储过程

    答:封装简化操作;保证数据的完整性;提高性能,比单独的SQL语句更快。

    注意:如果在命令行中使用存储过程,一定要使用DELIMITER对其中的;进行更改,如下,DELIMITER //告诉命令行实用程序使用//作为新的语 句结束分隔符,可以看到标志存储过程结束的END定义为END //而不是END;。

    DELIMITER //
    create procedure propricing()
    begin
    	select avg(prod_price) as priceavg
    	from products;
    end //
    DELIMITER;
    call propricing();
    drop propricing if exists;
    
  2. 所有MySQL变量都必须以@开始

    call pro(@pricelow,@pricehigh);
    select @pricelow;
    
  3. 不像多数DBMS,MySQL游标只能用于存储过程(和函数)。

    create producer processor()
    begin
    	--declare 注释
    	declare done boolean default 0;
    	declare o int;
    	--创建游标
    	declare ordernumbers cursor
    	for select order_num from orders;
    	--停止条件的设定
    	declare continue handle for sqlstate '2000' 	set done=1;
    	--打开游标
    	open ordernumbers
    	repeat
    		fetch ordernumbers into o;
    	until done end repeat;
    	--显式关闭游标
    	close ordernumbers
    end;
    
  4. 只有表才支持触发器,视图不支持(临时表也不 支持),并且只支持update,delete,insert操作。

    create trigger neworder after insert on orders
    --每次操作
    for each row
    --new是一个虚拟表
    select new.order_num;
    
  5. 事务处理用来管理INSERT、UPDATE和 DELETE语句。你不能回退SELECT语句。(这样做也没有什么意 义。)你不能回退CREATE或DROP操作。事务处理块中可以使用 这两条语句,但如果你执行回退,它们不会被撤销。

  6. 决不能直接使用root账户,你可以创建一个账户并赋予其权限。

    create user ben identified by 'pass';
    grant select on table1.* to ben;
    show grants for ben;
    set password for ben = Password('123');
    
  7. 为了保证所有数据被写到磁盘(包括索引 数据),可能需要在进行备份前使用FLUSH TABLES语句。 然后再执行mysqldump语句。

  8. Char类型为定长串类型,必须指定长度,而Varchar为变长串。如果在你知道无负值的情况下可以使用Unsigned关键字,可以有效减少存储空间。 MySQL中没有专门存储货币的数据类 型,一般情况下使用DECIMAL(8, 2) 。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章