oracle数据库查询语句



一:

违反完整约束条件- 未找到父项关键字 的原因:
从表关联的外键 必须和 主表被关联的外键(他本身是主键) 数据类型和长度必须一致
插入的数值在 主表的记录中必须存在,比如你插入‘s1’,‘s1’必须在主表中存在。

在一个sql查询中同时使用where子句,group by子句和having子句时,其顺序是where,group by,having。where和having子句的根本区别在于作用对象不同。where子句作用于基本表或者视图,从中选择满足条件的元组;having子句作用于组,选择满足条件的组,必须用于group by子句之后,但group by子句可以没有having子句。

count(*)用来统计元组的个数,不消除重复行,不允许使用distinct关键字

二:

索引的作用:加快查找速度和保证行的唯一性。
聚集索引和非聚集索引,聚集索引需要排序,构建满;非聚集索引构建快;聚集索引比非聚集索引速度快。
唯一索引表示表中每一个索引值只对应唯一的数据记录,这与primary key的特性类似,因此唯一索引常用于primary key字段上,以区别每一个记录。
当表中有被设置为unique的字段时,sql server会自动建立一个非聚集的唯一索引。
而当表中有primary key字段时,sql server会在primary key字段建立一个聚集索引。
unique表示建立唯一索引, clustered表示建立聚集索引。

create unique index SC_i on sc(cno, sno);

--查询结果去掉重复行
select distinct sno from sc;

--将sn显示结果改成name
select sn as name ,sno, age from s;
select sn name ,sno, age from s;

三:

通配符:
% 代表零个或多个字符
_ 代表一个字符
[] 代表在某一个范围的字符
[^] 代表不在某一个范围的字符


--查询选修c1或c2且分数大于等于85分学生的学号课程号和成绩
select sno, cno, score from sc where ( cno='c1' or cno='c2') and(score >= 85);

--查询工资在1000至1500元之间的教师的教师号,姓名和职称
select tno, tn, prof from t where sal between 1000 and 1500

--查询工资在1000至1500元之间的教师的教师号,姓名和职称
select tno, tn, prof from t where sal>=1000 and sal<=1500

--查询工资不在1000至1500元之间的教师的教师号,姓名和职称
select tno, tn, prof from t where sal not between 1000 and 1500

--查询选修c1或者c2的学生的学号,课程号和成绩
select sno, cno, score from sc where cno in('c1', 'c2')

--查询选修c1或者c2的学生的学号,课程号和成绩
select sno, cno, score from sc where cno='c1' or cno='c2'

--查询没有选修c1,也没有选修c2的学生的学号,课程号和成绩
select sno, cno, score from sc where sno not in(select sno from sc where cno in('c1', 'c2'))

--查询没有选修c1,也没有选修c2的学生的学号,课程号和成绩
select sno, cno, score from sc where cno not in('c1', 'c2')

--查询所有姓张的教师的教师号和姓名
select tno, tn from t where tn like '张%'

--查询姓名中第二个汉字是‘力’的教师号和姓名
select tno, tn from t where tn like '_力%'

--查询没有考试成绩的学生的学号和相应的课程号
select sno, cno from sc where score is null

--求学号为是s1学生的总分和平均分
select sum(score) as totalScore, avg(score) as aveScore from sc where (sno = 's1')

--求选修c1号课程的最高分,最低分及之间相差的分数
select max(score) as maxScore, min(score) as minScore, max(score)-min(score) as diff from sc where (cno = 'c1')

--求学校中共有多少系
select count(distinct dept) as deptNum from s

--*统计有成绩同学的人数
select count(score) from sc group by sno

--利用特殊函数count(*)求计算机系学生的总数
select count(*) from s where dept='计算机'

--利用特殊函数count(*)求计算机系女学生的总数
select count(*) from s where dept='计算机' and sex='女'

--查询各个教师的教师号及其任课的门数
select tno, count(*) as c_num from tc group by tno

--查询选修两门以上课程的学生的学号和选课门数
select sno, count(*) as sc_num from sc group by sno having(count(*)>2)

--查询选修c1的学生学号和成绩,并按成绩降序排列
select sno, score from sc where (cno='c1') order by score desc

--查询选修c1的学生学号和成绩,并按成绩降序排列
select sno, score from sc where (cno='c1') order by score

--查询选修c2,c3,c4或c5课程的学号,课程号和成绩,查询结果按学号升序排列,学号相同再按成绩降序排列
select sno, cno, score from sc where(cno in('c2', 'c3', 'c4', 'c5')) order by sno, score desc

--*求有三门以上选课成绩及格的学生的学号及其总成绩,查询结果按总成绩降序列出
select sno, sum(score) as totalScore from sc where (score>=60) group by sno having (count(*) >=3) order by sum(score) desc

--查询刘伟老师所讲授的课程,要求列出教师号,教师姓名和课程号
select t.tno, tn, cno from t, tc where (t.tno=tc.tno)and(tn='刘伟')

--查询刘伟老师所讲授的课程,要求列出教师号,教师姓名和课程号
select t.tno, tn ,cno from t inner join tc on t.tno=tc.tno where(tn='刘伟')

--查询所有选课学生的学号,姓名,选课名称和成绩
select s.sno, sn, cn, score from s,c,sc where s.sno=sc.sno and sc.cno=c.cno

--查询每门课程的课程名,任课教师姓名及其职务、选课人数
select cn,tn,prof,count(sc.sno) from c, t, tc, sc where t.tno=tc.tno and c.cno=tc.cno and sc.cno=c.cno
group by sc.cno, c.cn, t.tn, t.prof

--查询所有比‘刘伟’工资高的教师姓名,工资和刘伟的工资
with x as ( select tn, sal from t), y as ( select sal from t where tn='刘伟')
select x.tn, x.sal as sal_a, y.sal as sal_b from x inner join y  on x.sal >= y.sal 

--查询所有比‘刘伟’工资高的教师姓名,工资和刘伟的工资
with x as ( select * from t),  y as ( select * from t)
select x.tn, x.sal, y.sal from x inner join y on x.sal >= y.sal and y.tn='刘伟'



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值