oracle从入门到精通(3)------多表连接查询

oracle数据库入门(3)

-------------------------------------
多表连接查询
-------------------------------------
oracle的语句分为三种类型:

    1        DDL语句(数据定义语言)    
    
                   create-------- 创建对象
                   drop --------- 删除对象
                   
    2        DML语句(数据操作语言)   ------------主要是对数据表进行相关操作                       
    
                   insert -------添加
                   delete -------删除
                   update -------修改
                   select -------查询

    3         DCL(数据控制语言)                                                        
              
                   grant-----------授权
                   revoke----------撤销权限
                   
drop table inf;                   

create table inf
(
       id number(11) primary key,
       name varchar2(20) not null,
       age number(11)
);                   
insert into inf values(1,'jack',23); 
insert into inf values(2,'tom',22);                 
                   
commit;                   
           
insert into inf values(3,'cindy',22);
insert into inf values(4,'kendy',26);        
insert into inf values(5,'chris',27);
insert into inf values(6,'christina',24);        
insert into inf values(7,'mariah',26);
insert into inf values(8,'austin',29);                          
--如果对数据表,做了增\删\改的操作,要提交后以后,数据才会更新到数据表中

提交的方式有三种:
1  点击提交安钮    
2  执行commit命令
3  如果再执行一次DDL语句,之前没有提交的语句,将会自动提交               
                   
create table inf4
(
       id number(11) primary key,
       name varchar2(29)
)                   
                   
select * from inf;                   
                   
select * from inf where age>22; --带条件查询       
select * from inf where age>22 and name like '%a%';---多条件查询,姓名模糊匹配   

--查询年龄是22,24,27,29的人员信息
select * from inf where age =22 or age=24 or age=27 or age =29;       
select * from inf where age not in (22,24,27,29);                   
                   
--查询年龄在23-26岁之间的人员信息

select * from inf where age >=23 and age<=26;                   
select * from inf where age between 23 and 26;    

--查询姓名的长度为5个字的
select * from inf where length(name)=5;               
                   
--查询姓名的长度为5,其中第一个字符必须是c的
select * from inf where name like 'c%' and length(name)=5;                   
select * from inf where name like 'c____';                   
                   
-------------------------------------------------------------
在oracle查询前三条语句
---***************注意:  top 与  limit都不能使用,如果要达到相同的效果,只能使用伪列--rownum

select t.*,rownum 序号值 from inf t where rownum<=3;
delete  from inf where id=2;                   

子查询:
        在一个查询语句中包含有多个select子语,就称为:子查询语句                   
                   
oracle中的分页查询语句:

        select * from inf;        --每页显示2条,当前第3页
 
第一步:先计算第1页加上第2,3页,总共应该显示的条数:        当前页 *  每一页显示的条数  = 应显示总数

                 2 * 3 = 6;                                                   

第二步:再计算第3页之前,已经显示的总条数:    (当前页-1)*每一页显示的条数
        
                  (3-1) * 2 = 4;
        
        
(select p.*,rownum r  from inf p   where rownum <=6)  --前三页的数据

把已经查询到的结果,当作一张表来继续查询

select * from (select p.*,rownum r  from inf p   where rownum <=6) k where k.r  >4;
        
--
聚合函数: min,max,avg,count,sum

select count(*) from inf;
select max(age) from inf;
select min(age) from inf;
select avg(age) from inf;
select sum(age) from inf;



--oracle中,多表连接的方式

       1       自连接(不需要设置任何连接条件,数据表会自动连接)  natural join
       
       2       内连接  inner join
       
       3       外连接 (左外连接 右外连接  完全外连接) left outer join,right outer join,full outer join
       
create table stu
(
       sid number(11) primary key,
       sname varchar2(20) not null
); 
insert into stu values(1,'jack');
insert into stu values(2,'bruce');
insert into stu values(3,'cindy');
drop table score;

create table score
(
       id number(11) primary key,---成绩编号
       score number(11) not null,--成绩
       stuid number(11) not null--哪一个学员的成绩
)
insert into score values(1,99,1);
insert into score values(2,87,3);

select * from stu natural join score;

--自连接的前提的条件,连接的       "两张表中必须有相同的字段"   名称,才可以使用自连接,如果没有相同的名称,
--则不允许使用,如果两张表同一字段的值是一样的,系统就会自动把这两条数据关联起来


--查询没有参加考试的学员信息
select * from stu where sid  not in (select sid from score);


----------------------------------------------------------------------
内连接 : 连接的两张表,不需要有相同的字段名,只需要设置连接条件即可

