加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL:数据库入门篇4

发布时间:2020-12-11 23:56:49 所属栏目:MySql教程 来源:网络整理
导读:1. 视图 创建视图 create view 视图名字 as 查询sql语句; drop view 视图名字; alter view 视图名字 as 查询sql语句; 2. 触发器 1. 插入事件触发器 INSERT INTO order_table(gid,much) VALUES(1,3); -- update goods set num = num -3 where id =1; CREATE T

1. 视图 创建视图 create view 视图名字 as 查询sql语句; drop view 视图名字; alter view 视图名字 as 查询sql语句; 2. 触发器 1. 插入事件触发器 INSERT INTO order_table(gid,much) VALUES(1,3);

-- update goods set num = num -3 where id =1;

CREATE TRIGGER tg1 AFTER INSERT on order_table for EACH row -- 固定写法 BEGIN update goods set num = num -new.much where id =new.gid; END

-- 删除触发器 drop TRIGGER TG1; 2.更新事件触发器 update order_table set much = much +2 where oid = 6;

update goods set num = num+2 where id = 1;

create TRIGGER tg2 AFTER UPDATE ON order_table for EACH ROW BEGIN update goods set num = num+old.much - new.much where id = old.gid; END 3.删除事件触发器 DELETE FROM order_table where oid =6;

update goods set num = num + 3 where id = 1;

create TRIGGER tg3 AFTER DELETE on order_table for EACH ROW BEGIN update goods set num = num + old.much where id = old.gid; END 4.查看触发器 show tiggers;

3.存储过程 1.封装 -- CREATE PROCEDURE p1() -- BEGIN -- INSERT into goods VALUES (null,'韩涉',50); -- select * from goods; -- END --

call p1(); 2.参数 -- in out inout CREATE PROCEDURE p3(in i int,inout names varchar(50)) BEGIN update goods set name = names where id = i; END

set @names = '大鹅';

call p2(4,@names);

select @names; into 使用 set @i = 0; set @n = ''; select num into @i from goods where id = 1;

select @i; 3.判断 CREATE PROCEDURE p3(in flag char(5),in nums int) BEGIN if flag = 'true' then SELECT * from goods where num < nums; ELSEIF flag ='false' THEN SELECT * FROM goods where num > nums; ELSE SELECT * FROM goods; END if;

END call p3('false',20); 4.循环 -- 做 1-100 累加的和 create PROCEDURE p4(in n int,out he int) BEGIN declare i int DEFAULT 0; DECLARE sum int; set sum = 0; while i <= n DO set sum = sum +i; set i = i + 1;

end WHILE; set he = sum; end

set @he = 0;

call p4(100,@he);

select @he; 查看存储过程 show PROCEDURE status; 删除存储过程 drop PROCEDURE p1;

4.函数 create FUNCTION f1(x int,y int) RETURNS INT

BEGIN declare sum int DEFAULT 0; set sum = x +y; RETURN(sum); END

select f1(100,2);

select g.*,f1(100,num) FROM goods g;

DROP FUNCTION f1; 5.事物 什么是事物 一组sql语句批量执行,要么全部执行成功,要么全部执行失败 事物的四个特点: 原子性:对于其数据修改,要么全都执行,要么全都不执行。 一致性:数据库原来有什么样的约束,事务执行之后还需要存在这样的约束,所有规则都必须应用于事务的修改,以保持所有数据的完整性。 隔离性:一个事务不能知道另外一个事务的执行情况(中间状态).所以同一个数据在有多个事物访问时,其中一个得到数据访问修改,其他的事物处于阻塞状态,直到数据被释放,轮到下一个使用。 持久性:即使出现致命的系统故障也将一直保持。不要告诉我系统说commit(提交)成功了,回头电话告诉我,服务器机房断电了,我的事务涉及到的数据修改可能没有进入数据库。 start TRANSACTION; -- 开启事物,关闭mysql自己的自动提交方式 SAVEPOINT sa1;

update account set money = money -1000 where id = 4;

SAVEPOINT sa1;

update account set money = money +1000 where id = 3;

-- COMMIT; -- 提交当前事物 select * from account;

ROLLBACK to sa1;-- 回滚当前事物 6.锁 当并发事务同时访问一个资源时,有可能导致数据不一致,因此需要一种机制来将数据访问顺序化,以保证数据库数据的一致性。 7.数据库的备份 备份: mysqldump -uroot -p123456 数据库名 表 > 保存位置. 导入: mysql> USE 数据库名; mysql> source 备份文件.sql;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读