设为首页 加入收藏

TOP

HeadFirstSQL:学习笔记2/2(二)
2014-11-23 22:21:06 来源: 作者: 【 】 浏览:22
Tags:HeadFirstSQL: 学习 笔记 2/2
每次loop循环再对内表进行查询。一直以来认为exists比in效率高的说法是不准确的。

如果查询的两个表大小相当,那么用in和exists差别不大。

如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:

例如:表A(小表),表B(大表)

a:

select * from A where cc in (select cc from B) 效率低,用到了A表上cc列的索引;

select * from A where exists(select cc from B where cc=A.cc) 效率高,用到了B表上cc列的索引。

相反的

b:

select * from B where cc in (select cc from A) 效率高,用到了B表上cc列的索引;

select * from B where exists(select cc from A where cc=B.cc) 效率低,用到了A表上cc列的索引。

not in 和not exists如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。所以无论那个表大,用not exists都比not in要快。

in 与 =的区别

select name from student where name in ('zhang','wang','li','zhao');

select name from student where name='zhang' or name='li' or name='wang' or name='zhao'

的结果是相同的。

第十章 外联接、内联接与联合

1.左外联接:匹配左表中的每一行及右表中符合要求的行。

Select g.girl,b.boy from girls as g left out join toys as t on g.toy_id = t.toy_id;

2.外联接与内联接的区别是:外联接一定能够返回结果集数据行(找不到相符合的返回NULL),结果行数等于右表行数。

3.右外联接:right out join,结果行数等于左表函数。

4.自引用外键:self-referencing foreign key,出于其他目的而引用同一张表的主键。

5.自联接:适用于含有子引用外键的表,将单一表当成两张具有完全相同信息的表进行查询。使用inner join完成查询。表cc的boss_id自引用外键id。

Select c1.name,c2.name as boss from cc c1 inner join cc c2 on c1.boss_id = c2.id;

6.union:联合,组合查询结果集;

Select profession from A union select profession from B;相同profession只出现一次

Select profession from A union all select profession from B;相同profession出现多次

7.使用union创建新表:任何select都可以创建新表

Create table test as select profession from A union select sex from B;

8.intersect:交集

9.Except:差集

第十一章 约束、视图和事务

1.约束:例如check(限定允许插入某个列的值,coin char(1) check in(‘A’,’B’,’C’)),not null, primary key, foreign key, unique等。

添加约束:alter table mytable add constraint check gender in (‘M’,’F’);

2.视图:虚拟数据表

创建视图:create view webdesign AS select name,sex from table1 nature join table2 where table1.id = table2.id;

查看视图:select * from webdesign;

删除视图:drip view webdesign;

3.事务:transaction完成一组工作的sql,所有步骤必须能够全部完成,否则不完成任何一项任务。

事务过程:start transaction->执行sql语句->commit/rollback

显示创作数据表代码:show create table players;

必须使用支持事务的存储引擎:InnoDB和BDB.

改变存储引擎:alter table yourtable TYPE=InnoDB;

第十二章 安全性

1.设定用户密码:set password for ‘root’@’localhost’ = password(‘aaa’);

2.添加新用户:create user conan identified by ‘conanswp’;

3.授权:grant select on table1 to conan

4.撤销权限:revoke select on table1 from conan

5.创建角色:create role data_entry;

6.授权:grant select,update,insert on table1 to data_entry;

7.使用角色:grant data_entry to conan;

8.删除角色:drop role data_entry;

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇PLSQLDeveloper32bit在64位window.. 下一篇SQLServer不允许保存更改【解决】

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: