1,通过命令行方式打开sqlplus
2,执行登录命令
sqlplus scott/scott@192.168.248.129/orcl
3进行sqlplus命令测试

Set time on
Set pagesize 数字
Set linesizes 数字
Set pause on/off

Describe命令  查看表或视图结构 也可以desc

Show 查看相应参数 如:show parameter,show user,show error等

Get命令 把一个sql脚本文件内容放入缓冲区

用系统编辑程序编辑命令
在SQL*PLUS中运行操作系统缺省的文本编辑程序(EDIT)
命令为:edit或者ed

将缓存中的sql语句保存到文件中:SAVE 文件名【具体路径】
查看缓存sql:list

执行文件中的sql语句:
START my.sql
@ my.sql

清空缓冲区:clear buffer
格式化命令:column
column sal format $999,999.00
如:
SQL> select ename, sal from emp
  2
SQL> /

ENAME               SAL
---------- ------------
SMITH           $800.00
ALLEN         $1,600.00
WARD          $1,250.00
JONES         $2,975.00
MARTIN        $1,250.00
BLAKE         $2,850.00
CLARK         $2,450.00

重置为默认值:clear columns;
将屏幕上的内容写入到文件中
spool fileName
结束写入
spool off

3.Oracle查询
1) 查询EMP表,显示部门号为10的所有雇员的NAME 、JOB、SALARY和 DEPTNO,并以岗位降序、工资升序进行排序。
select ename,job,sal,deptno from emp where deptno=10 order by job desc , sal asc

2) 从EMP表中查询所有雇员的NAME和补助(SALARY+COMM),并处理null行。
select ename,(sal+nvl(comm,0)) from emp

3) 统计30号部门的总人数、人均工资、最高工资和最低工资。
select count(*) totle,avg(sal),max(sal),min(sal) from emp where deptno=30

4) 查询各个部门各个工种的平均工资、每个部门的平均工资和所有员工的平均工资。
select deptno,job,avg(sal) from emp group by rollup(deptno,job);

5) 查询各个部门中各个工种的平均工资、每个部门的平均工资、每个工种的平均工资和所有员工的平均工资。
select deptno,job,avg(sal) from emp group by cube(deptno,job);

6) 查询EMP,显示1999年之后参加工作人员的NAME、JOB和HIREDATE,并以工作日期的升序进行排序。
select * from emp where hiredate>'1-JAN-99';

7) 查询名字以“S”开始的所有职工:
select *from emp where ename like 'S%';

8) 让员工姓名右对齐显示
col ename jus left

9) 显示姓名只有5个字母组成的员工信息
select *from emp where ename like '_____'
或者
select *from emp where length(ename)=5

10) 查询在当月倒数第三天入职的员工信息
select *from emp where hiredate = last_day(hiredate)-2

2) 对各表中的数据进行不同条件的查询;
a)查询全体学生的学号和姓名
select sno,sname from student;

b)查询全体学生的详细记录
select *from student;

c)查询所有选修过课程的学生学号
select distinct sno from sc;
或者
select sno from student where sno in (select sno from sc);

d)查询考试有不及格的学生学号
select sno from sc where nvl(grade,0) <60;

e)查询不是信息系(IS)、计算机系(CS)的学生性别、年龄、系别
select ssex,sage,sdept from student where sdept not in('is','cs');

f)查询选修了4号课的学生学号和成绩,结果按成绩降序排列
select sc1.sno,sc1.grade from sc sc1,sc sc2 where sc2.cno=4 and sc1.sno=sc2.sno;

g)查询每个课程号和相应的选课人数
select cno,count(sno) from sc group by cno;

h)查询计算机系(CS)的学生姓名、年龄、系别
select sname,sage,sdept from student where sdept='cs'

i)查询年龄18-20岁的学生学号、姓名、系别、年龄;
select sno,sname,sage,sdept from student where sage between 18 and 20

j)查询姓刘的学生情况
select *from student where sname like '刘%'

