您的位置:首页 > 数据库 > Oracle

Oracle之SQL 和 PL/SQL编程指南

2013-07-14 13:13 477 查看
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=’计算机’;

检索1990年1月1号以前出生学生信息:

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

2.使用包含测试:

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

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

检索students表中在1989年12月26日及1990年8月10日出生的学生信息:

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. 空值测试:

表students中bonus为空:

Select * from students where bonus is null;

4.复合条件查询:

1.使用and ,or,not

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

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 ‘袁%’;

除姓名为:A和B以外的的学生信息:

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

检索students表中在1989年12月26日和1990年8月10日之间出生的学生信息:

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表中不是工程师,并且2002年1月1日前参加工作,工资低于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. 多行子查询:

利用IN或not 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.聚合操作中的数据类型:

   --将name,dob均改为文本型数据:   

 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是表名  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: