手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL总结之SQL语句的应用

[日期:2011-05-17] 来源:51cto  作者:lyp0909 [字体:大 中 小]

内连接:即等值连接,根据两个表的对应列值相等的原则进行连接。连接条件的形式:“主键=外键”,即一个表的主键值与另一个表的外键值相等的原则进行连接。

MySQL> select * from stu,course where stu.course=course.id;

MySQL

外连接:分为左外连接和右外连接。外连接不仅包含满足条件的行,还包括其中某个表中不满足连接条件的行。

mysql> select * from stu left join course  on stu.course=course.id;

MySQL

mysql> select * from stu right join course  on stu.course=course.id;

MySQL

mysql> select * from stu right join course  on stu.course=course.id where stu.gender='m' or course.id=4;

MySQL

mysql> select s1.name as HOME ,s2.name as CUSTOMER from stu as s1 inner join stu as s2 where s1.name  s2.name;

MySQL

子查询:

非相关子查询:外查询的查询结果依赖于子查询的结果。

mysql> select * from stu where course =(select id from course where course='Maths');

MySQL

mysql> select s1.name from (select * from stu where gender='m') as s1 where s1.course=2;

MySQL

mysql> select course from course where id in (select course from stu where gender='f');

MySQL

相关子查询:子查询的执行依赖于外查询。执行从外查询开始,只有外查询把值传给内查询后,内查询才能执行。

mysql> select name from stu where exists (select * from stu where age>20);

//** exists 条件为真,就将外查询的结果输出出来。

PS:EXISTS 的子查询只测试子查询的结果集是否为空,因此在子查询中指定列名是没有意义的,所以在有EXISTS的子查询中,其列名序列通常都用“*”表示。

EXISTS后的子查询中必须加入外查询所使用的表与内查询所用的表之间的连接条件。

MySQL

mysql> select * from stu where exists (select * from course where course.id=stu.course and course.course='Maths');

MySQL

视图:虚表

mysql> create view students as select * from stu;

MySQL

视图可以更新。

mysql> create view test as select name,course from stu where course is not null with check option;

索引:

PS:MySQL将存储数据的文件划分为页面。

为stu创建索引:

mysql> create index stu_age_index on stu(age) using btree;

mysql> show create table stu;

MySQL

事务(transaction):一组操作的组合。

(ACID标准)四种约束: 原子性   作为一个整体,不可分割

一致性   事务必须完成全部操作,结果必须保持一致

隔离性   事务之间不能互相干扰

持久性  可以永久保持

事务隔离级别: 读未提交  Read  uncommitted   |

读提交    Read committed       |

重读      Repeatable read       |

串行化    Serialable          级别依次增高

PS:InnoDB支持事务,MyISAM不支持事务。

mysql> start transaction;   //** 事务开始

……

mysql> savepoint S1;

……

mysql> savepoint S2;

mysql> rollback to savepoint S1;  //** 回滚

mysql> commit;         //** 提交

锁:防止事务之间的有毒性交互的机制,当用户对数据库进行并发访问时,为了确保书屋完整性和数据库一致性,需要使用锁,它是实现数据库并发控制的主要手段。

mysql>lock table stu read;

........

mysql>unlock table stu read;

查看全局变量:

mysql> show global variables like '%auto%';

MySQL

修改全局变量:

mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select @@autocommit;

MySQL

练习:查询年龄大于平均年龄的学生的名字:

mysql> select name from stu where age > (select avg(age) from stu);

MySQL

存储过程(procedure):在数据库中定义子程序,这种程序块称之为存储过程。不同用户和应用程序之间共享,并可实现程序的优化和重用。使用 CALL procedure_name(message)来调用,返回结果集或者标量。

mysql> \d //

mysql> create procedure select_stu()

-> begin

-> select * from stu;

-> end //

Query OK, 0 rows affected (0.08 sec)

mysql> \d ;

mysql> call select_stu();

MySQL

mysql> drop procedure select_stu;

Query OK, 0 rows affected (0.04 sec)

在过程中使用IF嵌套:

mysql> \d //

mysql> create procedure what_is_today()

-> begin

-> if dayofweek(now()) between 2 and 6 then

-> select 'Today is a weekday.' as what_is_today;

-> else select 'Today is weekend!';

-> end if;

-> end //

Query OK, 0 rows affected (0.01 sec)

mysql> \d ;

mysql> call what_is_today();

MySQL

存储函数:存储在数据库中的代码块,可以把值返回到调用程序。定义过程与存储过程相似,返回值只能是标量,不能返回结果集。使用SELECT function_name() 来调用。

PS :存储例程:包括存储过程和存储函数。

触发器(trigger)是一些过程,与表关系密切,用户保护表中的数据。当一个基表被修改(INSERT,UPDATE或者DELETE)时,触发器自动执行,通过触发器可实现多个表间数据的一致性和完整性。

MySQL 支持六种触发器:INSERT,UPDATE,DELETE执行前后。

mysql> create trigger flight_ai

-> after insert on stu

-> for each row

-> insert into log (byuser,note,eventtime)

-> values (current_user(),'Record added:stu',now());

Query OK, 0 rows affected (0.04 sec)

调度事件:定时执行一些语句的功能模块,在时间满足的时候自动执行。

练习:为stu新增一个字段,允许为空。为每一个人定义一个alive状态 ENUM 枚举

mysql> alter table stu add alive enum('Y','N') not null default 'Y';

MySQL

mysql> update stu set alive='N' where name='jim' or name='lucy';

MySQL

linux
相关资讯       MySQL教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款