k)查询既选修1号课程,又选修2号课程的学生学号和成绩
select sno,grade from sc where cno = 1 and cno in(select sno from sc where cno=2);

l)查询学生的姓名和出生年份(今年2003年)
select sname,2017-sage born from student;

m)查询没有成绩的学生学号和课程号
select s.sno,c.cno from student s,course c where (s.sno,c.cno) not in(select sno,cno from sc);

n)查询总成绩大于200分的学生学号
select sno from sc group by sno having sum(grade)>200;

o)查询每门课程不及格学生人数
select cno ,count(sno) from sc where nvl(grade,0)<60 group by cno【不及格分组】

p)查询不及格课程超过3门的学生学号
select sno from (select sno,cno from sc where nvl(grade,0)<60) r group by sno having count(r.cno)>3

q)查询年龄在10到19岁之间的学生信息
select *from student where sage>=10 and sage<=19;
select *from student where sage between 10 and 19;【between and 包含两头】

r)查询全体学生情况,按所在系升序排列,同一个系的学生按年龄降序排列
select *from student order by sdept asc,sage desc;

s)查询选了1号课程的学生平均成绩
select cno, avg(grade) avgGrade from sc where cno=1 group by cno;

t)查询选了3号课程的学生的最高分
select max(grade) from sc where cno=3;

u)查询每个同学的总成绩
select sno, count(grade) from sc group by sno;

复杂查询
内容和主要步骤:
1)实验一中的数据为基础

2) 对各表中的数据进行不同条件的连接查询和嵌套查询;

(1)?查询每个学生及其选课情况;
select * from sc;

(2)?查询每门课的间接先修课
select a.cno,b.cpno from course a,course b where a.cpno=b.cno;

(3)?将STUDENT,SC进行右连接
select *from student right join sc on student.sno=sc.sno;

(4)?查询有不及格的学生姓名和所在系
select sname,sdept from student where sno in (select distinct sno from sc where nvl(grade,0)<60);

(5)?查询所有成绩为优秀(大于90分)的学生姓名
select sname from student where sno in (select distinct sno from sc where nvl(grade,0)>90);

(6)?查询既选修了2号课程又选修了3号课程的学生姓名、学号;
 select sname,sno from student where sno  in (select sno from sc where cno = 3 and cno in(select sno from sc where cno=2));

(7)?查询和刘晨同一年龄的学生
select *from student where sname<>'刘晨' and sage in (select sage from student where sname='刘晨');

(8)?选修了课程名为“数据库”的学生姓名和年龄
select sname,sage from student  where sno in(select sno from sc where cno in(select cno from course where cname='数据库'))

(9)?查询其他系比IS系任一学生年龄小的学生名单
select sname from student where sdept<>'is' and sage < (select max(sage) from student where sdept='is')

(10)?查询其他系中比IS系所有学生年龄都小的学生名单
select sname from student where sdept<>'is' and sage < (select min(sage) from student where sdept='is')

(11)?查询选修了全部课程的学生姓名
 select sname from student where sno in(select sno from sc group by sno having count(cno) = (select count(cno)from sc))

(12)?查询计算机系学生及其性别是男的学生
select *from student where sdept='cs' and ssex='男'

(13)?查询选修课程1的学生集合和选修2号课程学生集合的差集
select *from sc where cno='1' and sno not in(select sno from sc where cno='2')

(14)?查询李丽同学不学的课程的课程号
select cno from course where cno not in(select cno from sc where sno in (select sno from student where sname='李丽'))

(15)?查询选修了3号课程的学生平均年龄
select avg(sage) from student where sno in(select sno from sc where cno=3)

(16)?求每门课程学生的平均成绩
select cno ,avg(grade) from sc group by cno;

(17)?统计每门课程的学生选修人数(超过3人的才统计)。
要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列
select cno ,count(sno) from sc group by cno having count(sno)>3 order by count(sno) desc,cno asc;

(18)?查询学号比刘晨大,而年龄比他小的学生姓名。
select a.sname from student a,(select sno,sname,sage from student where sname='刘晨') b where a.sno>all b.sno and a.sage<all b.sage;

