Oacle 학습 노트 (2) 표 조회
34980 단어 oracle 학습
1 --oracle
2
3 --
4 create table users(userName varchar2(20));
5 --
6 drop table users;
7 --
8 create table student(xh number(4),xm varchar2(20),sex char(2),birthday date,sal number(7,2));
9 create table classes(classId number(2),cnmae varchar2(40));
10 --
11 alter table student add (classId number(2));
12 --
13 alter table student modify(xm varchar2(30));
14 -- / ( )
15 alter table student modify(xm char(30));
16 --
17 alter table student drop column sal;
18 --
19 rename student to stu;
20 --
21 drop table student;
22 --oracle 'DD-MON-YY' DD ,MON YY
23 --
24 alter session set nls_date_format='DD-MM-YYYY'
25 --
26 insert into student(xh,xm,sex,birthday) values(1,'aa',' ',null);
27 --
28 select * from student where birthday is null;
29 --
30 select * from student where birthday is not null;
31 --
32 update student set sal=sal/2 where sex=' ';
33 --
34 -- , , , ,
35 delect from student;
36 --
37 drop table student;
38 -- , , , ,
39 truncate table student;
40 --
41 savepoint a;
42 -- a
43 rollback to a;
44 --///////////////////////////////
45 commit
46 --
47 --
48 sesc dept;
49 select * from emp;
50 select * from dept;
51 --
52 create table users(userId varchar2(20),userName varchar2(20),userpasswd varchar2(20));
53 insert into users values('001','aaa','12aaa');
54 insert into users(userId,userName,userpasswd) select * from users;
55 -- distinct
56 select distinct deptno,job from emp;
57 -- SMITH deptno,job,sal
58 select deptno,job,sal from emp where ename='SMITH'
59 --
60 --
61 select ename,sal*13 " " from emp;
62 --
63 -- null nvl
64 -- comm 0 , comm
65 select ename,sal*13+nvl(comm,0)*13 " " from emp;
66 -- 3000
67 select * from emp where sal>3000;
68 -- 1982 1.1
69 select ename,hiredate from emp where hiredate>'1-1-1982';
70 -- 2000 2500
71 select * from emp where sal>2000 and sal<2500;
72 --like
73 --% 0
74 -- _
75 --
76 select ename ,sal from emp where ename like 'S%'
77 -- O
78 select ename ,sal from emp where ename like '__O%'
79 -- in
80 select * from emp where empno in (7844,234,345);
81 --
82 select * from emp where mgr is null;
83
84 --
85 select * from emp where (sal>500 or job='MANAGER') and ename like 'J%'
86 --
87 select * from emp order by sal;
88 --
89 select * from emp order by sal desc;
90 -- , ( )
91 select * from emp order by deptno ,sal desc;
92 -- ,
93 select * from emp order by deptno,hiredate ;
94 --
95 select ename,sal*12+nvl(comm,0)*12 " " from emp order by " ";
96 ---////////////////////////
97 --
98 --
99 --
100 select max(sal),min(sal) from emp;
101 --
102 select ename,sal from emp where sal =(select max(sal) ab from emp);
103 --
104 select * from emp where sal<( select avg(sal) from emp);
105 --group by
106 --having
107 --
108 select avg(sal),max(sal),min(sal),deptno ,job from emp group by deptno,job;
109 -- 2000
110 select avg(sal),max(sal),deptno from emp group by deptno having avg(sal)>2000;
111 --
112 -- ,having,order by
113 -- select group by>having>order by
114 select avg(sal),max(sal),deptno from emp group by deptno having avg(sal)>2000 order by avg(sal) desc;
115 --//////////////////////////////////
116 --
117 -- , , , emp dept
118 select ab.ename ,ab.sal ,ac.dname from emp ab,dept ac where ab.deptno=ac.deptno
119 -- 10 ,
120 select a1.ename,a2.dname,a1.sal from emp a1,dept a2 where a1.deptno=a2.deptno and a1.deptno=10
121 -- , , ,
122 select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno order by a1.deptno;
123 --
124 -- 'FORD'
125 select worker.ename " ",boss.ename " " from emp worker ,emp boss where worker.mgr=boss.empno and worker.ename='FORD';
126 --
127 --
128 --- smith ,
129 select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH')
130 --
131 --1.
132 select avg(sal) mysal ,deptno from emp group by deptno
133 --2.
134 select * from emp a2,(select avg(sal) mysal ,deptno from emp group by deptno) a1 where a2.deptno=a1.deptno and a2.sal>a1.mysal;
135 ---////////////////////////////////////////
136 --oracle ,
137 --1.rownum
138 select * from emp;
139 --2. rownum
140 select a1.* ,rownum rn from (select * from emp) a1 ;
141 --3.
142 --a, ,
143 --b, ,
144 select a2.* from (select a1.* ,rownum rn from (select ename,sal from emp order by sal) a1 where rownum<9)a2 where rn>=6;
145 --/////////////////////
146 --
147 create table myemp2(id,ename,sal)as select empno,ename,sal from emp;
148 desc myemp2;
149 select * from myemp2;
150 --
151 -- select , union ,union all,minus,intersect
152 --union ,
153 --union union , ,
154 --minus , , ,