--https://blog.csdn.net/weixin_39718665/article/details/78161013
/*
1.用系统管理员登陆,我这里用户名是system,密码是manager
2.首先建立表空间(tablespaces),点击file->new->sql window
*/
/*
   1) DATAFILE: 表空间数据文件存放路径
   2) SIZE: 起初设置为200M
   3) UNIFORM: 指定区尺寸为128k,如不指定,区尺寸默认为64k
   4) 空间名称MOF_TEMP与 数据文件名称 MOF_TEMP.dbf 不要求相同,可随意命名.
   5) AUTOEXTEND ON/OFF 表示启动/停止自动扩展表空间
   6) alter database datafile ' D:/oracle/product/10.2.0/oradata/orcl/MOF_TEMP.dbf ' resize 500m;

    //手动修改数据文件大小为 500M
*/
--删除表空间
--drop tablespace "001_TEST_TEMP" including contents and datafiles;

/*
--1,创建临时表空间
create temporary tablespace "001_TEST_TEMP" tempfile 'E:\DB\002_sqlscript\001_oracle\001_TEST_TEMP.dbf' size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
*/
/*
--2,创建数据表空间
create tablespace "001_TEST_DATA"
datafile 'E:\DB\002_sqlscript\001_oracle\001_TEST_DATA.dbf' size 50m
autoextend on next 50m maxsize unlimited logging 
extent management local autoallocate
segment space management auto;
*/

/*
-- 3,创建用户并指定表空间
create user TEST_DATA                --创建用户名
identified by "123456"             --创建密码
default tablespace "001_TEST_DATA"      --默认表空间 
temporary tablespace "001_TEST_TEMP";    --临时表空间(默认的)
-- 3,创建用户并指定表空间
create user TEST_DATA identified by "123456" default tablespace "001_TEST_DATA" temporary tablespace "001_TEST_TEMP";    
select * from dba_users where username like 'TEST_DATA';
delete from dba_users where username='TEST_DATA';
*/

/*
--4,赋权
-- Grant/Revoke role privileges 
--分配管理员权限 
grant dba to TEST_DATA;
-- Grant/Revoke system privileges 
grant unlimited tablespace to TEST_DATA; --开放所有的表空间对此用户
*/

/*
--5,创建表
CREATE TABLE T_001_STUDENT
(
  sno       VARCHAR(20) NOT NULL PRIMARY KEY,
  sname     VARCHAR(20) NOT NULL,
  ssex      VARCHAR(20) NOT NULL,
  sbirthday timestamp  NULL,
  class     VARCHAR(20) NULL
);
*/

/*
如果有管理员权限的用户,可以执行:
select * from dba_tables;这里可以看到此数据库下的所有表
如果没有管理员权限的用户,则执行:
select * from tabs;或者select * from user_tables; 只能查询此用户有权限的表,不一定是此用户自己创建的表
*/
--select * from tabs;
--select * from user_tables;

/* 查询教师所有的单位即不重复的Depart列*/
select distinct depart from T_001_TEACHER;
select * from T_001_STUDENT;
select * from T_001_SCORE where degreee between 60 and 80;
select * from T_001_SCORE where degreee in (85,86,88);
select * from T_001_STUDENT where class='95031' or ssex='女';
/*以Class降序查询Student表的所有记录*/
select * from T_001_STUDENT order by class desc;
selecct * from T_001_SCORE order by cno asc,degreee desc;
select count(*) from T_001_STUDENT where class='95031';
/*嵌套查询 */
/*查询Score表中的最高分的学生学号和课程号。(子查询或者排序)*/
select sno,cno from T_001_SCORE where degreee > (
       select degreee from T_001_SCORE--无逗号结尾
);
select sno,cno from T_001_SCORE where degreee > (
       select max(degree) from T_001_SCORE
);
select sno,con from T_001_SCORE order by degreee desc limit 0,1;
/*查询每门课的平均成绩*/
select cno,avg(degreee) as '每门课的平均成绩'  from T_001_SCORE group by cno;
/*查询Score表中至少有5名学生选修的并以3开头的课程的平均分数*/
select cno,avg(degreee) from T_001_SCORE where cno like '3%' 
       group by cno having count(sno) >= 5;
select from T_001_SCORE where con like '3%' and cno in(
       select con from T_001_SCORE group by con having count(sno) >=5 
);
select sno from T_001_SCORE where degreee > 70 and degreee <90;
/*多表连接和内连接*/
select sname,cno,degreee from T_001_STUDENT a inner join T_001_SCORE b
       on a.sno = b.sno;