(19)?求年龄大于女同学平均年龄的男同学姓名和年龄
select sname,sage from student where sage  > (select avg(sage) from student where ssex='女');

(20)?求年龄大于所有女同学年龄的男同学姓名和年龄
select sname,sage from student where sage  >all (select sage from student where ssex='女');

(21)?查询至少选修了95002选修的全部课程的学生号码
思想:首先查找学号为95002的同学的课程号集合,然后查找sc中所有学号在该集合中的选课信息,【此时该选课信息均为95002选过的课程信息】记为 msg
然后从msg中以学号进行分组,然后选出cno个数和95002选课个数相同的学号,该学号就是至少选修了95002选修的全部课程的学生学号
select sno from sc where sno<>95002
and
cno in (select cno from sc where sno=95002)
group by sno
having
count(cno)=(select count(cno) from sc where sno=95002)

(22)?查询95001和95002两个学生都选修的课程的信息
select cno from sc where sno =95001 and cno in (select cno from sc where sno=95002);

更新查询题目:
1)    应用INSERT,UPDATE,DELETE语句进行更新操作;
a)    插入如下学生记录(学号:95030,姓名:李莉,年龄:18)
insert into student (sno,sname,sage) values (95030,'李莉',18)

b)    插入如下选课记录(95030,1)
insert into sc (sno,cno) values(95030,1)

c)    计算机系学生年龄改成20
update student set sage=20  where sdept='cs';

d)    数学系所有学生成绩改成0
update sc set grade = 0
where sno in (select sno from student where sdept='ma')

e)    把低于总平均成绩的女同学成绩提高5分
update sc set grade = grade+5 where sno in (select sno from student where ssex='女' and grade < (select avg(grade) from sc))

f)    修改2号课程的成绩,若成绩小于75分提高5%,成绩大于75时提高%1
update sc set  grade = grade+grade*0.05 where grade<75
update sc set grade=grade+grade*0.01 where grade>75

4)(两个语句实现,注意顺序)
g)    删除95030学生信息
delete student where sno=95030

h)    删除SC表中无成绩的记录
delete sc where grade is null;

i)    删除张娜的选课记录
delete sc where sno in (select sno from student where sname='张娜')

j)    删除数学系所有学生选课记录
delete sc where sno in (select sno from student where sdept='ma')

k)    删除不及格的学生选课记录
delete sc where grade <60

l)    查询每一门课程成绩都大于等于80分的学生学号、姓名和性别,把值送往另一个已经存在的基本表STU(SNO,SNAME,SSEX)中
create table stu as (select sno,sname,ssex from student where sno in(select sno from sc where sno not in(select distinct sno from sc where grade<80)))

m)    把所有学生学号和课程号连接追加到新表中
create table newTable as select * from student,sc;

n)    所有学生年龄增1
update student set sage=sage+1;

o)    统计3门以上课程不及格的学生把相应的学生姓名、系别追加到另外一个表中
create table failT as select sname,sdept from student where sno in(select sno from sc where grade<60 group by sno having count(cno)>=3)