第一种:    select a.字段名,b.字段名  from A表 a ,B表  b where a.字段=b.字段 
第二种:    select a.字段名,b.字段名  from A表 a inner join B表  on (a.字段=b.字段);
        
select a.sid,a.sname,b.score  from stu a,score b where a.sid = b.stuid and b.score>=90
select a.sid,a.sname,b.score from stu a inner join score b on(a.sid=b.stuid and b.score>=90);
        
drop table stu;
drop table score;

create table stu
(
       sid number(11) primary key,
       sname varchar2(20) not null
); 
insert into stu values(1,'jack');
insert into stu values(2,'bruce');
insert into stu values(3,'cindy');
drop table score;

create table subject
(
       subId number(11) primary key,
       subName varchar(20) not null
)
insert into subject values(1,'英语');
insert into subject values(2,'化学');
insert into subject values(3,'物理');


create table score
(
       id number(11) primary key,---成绩编号
       score number(11) not null,--成绩
       subId  number(11) not null,---哪一个科目的成绩
       stuid number(11) not null--哪一个学员的成绩
       
)
insert into score values(1,99,2,1);
insert into score values(2,87,3,3);
                   
                   
select * from stu;                   
select * from subject;
select * from score;
                   

select a.sid,a.sname,b.subname,c.score 
       from stu a,subject b,score c 
            where 
                a.sid=c.stuid 
                          and 
                b.subid = c.subid
                          and 
                b.subname='化学';                                    
                                                         
-----------------
内连接的两张表之间,是平级关系,必须两张表都有的数据,才可以建立关联

--------------------------------------------------------------

外连接的两张表不是平级关系,而是主次关系,主表中的数据必须全部显示,次表中的数据,只有在与主表对应的上的
才可以显示

create table stu
(
       sid number(11) primary key,
       sname varchar2(20) not null
); 
insert into stu values(1,'jack');
insert into stu values(2,'bruce');--该学生没有参加考试
insert into stu values(3,'cindy');

create table score
(
       id number(11) primary key,---成绩编号
       score number(11) not null,--成绩
       stuid number(11) not null--哪一个学员的成绩
       
)
insert into score values(1,99,1);
insert into score values(2,65,3);
insert into score values(3,100,9);--编号为9的学生,在学生表中找不到对应的信息


select a.sname,b.score  from stu a,score b where a.sid=b.stuid;

left outer join左外连接-----------左边的表是主表,右边的表是次表

select a.sid,a.sname,b.score from stu a left outer join score b on(a.sid=b.stuid);

语法:

       select a.字段,b.字段 from A表 a, B表 b where a.字段 = b.字段(+);  --左外连接
       select a.字段,b.字段 from A表 a, B表 b where a.字段(+) = b.字段;  --右外连接


select a.sid,a.sname,b.score from stu a,score b where a.sid(+)=b.stuid;

--完全外连接   两张表都是主表,都要全部显示

select a.sid,a.sname,b.score from stu a full outer join score b on(a.sid=b.stuid);


create table inf3
(
       id number primary key,
       name varchar2(20) not null,
       bir date
)
insert into inf3 values(1,'jack',date'2011-10-02');
insert into inf3 values(2,'chris',date'2001-02-12');
insert into inf3 values(3,'andy',date'2002-03-12');
insert into inf3 values(4,'rose',date'2010-11-11');
insert into inf3 values(5,'cindy',date'2008-10-22');


select * from inf3;

select * from inf3 where bir >date'2005-01-01';

select * from inf3 where bir between date'2002-01-01' and date'2011-01-01' order by bir asc;

---oracle中,没有自动增长列

--但通过sequence (序列)可以达到相同的效果

创建序列的语法:

              create sequence 序列名  start with 从哪一个值开始  increment by 每一次递增几个值;

创建序列:
              create sequence inf3_seq start with 1 increment by 1;
drop table inf3;


create table inf3
(
       id number primary key,
       name varchar2(20) not null

)

insert into inf3 values(inf3_seq.nextval,'abc');
select * from inf3;






                   
                   
              
                                      

