--https://blog.csdn.net/weixin_39718665/article/details/78161013/*1.用系统管理员登陆,我这里用户名是system,密码是manager2.首先建立表空间(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 50mautoextend onnext 50m maxsize 20480mextent management local;*//*--2,创建数据表空间create tablespace "001_TEST_DATA"datafile 'E:\DB\002_sqlscript\001_oracle\001_TEST_DATA.dbf' size 50mautoextend on next 50m maxsize unlimited logging extent management local autoallocatesegment 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_STUDENTunionselect 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='李军')andclass = ( 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='计算机导论'); |