oracle-sql入门练习及答案的更多相关文章

  1. 另一套Oracle SQL练习题,更新参考答案

    题干: create table student( sno ) primary key, sname ), sage ), ssex ) ); create table teacher( tno ) ...

  2. 7.25实习培训日志-Oracle SQL(一)

    Oracle SQL(一) 重点 尽量避免select *,影响性能,不直观. 慎用Distinct,会排序,影响性能,用exists 排序尽量利用索引,索引有序 索引列不要加函数,会使索引失效 外连 ...

  3. Oracle PL/SQL入门之慨述

    Oracle PL/SQL入门之慨述 一.PL/SQL出现的目的 结构化查询语言(Structured Query Language,简称SQL)是用来访问关系型数据库一种通用语言,它属于第四代语言( ...

  4. Oracle编程入门经典 第12章 事务处理和并发控制

    目录 12.1          什么是事务处理... 1 12.2          事务处理控制语句... 1 12.2.1       COMMIT处理... 2 12.2.2       RO ...

  5. [转]oracle学习入门系列之五内存结构、数据库结构、进程

    原文地址:http://www.2cto.com/database/201505/399285.html 1 Oracle数据库结构 关于这个话题,网上一搜绝对一大把,更别提书籍上出现的了,还有很多大 ...

  6. oracle数据库入门

    oracle  数据库入门. 1.数据 2.数据存储的地方:变量 数组 容器 (内存中),文件,数据库(文件) 3.数据库系统:sqlserver 2000   2005  2008  mysql 5 ...

  7. Oracle从入门到精通----学习笔记

    书名:<Oracle从入门到精通:视频实战版>秦靖.刘存勇等编著 第4章 SQL基础 1.SQL语言分类 数据定义语言 --- DDL,Data Definition Language 数 ...

  8. 第87节:Java中的Bootstrap基础与SQL入门

    第87节:Java中的Bootstrap基础与SQL入门 前言复习 什么是JQ? : write less do more 写更少的代码,做更多的事 找出所有兄弟: $("div" ...

  9. oracle sql 优化大全

    转自: http://panshaobinsb.iteye.com/blog/1718233 http://yulimeander.blog.sohu.com/115850824.html 最近遇到了 ...

  10. Oracle从入门到精通 限定查询和排序查询的问题

    视频课程:李兴华 Oracle从入门到精通视频课程 学习者:阳光罗诺 视频来源:51CTO学院 知识点 SQL语句的执行顺序 限定符号的使用.   具体内容: 如果想要对所选择的数据进行控制,就可以使 ...

随机推荐

  1. File字节流

    1.    File f = new File("文件路径")      注意:相对路径:非web项目的相对都是以项目为起点.(src/a/txt(建议)      绝对路径:f: ...

  2. orcle :Could not initialize "D:\app\Administrator\product\11.2.0\dbhome_1\bin\oci.dll" Make sure you have the 32 bits Oracle Client installed.

    服务器重启后,数据库登录信息为空 错误信息: ---------------------------(Not logged on) - PL/SQL Developer---------------- ...

  3. APUE-文件和目录(七)符号链接

    符号链接 符号链接的用途 符号链接是对一个文件的间接指针,它与前面介绍的硬连接不同,硬连接指向文件的i节点.引入符号链接是为了避开硬连接的一些限制: 硬链接通常要求链接和文件位于同一文件系统中. 只有 ...

  4. ECSHOP购物车页面显示商品简单描述

    1.这里说的商品简单描述,不是商品的详细信息,而是后台编辑商品时在“其他信息”标签栏填写的那个“商品简单描述”,即goods_brief字段 2.修改lib_order.php文件的get_cart_ ...

  5. jenkins管理员密码登录不了

    1.密码管理员密码,如何修改 进入/var/jenkins_home/users/admin目录下修改config.xml文件: 以下密码是admin <hudson.security.Huds ...

  6. 用nrm一键切换npm源

    当使用官方npm源安装各种包比较慢的时候,建议修改npm源地址 查看npm源地址,在终端输入以下命令 npm config list 会看到官方的npm源 metrics-registry = &qu ...

  7. jquery 表格行计算

    表格行计算 总金额: function jisuanTrJinE(obj){ var curTab = $(obj).closest("table"); var curTr = $ ...

  8. js倒计时函数和(js禁用和恢复a标签的操作)

    <script type="text/javascript"> /*获取手机号*/ var start_time=60; function get_phone_nums ...

  9. 如何使用VBS脚本给在直播间授权登陆

    直接上代码,看不懂说明你技术不够 set WshShell=WScript.CreateObject("WScript.Shell") Dim ie Set mouse=New S ...

  10. 排序算法总结及Java实现

    1. 整体介绍 分类 排序大的分类可以分为两种,内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.主要需要理解的都是内排序算法: 内排序可以分为 ...