Oracle之SQL 和 PL/SQL编程指南

1. 定义表的结构:

sql>

create  table stu (

student_id  number(5)

constrant  student_pk  primary  key,  --定义主键约束

moitor_id  number(5),

name  varchar2(10)  not null,

sex varchar2(6),

constraint  sex_chk  check(sex  in (‘男’,’女’)),

dob  Date,

specialty   varchar2(10)

);

插入数据:

insert into  stu  values('12','5','e’,’男’,’07-11-2009,’mm’);

删除数据:

Delete  from  stu;

2. 简单查询

1. 检索所有教师信息,日期格式按“YYYY-MM-DD”输出:

Select  name,title,wage,TO_CHAR(hire_date,YYYY-MM-DD)  from  teachers;

2. 检索学生表,显示学生专业列(带distinct关键字,消除重复行)

Select distinct  specialty  from  students;

3. 使用别名:

Select  name  as “姓名”,  dob as 生日  from stu;

4. 字符串连接符“||

Select  name || ‘生日是:’ || dob  as “学生清单” from stu;

结果为:

学生清单:

小小生日是:08-3-89

5.使用算术表达式:

  Select  name  AS “姓名” , bonus+wage  AS ”月总收入” 

from teachers

注意bonus+wage 指“奖金+工资”

3. 空值处理:

1. 利用NVL()函数处理bonus出现空值情况:

Select   NVL(bonus,0)  as “奖金” from teachers;

注意:NVL(bonus,0)表示当bonus=null 时,返回0,不为null时,返回bonus

   2.利用NVL2()函数处理bonus出现空值情况:

    Select   NVL2(A,B,C)  from  D;

   :当A=null时,返回C

A=null时,返回B

2. 利用COALESCE()函数处理bonus出现空值情况:

Select   COALESCE (A,B)  from  D;

A!=null 时,返回 A

A=null,B!=null,返回B

A=B=null  返回 null

3.条件查询

 1.使用算术比较符()

检索工资多余或等于2000元的教师信息:

Select name from teacher where wage>=2000;

检索计算机专业学生信息:

Select * from students where specialty=’计算机’;

检索199011号以前出生学生信息:

Select  * from students where dob<’1-1-1990’;

2.使用包含测试:

检索teachers表中获得奖金为500600元的老师信息:

Select  * from teachers where bonus in (500,600);

检索students表中在19891226日及1990810日出生的学生信息:

Select * from where dob in(’08-10-1990’,’26-12-1989’);

时间格式:

select to_char(sysdate,'yy-mm-dd') from dual;

select to_char(sysdate,'dd-mon-yy') from dual;

检索teachers表中获得奖金为500-- 600元之间的老师信息:

Select  * from teachers where bonus  between 500 and 600;

3. 使用匹配测试(Like

检索students表中所有姓袁的学生信息:

Select * from students where name Like ‘袁%’;

4. 空值测试:

studentsbonus为空:

Select * from students where bonus is null;

4.复合条件查询:

1.使用and ornot

 检索计算机专业男生信息:

Select * from students where specialty=’ 计算机’ and sex=’男’;

检索计算机专业和自动化专业学生信息:

Select * from students where specialty=’ 计算机’ or specialty=’自动化’;

检索不是计算机专业男生信息:

Select * from students where  not specialty=’ 计算机’ ;

检索students表中不姓袁的学生信息:

Select * from students where name not like ‘袁%’;

除姓名为:AB以外的的学生信息:

Select * from students where name not in(‘A’,’B’);

检索students表中在19891226日和1990810日之间出生的学生信息:

Select * from where dob between ’08-10-1990’ and ’26-12-1989’;

2.组合使用逻辑条件:

检索计算机专业女生和机电专业男生的学生信息:

Select * from students  where specialty=’计算机’ and sex=’女’  

or specialty=’机电’ and sex=’男’;

4. 检索teachers表中不是工程师,并且200211日前参加工作,工资低于3000元的教师信息:

Select * from teachers  where not  title=’工程师’  

and hire_date<’1-1-2002’  and  wage<3000;

5.记录排序:

单一排序:

1. 升序:

Select  *  from  teachers  order  by  id  ASC;

2. 降序:

Select  id  as”ID”  from  teachers  order  by  “ID”  DESC;

按多列排序:

      --按姓名降序,id升序排序:

  Select  id ,name  from  teachers  order  by id  DESC,name  asc;

6.分组查询:

1.函数AVG(),COUNT(),MAX(),MIN(),SUM()的使用:

Select  avg(wage)  from teachers;              --教师的平均工资

Select  sum(wage)  from teachers;              --教师的总工资

Select  count(*)  from teachers ;                   --教师总数

Select  max(dob), min(dob) from teachers;           --最大及最小的出生日期

Select  stddev(wage) from teachers;         --教师的工资标准偏差

2.HAVING子句(必须与GROUP BY子句一起使用)

Select department_id,AVG(wage)  from teachers 

group by department_id  HAVING(WAGE)>2200;

3. 工资高于2000,低于3000的教师,并按工资排序:

Select  id,  wage  from teachers  where  wage<3000  GROUP BY  id  HAVING  wage>=2000  order by 2;

7.子查询:

1.单行子查询(where

查询工资低于平均工资:

Select * from  teachers where wage <(select avg(wage) 

from teachers);

2.查询与小笨同专业的所有学生

Select * from students where specialty = (select specialty from students  where  name=’小笨’)

4. Having 子句使用子查询:

teachers表中查询部门平均工资高于最低部门平均工资的部门和平均工资

  Select  department_id, avg(wage) as 平均工资 from teachers  group by department_id  having  avg(wage) >

(select min( avg(wage) ) from teachers group by department_id );

 

5. 在from子句中使用子查询:

students表中的男同学中,查询计算机专业的学生:

Select  * from  (select * from students where sex=’男’)  where 

Specialty=’计算机’;

6. 多行子查询:

利用INnot in 操作符,查询students表中姓王同学信息:

Select * from  students  where  student_id  in 

(select student_id from students where  name  like ‘王%’);

查询未被学生选修的课程:

Select  *  from  courses  

Where   courses_id   not  in   (select course_id  from  students_grade);

使用any操作符:

查询工资低于如何一个部门平均工资的教师信息:

Select * from teachers where  wage<any (select  avg(wage) from teachers  group by department_id);

使用all操作符:

查询工资多余各部门平均工资的教师信息:

Select * from teachers where wage > all(select  avg(wage) from teachers  group by department_id);

7. 多列子查询:

1.和小笨同专业,同生日的学生信息:

Select * from students where (specialty,dob)=(select specialty , dob  from students where name=’小笨’);

2.teachers表中查询在各部门工资最低的教师:

  Select * from teachers where (department_id,wage) in

  (select department_id,min(wage) from teachers  

group by department_id);

8. 相关子查询:

1. 使用exists

courses表查询已经被选修的课程:

Select course_id,course_name from courses c 

Where exists (select 2 from students_grade  sg  

Where  sg.courses_id  =  c.courses_id);

2. 使用  not  exists

courses表查未被选修的课程:

Select course_id,course_name from courses c 

Where  not  exists (select 2 from students_grade  sg  

Where  sg.courses_id  =  c.courses_id);

3. 使用  in

departments表中查已经安排的教师的系部:

Select department_id,department_name from departments

Where department_id  IN

(select  department_id  from  teachers);

4. 使用 not  in

departments表中查没有安排的教师的系部:

Select department_id,department_name from departments

Where department_id   NOT  IN

(select  department_id  from  teachers);

9. 嵌套子查询:

Students表中查询与小笨同专业的学生的姓名,性别:

Select name , sex  from (

Select  * from  students where specialty=

(select specialty  from students  where  name=’小笨’)

   );

8.聚合操作:

1.使用union all 求并集(不取消重复行,而且不会对结果进行排序)

Select course_id ,course_name,credit_hour  from  courses

Union  all

Select course_id ,course_name,credit_hour  from  minors;

2.使用union求并集(自动取消重复行,而且对结果进行排序)

Select course_id ,course_name,credit_hour  from  courses

union 

Select course_id ,course_name,credit_hour  from  minors;

3.使用聚合操作符intersect(取交集,对交集的第一列对交集

进行排序)

Select course_id ,course_name,credit_hour  from  courses

intersect

Select course_id ,course_name,credit_hour  from  minors;

4.使用聚合操作符minus(取差集(即在第一个集中,不在第二个集中),对差集的第一列对差集进行排序)

Select course_id ,course_name,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors;

5. 组合使用聚合操作:

Courses表与minors表进行intersect操作,然后与courses2进行union操作:

(Select course_id ,course_name,credit_hour  from  courses

intersect

Select course_id ,course_name,credit_hour  from  minors

Union

Select course_id ,course_name,credit_hour  from  courses2;

9.聚合操作进一步讨论:

1.使用order by 子句:

使用列名作为排序表达式:

Select course_id ,course_name,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors

Order by course_name;

使用别名作为排序表达式:

Select course_id ,course_name  as  name  ,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors

Order by  name;

使用位置编号作为排序表达式:

Select course_id ,course_name,credit_hour  from  courses

minus

Select course_id ,course_name,credit_hour  from  minors

Order   by  2;  --即按course_name排序;

2.聚合操作中的数据类型:

   --namedob均改为文本型数据:   

 Select  to_char(name) as 姓名, to_char(dob,’YYY-MM-DD’)

 as  出生年月  from students;

SQL语句:

1.  select sex as 性别 ,count(*)as 人数  from tb_studentinfo group by sex

2.  select sex as 性别 ,count(*)as 人数,avg (age )as 平均年龄  from tb_studentinfo group by sex

3.  SQL注释语句: 

- -” ,/*….*/

4.  insert into tb_studentinfo values('45','yuan','boy','22','','','')

5.  delete  from tb_studentinfo where studentid='45'   --注意:delete后没有“ ”号

6.   update tb_studentinfo set sex='' where studentid='002'    --tb_studentinfo是表名  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书是专门为Oracle应用开发人员提供的SQLPL/SQL编程指南。通过学习本书,读者不仅可以掌握Oracle常用工具Oracle Universal Installer、Net Comfiguration Assistant、SQL Developer、SQL*Plus的作用及使用方法,而且可以掌握SQL语句和PL/SQL的各种基础知识和高级特征(记录类型、集合类型、对象类型、大对象类型)。   除了为读者提供编写SQL语句和开发PL/SQL块的方法外,本书还为应用开发人员提供了一些常用的PL/SQL系统包。通过使用这些PL/SQL系统包,应用开发人员可以开发出功能更强大的数据库应用程序。本书不仅适合SQLPL/SQL初学者,也适合于有经验的Oracle应用开发人员。 前言 第一部分SQLPL/SQL相关工具  第1章在Windows平台上安装OracleDatabase11g  第2章配置网络服务名  第3章使用SQLDatabase  第4章使用SQL*Plus 第二部分SQL  第5章SQLPL/SQL综述  第6章简单查询  第7章SQL单行函数  第8章操纵数据  第9章复杂查询  第10章管理常用对象 第三部分PL/SQL  第11章PL/SQL基础  第12章访问Oracle  第13章编写控制结构  第14章使用复合数 据类型  第15章使用游标  第16章异常处理 . 第17章本地动态SQL  第18章PL/SQL过程  第19章PL/SQL函数  第20章PL/SQL包  第21章触发器  第22章使用对象类型 第四部分PL/SQL系统包  第23章使用大对象  第24章读写OS文件  第25章开发多媒体应用  第26章开发Web应用  第27章DBMS_SQ动态SQL  第28章管理统计  第29章使用数据库资源管理器  第30章数据加密和解密  第31章使用调度程序  第32章使用Flashback  第33章使用重定义联机表  第34章修正损坏块  第35章使用日里民挖掘  第36章使用管道  第37章使用精细访问控制  第38章使用精细审计  第39章使用预警事件  第40章转换ROWID  第41章其他常用包 习题答案
谢谢大家的支持,我会陆续上传相关电子书 由于体积较大,本书分两卷压缩,请都下载完再解压! Oracle 11g SQLPL SQL从入门到精通 pdf格式电子书 下载(一) http://download.csdn.net/source/3268267 Oracle 11g SQLPL SQL从入门到精通 pdf格式电子书 下载(二) http://download.csdn.net/source/3268312 内容简介   本书是专门为oracle应用开发人员提供的sqlpl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法,而且可以掌握sql语句和pl/sql的各种基础知识和高级特征(记录类型、集合类型、对象类型、大对象类型)。   除了为读者提供编写sql语句和开发pl/sql块的方法外,本书还为应用开发人员提供了一些常用的pl/sql系统包。通过使用这些pl/sql系统包,应用开发人员可以开发出功能更强大的数据库应用程序。本书不仅适合sqlpl/sql初学者,也适合于有经验的oracle应用开发人员。 前言 第一部分 sqlpl/sql相关工具  第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sqlpl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 操纵数据  第9章 复杂查询  第10章 管理常用对象 第三部分 pl/sql  第11章 pl/sql基础  第12章 访问oracle  第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象类型 第四部分 pl/sql系统包  第23章 使用大对象  第24章 读写os文件  第25章 开发多媒体应用  第26章 开发web应用  第27章 dbms_sq动态sql  第28章 管理统计  第29章 使用数据库资源管理器  第30章 数据加密和解密  第31章 使用调度程序  第32章 使用flashback  第33章 使用重定义联机表  第34章 修正损坏块  第35章 使用日里民挖掘  第36章 使用管道  第37章 使用精细访问控制  第38章 使用精细审计  第39章 使用预警事件  第40章 转换rowid  第41章 其他常用包 习题答案
内容简介   本书是专门为oracle应用开发人员提供的sqlpl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法,而且可以掌握sql语句和pl/sql的各种基础知识和高级特征(记录类型、集合类型、对象类型、大对象类型)。   除了为读者提供编写sql语句和开发pl/sql块的方法外,本书还为应用开发人员提供了一些常用的pl/sql系统包。通过使用这些pl/sql系统包,应用开发人员可以开发出功能更强大的数据库应用程序。本书不仅适合sqlpl/sql初学者,也适合于有经验的oracle应用开发人员。 前言 第一部分 sqlpl/sql相关工具  第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sqlpl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 操纵数据  第9章 复杂查询  第10章 管理常用对象 第三部分 pl/sql  第11章 pl/sql基础  第12章 访问oracle  第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象类型 第四部分 pl/sql系统包  第23章 使用大对象  第24章 读写os文件  第25章 开发多媒体应用  第26章 开发web应用  第27章 dbms_sq动态sql  第28章 管理统计  第29章 使用数据库资源管理器  第30章 数据加密和解密  第31章 使用调度程序  第32章 使用flashback  第33章 使用重定义联机表  第34章 修正损坏块  第35章 使用日里民挖掘  第36章 使用管道  第37章 使用精细访问控制  第38章 使用精细审计  第39章 使用预警事件  第40章 转换rowid  第41章 其他常用包 习题答案

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值