select sname,cno,degreee from T_001_STUDENT a,T_001_SCORE b where a.sno = b.sno;
/*查询“95033”班学生的平均分*/
select avg(b.degreee) from T_001_STUDENT a, T_001_SCORE b where a.sno=b.sno and a.class='95033';
select avg(degreee) from T_001_SCORE where sno in(
       select sno from T_001_STUDENT where class='95033'

/*查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录*/
select * from T_001_STUDENT a,T_001_SCORE b T_001_COURSE C where a.sno = b.sno and b.cno = c.cno
       and c.cno = '3-105' and b.degreee > 109;
select * from T_001_STUDENT where sno in (
       select sno from T_001_SCORE where degreee > 109 and cno ='3-105'
);
/*查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录*/
select * from T_001_SCORE where degreee>(
       select degreee from T_001_STUDENT where sno=109 and cno = '3-105'
);
/*查询和学号为108、101的同学同年出生的所有学生的Sno、Sname和Sbirthday列*/
select sno,sname,sbirthday from T_001_STUDENT year(sbirthday) = (
       select year(sbirthday) from T_001_STUDENT where sno in(108,101)
);
/*查询“张旭“教师任课的学生成绩*/
select a.sno,b.degreee from T_001_STUDENT a,T_001_SCORE b where a.sno = b.sno 
       and b.cno = (
           select c.cno from T_001_COURSE c, T_001_TEACHER d where c.tno = d.tno and d.tname = '张旭'
       ) ;
select sno,degreee from T_001_SCORE where cno in (
       select cno from T_001_COURSE where tno in(
              select tno from T_001_TEACHER where tname='张旭'
       )
);
/*查询选修某课程的同学人数多于5人的教师姓名*/
select tname from T_001_TEACHER tno in (
       select a.tno from T_001_COURSE a,T_001_SCORE b where a.cno = b.cno 
              group by a.cno having count(sno) >5 
);
select Tname from Teacher where Tno in (
       select Tno from Course where Cno in (
              select Cno from Score group by Cno having count(Sno)>5)
);
/*查询95033班和95031班全体学生的记录----和用or连接条件*/
select * from T_001_STUDENT a,T_001_SCORE b,T_001_COURSE where a.sno = b.sno 
       and b.cno =c.cno 
           and class='95033' or '95031';
/*查询出“计算机系“教师所教课程的成绩表*/
select * from T_001_SCORE where cno in(
       select cno from T_001_COURSE where tno in(
              select tno from T_001_TEACHER where depart='计算机系'
       )
);
/*查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof*/
/*(查询“计算机系”与“电子工程系“相同职称的教师的Tname和Prof)取反*/
select tname,prof from T_001_TEACHER where depart='计算机系' or '电子工程系' and prof not in(
       select prof from T_001_TEACHER where depart='计算机系' or '电子工程系';--既有计算机系的prof也有电子工程系的prof
);--最后not in==既没有计算机系的prof也没有电子工程系的prof。
select Tname,Prof from Teacher where Prof not in (
       select Prof from Teacher where Depart = '电子工程系' and Prof in (
              select Prof from Teacher where Depart = '计算机系'
       )
);
/*查询选修编号为“3-105“课程且成绩至少(重点在于这个至少)高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序*/
select cno,sno,degreee from T_001_SCORE where cno='3-105' and degreee > (
       any(select degreee from T_001_SCORE where cno ='3-245')
) order by degreee desc;
select cno,sno,degreee from T_001_SCORE where cno='3-105' and degreee > (
       min(select degreee from T_001_SCORE where cno ='3-245')
) order by degreee desc;
/*
--any表示有任何一个满足就返回true,相当与大于最小的就可以,all表示全部都满足才返回true,相当于大于最大的就可以了
select * from student where 班级='01' and age > all (select age from student where 班级='02');
就是说,查询出01班中,年龄大于 02班所有人 的 同学
相当于
select * from student where 班级='01' and age > (select max(age) from student where 班级='02');

select * from student where 班级='01' and age > any (select age from student where 班级='02');
就是说,查询出01班中,年龄大于 02班任意一个 的 同学
相当于
select * from student where 班级='01' and age > (select min(age) from student where 班级='02');
*/
/*查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree*/
select cno,sno,degreee from T_001_SCORE where cno='3-105' and degreee>(
       all(select degreee from T_001_SCORE where cno='3-245')
);
select cno,sno,degreee from T_001_SCORE where cno='3-105' and degreee>(
       max(select degreee from T_001_SCORE where cno='3-245')
);
/*查询所有教师和同学的name、sex和birthday*/
select sname,ssex,sbirthday from T_001_STUDENT
union
select tname,tsex,tbirthday from T_001_TEACHER;
/*查询成绩比该课程平均成绩低的同学的成绩表*/
select sno,cno from T_001_SCORE where degreee < (
       select avg(degreee) from T_001_SCORE group by cno
);
--(父子)自相连接,达到分组效果
select * from score a  where degree < (
       select avg(degree) from score b where b.cno=a.cno
);--关键在于这种sql语句的运算流程不同。
/*
这是一种特殊形式的父子表连接(自连接)SQL选择查询写法。对于这种特殊的写法,数据库引擎会以特殊的方式检索父查询表里的数据。如果搞不清楚这种特殊的检索方式,我们很难从该SQL语句的表面逻辑理出个中道理。 
现在我们来分拆该SQL语句里的父查询和子查询 
1)语句中的父查询 
select * from score a where degree<”子查询获得的一个数据值“ 
2)语句中的子查询 
select avg(degree) from score b where a.cno=b.cno 
请注意这个子查询的from子句里只有一张表 b ,但是where子句里却出现了第二张表 a , 
如果单独运行这个子查询,因为子查询没有列出表a,系统会要求输入a.cno或者直接报错,反正无法顺利执行,但是表a可以在父查询里的from子句中找到,面对这种情况数据库引擎会采取逐条取主查询记录与子查询实施比对以确定是否检出该条记录,最后汇总各次检索的结果输出整个记录集。 
这个特殊的SQL语句检索过程大致如下: 
取出首条记录的a.cno用作过滤,子查询里以avg函数得到该课程的平均分,主查询以分数比对平均分,满足条件保留否则抛弃(degree小于平均分的留下); 
跟着判断父查询表下一条记录,处理过程相同,最后合并各次判断结果从而的到最终结果。 
这种特殊的写法可以规避输出包含非分组字段,而分组不得输出非分组字段的矛盾。
*/

/*查询所有未讲课的教师的Tname和Depart*/
select tname,depart from T_001_TEACHER where tno not in (
       select tno from T_001_COURSE where cno in(
              select cno from T_001_SCORE ;
       )
);
/*查询至少有2名(包括2名)男生的班号*/
select from T_001_STUDENT where ssex='男' group by class having count(sno) > 1;

/*查询Student表中不姓“王”的同学记录*/
select * from T_001_STUDENT where not like '王%';
/*查询Student表中每个学生的姓名和年龄*/
select sname,year(now())-year(sbirthday) as '年龄' from T_001_STUDENT;
/*查询Student表中最大和最小的Sbirthday日期值*/
select max(sbirthday),min(sbirthday) from T_001_STUDENT ;
/*查询“男”教师及其所上的课程*/
select tno,cno,cname from T_001_COURSE where tno in(
       select tno from T_001_TEACHER where tsex='男'
);
select Cno,Cname,Tname from Course,Teacher where Course.Tno=Teacher.Tno and Tsex='男';
select Cno,Cname,Tname from Course inner join Teacher on Course.Tno=Teacher.Tno where Tsex='男';
/*查询最高分同学的Sno、Cno和Degree列*/
select sno,cno,degreee from T_001_SCORE order by Degree desc limit 0,1;
select sno,cno,degreee from T_001_SCORE where Degree = (
       select max(degreee) from T_001_SCORE
);
/*查询和“李军”同性别的所有同学的Sname*/
select sname from T_001_STUDENT where ssex = (
       select ssex fromm T_001_STUDENT where sname='李军'
);
/*查询和“李军”同性别并同班的同学Sname*/
select sname from T_001_STUDENT where ssex = (
       select ssex fromm T_001_STUDENT where sname='李军'
)and
class = (
      select class fromm T_001_STUDENT where sname='李军'
);
/*查询所有选修“计算机导论”课程的“男”同学的成绩表*/
select sno,degreee from T_001_SCORE where sno  in(
       select sno from t_001_Student where ssex='男'
)and cno =(
     select cno from T_001_COURSE where cname='计算机导论'
);

sql重点题的更多相关文章

  1. Java 整体测试重点题 错题积累

    重点题    错题积累 1: 解析: %d:用来设置输出日志的日期和时间 %m:用来输出代码中指定的消息 %n:用来输出一个回车换行符 %l:用来输出日志事件的发生位置 %p:用来输出优先级 %f:用 ...

  2. web metrics dashboard 数据分析工具 看板 从可视化发现问题 避免sql重复写 调高效率

    <?php$todo = array();$done = array();$h = array();$v = $all['v'];$l = count($v);#19700101 08for ( ...

  3. for循环 重点题

    1.冒泡排序  (特别重要): <script type="text/javascript"> var attr=Array(); for(var i=0; i< ...

  4. Spark SQL数据加载和保存实战

    一:前置知识详解: Spark SQL重要是操作DataFrame,DataFrame本身提供了save和load的操作, Load:可以创建DataFrame, Save:把DataFrame中的数 ...

  5. 【数据库(一)】SQL语言-表定义、查询

    基本模式定义+ SQL支持许多不同的完整性约束. not null, 在该属性上不允许空值 primary key 是否是是主码,主码必须非空且唯一 foreign key check(P),P是谓词 ...

  6. 阿里数据库性能诊断的利器——SQL执行干预

    概述 在业务数据库性能问题诊断中,如果发现一个业务性能很差跟某个SQL有关,应用连接池几乎被该SQL占满,同时数据库服务器上也不堪重负.此时情况很紧急,业务改SQL重发布已经来不及了,运维能选择的操作 ...

  7. Spark SQL数据载入和保存实战

    一:前置知识具体解释: Spark SQL重要是操作DataFrame,DataFrame本身提供了save和load的操作. Load:能够创建DataFrame. Save:把DataFrame中 ...

  8. SQLServer常用运维SQL整理

    今天线上SQLServer数据库的CPU被打爆了,紧急情况下,分析了数据库阻塞.连接分布.最耗CPU的TOP10 SQL.查询SQL并行度配置.查询SQL 重编译的原因等等 整理了一些常用的SQL 1 ...

  9. windows系统与SQL SERVER 2008数据库服务性能监控分析简要

    软件系统性能测试体系流程介绍之windows系统与SQL SERVER 2008数据库服务性能监控分析简要 目前大部分测试人员对操作系统资源.中间件.数据库等性能监控分析都是各自分析各自的监控指标方式 ...

随机推荐

  1. highcharts之柱状图

    <div class="row"> <div class="col-md-12"> <div id="container ...

  2. Codeforces 1107G Vasya and Maximum Profit [单调栈]

    洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...

  3. hadoop环境配置

    0. Hadoop源码包下载 http://mirror.bit.edu.cn/apache/hadoop/common/   1. 集群环境 Master 172.16.11.97 Slave1 1 ...

  4. easyui的combobox,自动搜索的下拉框

    作者:多来哈米 如图,输入关键字,左匹配检索 HTML代码 <input class="easyui-combobox" name="userId" id ...

  5. Confluence 6 自定义站点和空间布局

    你可以通过编辑布局文件来修改 Confluence 的外观和感觉(也可以被称为装饰).编辑这些文件将会允许你对整个 Confluence 站点的外观和感觉进行修改或者仅仅是一个独立的空间. 当你对一个 ...

  6. 利用map和stringstream数据流解题

    题目描述 喜闻乐见A+B.读入两个用英文表示的A和B,计算它们的和并输出. 输入 第一行输入一个字符串,表示数字A:第二行输入一个字符串表示数字B.A和B均为正整数. 输出 输出一个正整数n,表示A+ ...

  7. C#概念总结(一)

    1.C#程序的框架问题 首先是命名的空间申明   (NameSpace delclaration) 一个 ClASS class 方法 class属性 一个main 的方法 语句(Statement) ...

  8. Spring.Net 简单实例-01(IOC)

    1.话不多说看操作.新建"Windows窗体应用程序" 2:通过配置文件创建IOC容器 首先引入安装包 3:定义一个接口(更好的体现封装性,当然也可以直接使用类) 定义一个类,实现 ...

  9. Java接口自动化测试之TestNG测试报告ExtentReports的应用(三)

    pom.xml导入包 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  10. map映射巧用 A-B Problems

    A-B problem Description 大家都非常熟悉 A+B Problem! 题目看多了也有审美疲劳,于是我舍弃了,改用 A-B problem! 题目是这样的:给出一串数以及一个数字 C ...