找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
例子:
having 可以接聚合函数做比较  
  select dept_name from dept left join emp on dept1=dept2 group by dept_name having count(*)>1;

group by后面可以接order by ,order by后面不能接group by
select sum(incoming)a from dept left join emp on dept1=dept2 group by dept_name order by a desc;

% 可以用到年代
select dept_name from dept left join emp on dept1=dept2 where worktime_start like '197%';
order by 后面可以接多个排序
select * from dept left join emp on dept1=dept2 order by dept1,worktime_start asc;

第五讲

索引
索引的作用?
1,快速读取数据
2,保证数据记录的唯一性
3,实现表与表之间的参照完整性
4,在使用order by ,group by 子句进行检索时,索引可以减少排序和分组的时间
优点:
1,大大加快数据的检索速度
2,创建唯一性索引,保证数据库中的每一行数据的唯一性
3,加速表与表之间的连接
4,在使用分组和排序检索时,可以显著的减少查询的时间
缺点:
1,索引占用物理空间
2,当对表中的数据进行增加,删除和修改的时候,索引也要动态的维护,降低了数据的维护速度


1,普通索引-------创建后不会对字段有任何约束,可以为空,不为空,相同
index ==索引
创建普通索引:index
create index a on aa(id);给aa表中的id字段创建了一个普通索引别名:a
查询索引:
show index from aa; 查询aa表中有哪些索引
删除索引:
alter table aa drop index a; 删除aa表中的索引a

2,唯一索引---------值不能相同,可以为空
创建唯一索引:unique index
create unique index a on aa(id);  
查询索引:
show index from aa; 查询aa表中有哪些索引
删除索引:
alter table aa drop index a; 删除aa表中的索引a

3,主键索引-------不能为空,不能相同(常见)
创建表的时候增加主键:
create table aa(id int(4)primary key);
表已创建OK增加主键:
alter table aa change id id int(4)primary key;
alter table aa add primary key(id);
查询索引:
show index from aa; 查询aa表中有哪些索引
删除索引:
alter table aa drop prmary key; 删除aa表中的索引

普通索引,唯一索引,主键索引的区别?
1,普通唯一索引在表中是可以有多个的存在(但是一般不会这样做)
2,主键索引在表中只能有一个
3,普通没有约束,唯一值不能相同但是可以为空,主键不能为空,不能相同



求出谁没参加考试?

select name from (select * from aa left join cc on id=s_id)a where score is null;
select name from aa where id not in(select id from aa,cc where id=s_id);
select name from aa left join cc on id=s_id where score is null;
select name from cc right join aa on id=s_id where score is null;
select name from aa where id not in(select id from aa,cc where id=s_id);










分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册