一、SQL ...........................................................................................................................................................8 1.1、基本概念: ...................................................................................................................................8 1.2、数据库安全 : ...............................................................................................................................8 1.3、基本的SQL SELECT 语句 .............................................................................................................8 1.4、SELECT语句 ...................................................................................................................................9 1 、语法: .......................................................................................................................................9 2、SQL语句说明: .........................................................................................................................9 3、数字和日期都可以使用数学运算符建立表达式。 ...............................................................9 4、定义空(NULL)值 ...................................................................................................................9 5、别名 ...........................................................................................................................................9 6、spool +路径 ...........................................................................................................................10 7、连接操作符: || ...................................................................................................................10 8、文本字符串 .............................................................................................................................10 9、DISTINCT .................................................................................................................................10 1.5、SQLPLUS 与 SQL 的关系 ...........................................................................................................10 1、SQLPLUS命令的功能: ...........................................................................................................10 2、查询 SQLPLUS 命令 ...............................................................................................................10 3、SQLPLUSW 在 WINDOWS 下运行的分析器。 .........................................................................10 4、SQLPLUS 命令: ..................................................................................................................... 11 1.6、单行函数 .....................................................................................................................................12 1、character字符类型函数: ...................................................................................................12 2、number数字类型函数 .............................................................................................................15 3、时间类型函数: (date) .......................................................................................................15 1.7 、嵌套函数: ..................................................................................................................................21 1. 通用函数: .......................................................................................................................21 2. 条件表达式: ...................................................................................................................24 3. 从多表中显示数据: .......................................................................................................25 1.8、用字函数产生的总计 .................................................................................................................26 1.9、子查询: .....................................................................................................................................28 2.0、替换变量: .................................................................................................................................29 1.& .................................................................................................................................................29 2.&& ...............................................................................................................................................29 2.1.环境变量: ...................................................................................................................................29 2.2 格式化命令: ................................................................................................................................30 2.3 做脚本文件的过程: ....................................................................................................................31 2.3 数据操作语句: ............................................................................................................................31 1. 插入 ...................................................................................................................................31 2. 删除 ...................................................................................................................................31 3. 更新 ...................................................................................................................................31 4. MERGE语句 .........................................................................................................................32 5. 事务(transaction) : ...................................................................................................32 2.4 创建和管理表 ................................................................................................................................33 1、表(TABLE)基本的存储单位,由行和列组成。 ...............................................................33 2 、方案:一个用户所有对象的命名集合。 .............................................................................34 3、CTAS(子查询建表) : ...........................................................................................................34 4、截取: .....................................................................................................................................35 5、给表加注释:COMMENT ...........................................................................................................36 6、约束条件: .............................................................................................................................36 2.5.视图 (VIEW) .............................................................................................................................37 2.6、序列: .........................................................................................................................................39 2.7、索引: .........................................................................................................................................40 2.8 控制用户的访问 ............................................................................................................................41 1.数据库的安全性 .......................................................................................................................41 2.角色: .......................................................................................................................................41 3.使用集合操作 ...........................................................................................................................42 4.ORDER BY 子句: .....................................................................................................................42 5.GROUP BY 子句的增强 .............................................................................................................43 6.GROUPING 函数 .........................................................................................................................43 2.9 高级子查询 ....................................................................................................................................44 1. 成对子查询: ...................................................................................................................44 2.层次查询 ...................................................................................................................................44 二、Management: .......................................................................................................................................45 1.Oracle的构件和组件 .......................................................................................................................45 2.数据库的物理结构: .......................................................................................................................46 1.控制文件 ...................................................................................................................................46 2. 数据文件 ...........................................................................................................................46 3. 重做日志文件 ...................................................................................................................46 4. data file 数据文件: .................................................................................................46 5. 作用:存放数据。 ...........................................................................................................46 6. 数据文件大小可以扩展。 ...............................................................................................46 7. tablespace 表空间:一个或多个数据文件的逻辑组成。 .........................................46 8. redo log file 重做日志文件 .....................................................................................46 9. control file 控制文件 ...............................................................................................46 10. parameter file 初始化参数文件 ...............................................................................46 11. password file 口令文件 ...........................................................................................47 12. archived log file 归档日志文件 .............................................................................47 3.instance 实例/例程 .......................................................................................................................47 4、进程结构 .........................................................................................................................................49 1. 用户进程:开始于数据库用户请求连接数据库 ...........................................................49 2. 服务进程:与ORA实例连接,开始于用户会话的建立。 .............................................49 3. 后台进程:当ORA实例启动时启动 .................................................................................49 1. DBWR 数据库写进程 .......................................................................................................49 2. LGWR 重作日志写进程 ...................................................................................................50 6.CKPT 检查点进程 .................................................................................................................50 7.ARCn 归档进程(可选) .....................................................................................................50 8.LOGICAL STRUCTURE 逻辑结构 ............................................................................................50 5、OEM ORACLE 企业管理器 ...............................................................................................................51 6.管理ORA实例 .....................................................................................................................................51 7.启动过程: .......................................................................................................................................52 1. NOMOUNT 实例启动阶段 ...................................................................................................52 2. MOUNT 数据库装载阶段 ...................................................................................................52 3. OPEN 打开数据库 .............................................................................................................52
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值