컬렉션 조회

--         ,  ,  ,    
--    "  " 
--     :Sno,   char(9)-->        9        ;
--     :Sname,   varchar2(8)-->     2,3,4  ,                ;
--     :Sage,   smallint-->smallint        ,       ,-32,768-->32,767;
--     :Ssex,   char(2)-->           ,' '  ' ',    2         ;
--       :Sdept,   varchar2(4)-->              ,    4   ;
--            ;
create table Student
(
       Sno char(9) primary key,
       Sname varchar2(8),
       Ssex char(2) not null,
       Sage smallint not null,
       Sdept varchar2(4) not null
);

--    "  " 
--      :Cno,   char-->      4      ;
--      :Cname,   varchar2-->             ,         10   ,    20     ;
--        :Cpno,   char-->                        ;
--     :Ccredit,   smallint-->            ;
--  "  "      Cno;
--  "  "      Cpno;     Cpno        Cno;
create table Course
(
       Cno char(4) primary key,
       Cname varchar2(20) not null,
       Cpno char(4),
       Ccredit smallint not null,
       foreign key(Cpno) references Course(Cno)
);

--    "  " 
--     Sno,   char(9)-->      9        ;
--      Cno,   char(4)-->       4        ;
--     Grade,   smallint-->     0->100     ;
--      :Cno Sno   ;
--      :     Sno      Student    Sno;     Cno      Courses     Cno;
create table SC
(
       Sno char(9),
       Cno char(4),
       Grade smallint,
       primary key(Sno,Cno),
       foreign key(Sno) references Student(Sno),
       foreign key(Cno) references Course(Cno)
);

--            ,
--  value=ZHS16GBK,        2   ,       2      ;
--  value=AL32UTF8,        3   ,       3      ;
select * from v$nls_parameters t where t.PARAMETER='NLS_CHARACTERSET';
alter user scott account unlock;
alter user sys account unlock;
alteruseruser_name account unlock identifiedby  new_password;
--        value = ZHS16GBK,                      .

--         
insert into Student values('201215121','  ',' ',20,'CS');
insert into Student values('201215122','  ',' ',19,'CS');
insert into Student values('201215123','  ',' ',18,'MA');
insert into Student values('201215125','  ',' ',19,'IS');

--        
insert into Student values('201215121','  ',' ',20,'CS');
insert into Student values('201215122','  ',' ',19,'CS');

--        
insert into Student values(null,'  ',' ',19,'CS');
insert into Student values(null,'  ',' ',20,'IS');

--          
insert into Student values('201215126','  ',' ',null,'IS');
insert into Student values('201215126','  ',null,30,'IS');

--         
select * from Student;

--     
drop table Student;

--         
--       ,             -->  Cpno               ;
--       ,             -->           ,    ,        ;
insert into Course values('2','  ',null,2);
insert into Course values('6','    ',null,2);
insert into Course values('4','    ','6',3);
insert into Course values('7','PASCAl  ','6',4);
insert into Course values('5','    ','7',4);
insert into Course values('1','   ','5',4);
insert into Course values('3','    ','1',4);

--           
insert into Course values('1','   ','5',5);
insert into Course values('3','    ','1',4);

--         
insert into Course values(null,'   ','5',4);
insert into Course values(null,'    ','7',4);

--         
insert into Course values('8','    ','20',4);
insert into Course values('9','    ','11',4);

--           
insert into Course values('10','    ','7',null);
insert into Course values('13','    ','6',null);

--                            ,        
insert into Course values('2','  ',null,2);
insert into Course values('6','    ',null,2);

--           
select * from Course;

--     
drop table Course;

--         
--                ,                   ,          ;
--                  ,                           ;
--                         ,     
insert into SC values('201215121','1',92);
insert into SC values('201215121','2',85);
insert into SC values('201215121','3',88);
insert into SC values('201215122','2',90);
insert into SC values('201215122','3',80);

--insert into SC values('201215122','1',90);

--        ,                     
insert into SC values('201215122','9',100);
insert into SC values('201400644','1',70); 

--         ,             ,     
insert into SC values('201215122','3',90);
insert into SC values('201215121','1',92);

--        ,                
insert into SC values('201215122',null,90);
insert into SC values(null,'2',70);   

--           
select * from SC;     

--        
drop table SC;
--select               
--          ,  ,  
--  union,  intersect,   except

--               19    
--                      19    
select *
from Student
where Sdept='CS'
union
select *
from Student
where Sage<=19;

--  union              ,     
--             ,           ,
--    union all      
--                  ,     
--                
select *
from Student
where Sdept='CS'
union all
select *
from Student
where Sage>=19;

--       or                 
--               19    
select *
from Student
where Sdept='CS' or Sage<=19;

--       1       2   
--                     2        
select *
from SC
where Cno='1'
union
select *
from SC
where Cno='2';

--              ,    union all   
select *
from SC
where Cno='1'
union all
select *
from SC
where Cno='2';

--                      
--              
select *
from SC
where Cno='1' or Cno='2';

--                 19     
select *
from Student
where Sdept='CS'
intersect
select *
from Student
where Sage<=19;

--         ,       and       
--         and                
select *
from Student
where Sdept='CS' and Sage<=19;

--     1       2      .
--   ,                  2          
--               intersect
select Sno
from SC
where Cno='1'
intersect
select Sno
from SC
where Cno='2';

select *
from SC
where Cno='1'
intersect
select *
from SC
where Cno='2';

--                    ?
--                      
--   1      ,      2   ,
--     :    2              
--         2             
select Sno
from SC
where Cno='2';
--                     2      
select Sno
from SC
where (Sno='201215121' or Sno='201215122') and Cno='2' 

--       in      
select Sno
from SC
where Sno in('201215121' ,'201215122') and Cno='2';

--      :    1          2   
select Sno
from SC
where Cno='1';

--             ,        1      
select Sno
from SC
where (Sno='201215121' or Sno='201215122') and Cno='1';

--       in      
select Sno
from SC
where Sno in('201215121' ,'201215122') and Cno='1';

--                 19       
select *
from Student
where Sdept='CS'
except
select *
from Student
where Sage<=19;
--     ?ORA-00933: SQL        

--                     19    
select *
from Student
where Sdept='CS' and Sage>19;

좋은 웹페이지 즐겨찾기