반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* 
    DML (Data Manipulation Language) : 데이터 조작어
    SELECT문, DELETE문, INSERT문, UPDATE문
    
    --형식--
    SELECT 컬럼명, 컬럼명2,...
    FROM 테이블1, 테이블2,...
    WHERE 조건들
    
    //테이블의 레코드(로우)를 입력
    INSERT INTO 테이블명 (컬럼명1, 컬럼명2, ...)
    VALUES (값1, 값2, ...); 
    
    UPDATE 테이블명 
    SET 컬럼명1 = 값1,
        컬럼명2 = 값2
    WHERE 조건..;
    
    
    //테이블의 레코드(로우)를 삭제
    DELETE (FROM-생략가능)테이블명
    WHERE 조건;   
*/
    
/* 테이블 생성 */
 
create table sample(
    deptNo number(20),
    deptName varchar2(15),
    deploc varchar2(15),
    depManager varchar2(10)
);
 
 
desc sample;
 
 
 
/* 데이터 입력 */
 
insert into sample(deptNo, deptName, deploc, depManager)
values(10'기획실''서울''홍길동');
 
 
select * from sample;
 
 
 
/* 모든 컬럼에 데이터를 넣을 경우 컬럼명 생략가능 */
 
insert into sample
values(20,'전산실','부산','김말동');
 
 
insert into sample
values(30,'영업부','광주',null);
 
 
 
/* 데이터 필드 수정 */
 
update sample set deptNo=50
where deptNo=30;
 
 
update sample set deploc = '인천'
where deptNo = 50;
 
 
 
/* 데이터 삭제 */
 
delete sample 
where deptNo = 50;
 
 
 
/* 테이블 내용 모두 삭제 */
 
delete sample;
 
 
 
/* commit  */
 
insert into sample
values(20,'전산실','부산','김말동');
 
 
insert into sample
values(30,'영업부','광주',null);
 
 
commit;
 
 
 
/* rollback : commit한 시점으로 돌아감*/
 
update sample set deploc = '인천'
where deptNo = 30;
 
 
select * from sample;
 
 
delete sample;
 
 
rollback;
 
select * from sample;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] VIEW  (0) 2017.08.16
[Oracle SQL] 무결성 제약조건  (0) 2017.08.12
[Oracle SQL] CREATE, ALTER, DROP, TRUNCATE  (0) 2017.08.10
[Oracle SQL] SubQuery 서브쿼리  (0) 2017.08.10
[Oracle SQL] Join  (0) 2017.08.08
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*  DDL (Data Definition Language) : Creat문, Drop문, Alter문, Truncate문 */
 
/* Create Table */
 
 create table employees2(
    employee_id number(10),
    name varchar2(20),
    salary number(7,2)
 );
 
 
select * from employees2;
 
 
 
/* 같은 테이블을 동일하게 만들 때 */
 
create table employees3 
as
select * from employees2;
 
 
select * from employees3;
 
 
 
/* Alter - add: 테이블 추가 */
 
alter table employees2 add(
    manager_id varchar2(10)
    );
 
 
select * from employees2;
 
 
 
/* Alter - modify : 테이블 수정 */
 
alter table employees2 modify(
    manager_id varchar2(20)
    );
 
 
 
 
/* Alter - drop column : 칼럼 삭제 */
 
alter table employees2 drop column manager_id;
 
 
select * from employees2;
 
 
 
/* 
    문자형 데이터 
    char, varchar, nchar, nvarchar, long 형태 존재
    long은 최대 2GB가능 
 */
 
/* Drop문 : 테이블 구조를 삭제 */
 
drop table employees2;
 
 
select * from employees2;
 
 
/* Rename */
 
Rename employees3 to employees2;
 
 
desc employees3;
desc employees2;
 
/* Truncate : 테이블 내의 모든 레코드 삭제 */
 
truncate table employees2;
 
 
select * from employees2;
cs


반응형
반응형





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 서브쿼리 
    : Main Query에 반대되는 개념
    : 메인쿼리를 구성하는 소단위 쿼리
    : select / insert / delete / update 절에서 모두 사용가능
    : 서브쿼리의 결과 집합을 메인 쿼리가 중간 결과값으로 사용
    : 서브쿼리 자체는 일반 쿼리와 다를 바 없음
 */
 
 select round(avg(salary)) 
 from employees;
 
 
 select employee_id, first_name, last_name
 from employees
 where salary < 6462;
 
 
 /* Where절에서는 집계함수 사용 불가 */
 
 select employee_id, first_name, last_name
 from employees
 where salary < round(avg(salary));
 
 
 
 /* select서브쿼리 문 */
 
 select employee_id, first_name, last_name
 from employees
 where salary < (select round(avg(salary)) 
                 from employees);
 
 
 
 select location_id
 from locations
 where state_province is null;
 
 
 
 select * from departments
 where location_id in ( select location_id
                       from locations
                       where country_id = 'US' );
 
 
 
/* 월급이 가장 적은 사원 */
 
select emp.first_name, emp.last_name, job.job_title, emp.salary
from employees emp, jobs job
where emp.salary = (select min(salary) from employees)
  and emp.job_id = job.job_id;
 
 
 
/* 월급이 가장 많은 사원 */
 
select emp.first_name, emp.last_name, job.job_title, emp.salary
from employees emp, jobs job
where emp.salary = ( select max(salary) from employees)
  and emp.job_id = job.job_id;
  
 
 
/* 평균 급여보다 많이 받는 사원들의 명단 조회 */
 
select emp.first_name, emp.last_name, job.job_title
from employees emp, jobs job
where emp.salary > (select avg(salary) from employees)
  and emp.job_id = job.job_id;
 
 
 
/* any, all */
 
select salary
from employees
where department_id = 20;
 
 
 
/* department_id = 20의 최소 연봉보다 많이 받는 사람 모두 출력 */
 
select employee_id, department_id, salary
from employees 
where salary > any ( select salary
                     from employees
                     where department_id = 20 );
 
 
 
select employee_id, department_id, salary
from employees 
where salary > ( select min(salary)
                 from employees
                 where department_id = 20 );
 
 
 
/* department_id = 20의 최대 연봉보다 많이 받는 사람 모두 출력 */
 
select employee_id, department_id, salary
from employees 
where salary > all ( select salary
                     from employees
                     where department_id = 20 );
 
 
 
select employee_id, department_id, salary
from employees 
where salary > ( select max(salary)
                 from employees
                 where department_id = 20 );
 
 
 
/* department_id = 20의 연봉과 동일하게 받는 사람 모두 출력 */
 
select employee_id, department_id, salary
from employees 
where salary in ( select salary
                 from employees
                 where department_id = 20 );
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] INSERT, UPDATE, DELETE, COMMIT, ROLLBACK  (0) 2017.08.10
[Oracle SQL] CREATE, ALTER, DROP, TRUNCATE  (0) 2017.08.10
[Oracle SQL] Join  (0) 2017.08.08
[Oracle SQL] GROUP BY, HAVING, ROLLUP  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Inner Join : 일반적 Join */
 
select e.first_name, e.last_name, e.email, d.department_name
from employees e, departments d
where e.department_id = d.department_id;
 
 
 
select emp.first_name, emp.last_name, emp.email, emp.department_id, 
       dep.department_name, job.job_title, loc.city
from employees emp, 
     departments dep,
     jobs job,
     locations loc
where emp.department_id = dep.department_id
  and emp.job_id = job.job_id
  and dep.location_id = loc.location_id
  and loc.city = 'Seattle';
 
 
/* Self Join : 자신의 테이블에서 자신의 칼럼을 Join */
 
select emp1.employee_id, emp1.first_name, emp2.employee_id "상사ID", emp2.first_name "상사 이름"
from employees emp1, employees emp2
where emp1.manager_id = emp2.EMPLOYEE_ID;
 
 
/* 
   Outter Join
   (+) : 두개의 테이블 중 하나의 테이블 컬럼 데이터가 없을 경우 
         ROW가 누락되기 때문에 모든 데이터를 누락없이 출력하기 위해 사용
*/
 
select emp.employee_id, emp.first_name, emp.department_id, dep.department_name, loc.city
from employees emp, departments dep, locations loc
where emp.department_id = dep.department_id (+)
  and dep.location_id = loc.location_id (+);
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] CREATE, ALTER, DROP, TRUNCATE  (0) 2017.08.10
[Oracle SQL] SubQuery 서브쿼리  (0) 2017.08.10
[Oracle SQL] GROUP BY, HAVING, ROLLUP  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 숫자함수  (0) 2017.08.04
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Group by 절 */
 
select distinct(department_id) 
from employees;
 
 
select department_id 
from employees 
group by department_id;
 
 
 
/* 부서 별 급여 합계 */
 
select department_id, sum(salary) 
from employees 
group by department_id;
 
 
 
/* 단일 그룹이 아니므로 에러 */
 
select distinct(department_id), sum(salary) 
from employees; 
 
 
 
/* 부서 별 사원수와 급여 평균 */
 
select department_id, sum(salary), count(salary), avg(salary)
from employees
group by department_id;
 
 
 
/*  부서별 직급별 사원수와 급여 평균 */
 
select department_id, job_id, sum(salary), count(salary), avg(salary)
from employees
group by department_id, job_id
order by department_id, job_id;
 
 
 
select department_id, job_id,
   to_char(sum(salary),'999,999'"총급여",
   to_char(avg(salary),'999,999'"평균급여"
from employees
where department_id = 80
group by department_id, job_id
order by department_id, job_id;
 
 
 
/* 현재 부서 별 사원수 */
 
select department_id, count(*
from employees
where department_id is not null
group by department_id;
 
 
 
/* Where절에서는 집계함수 사용 불가 */
 
select department_id, count(*)
from employees
where department_id is not null
    and count(*< 10
group by department_id;  
 
 
 
/* Having 절 */
 
select department_id, count(*
from employees
where department_id is not null
group by department_id
Having count(*>=10;
 
 
 
/* Rollup : 그룹별 합계 정보를 추가해 보여주는 함수 */
 
select l.city, d.department_name, e.job_id,
  count(*"사원수", sum(e.salary) 총급여
from employees e, departments d, locations l
where e.department_id = d.department_id
  and d.location_id = l.location_id
group by l.city, d.department_name, e.job_id
order by l.city, d.department_name, e.job_id;
 
 
 
select l.city, d.department_name, e.job_id,
  count(*"사원수", sum(e.salary) 총급여
from employees e, departments d, locations l
where e.department_id = d.department_id
    and d.location_id = l.location_id
group by rollup(l.city, d.department_name, e.job_id)
order by l.city, d.department_name, e.job_id;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] SubQuery 서브쿼리  (0) 2017.08.10
[Oracle SQL] Join  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] 문자 함수  (0) 2017.08.03
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 날짜 함수 */
 
/* sysdate()  : 현재날짜 (system의 날짜) */
 
select sysdate 
from dual; 
 
 
 
/* months_between(date1-최근날짜,date2-이전날짜) */
 
select first_name, last_name, months_between(sysdate,hire_date) 
from employees
where department_id = 50/* 두 날짜 사이의 개월수 차이 (고용일부터 현재까지)*/
 
 
 
/* add_months() */
 
select add_months(sysdate, 4-- 개월수를 더함
from dual; 
 
 
 
/* next_day() */
 
select next_day(sysdate,'일요일'-- 다가올 일요일에 해당하는 날짜
from dual; 
 
 
select next_day(sysdate,'월'
from dual; 
 
 
 
/* last_day() */
 
select last_day(sysdate) -- 해당 달의 마지막 일수 
from dual;
 
 
 
/* to_char() */
 
select to_char(sysdate, 'yyyy-mm-dd'-- 문자열 형태로 출력 
from dual; 
 
 
select to_char(sysdate, 'yy/mm/dd')
from dual;
 
 
 
/* to_date() */
 
select to_date('2015/03/03''yyyy/mm/dd'-- 문자열을 날짜형태로 바꿈 
from dual; 
 
 
 
/* nvl() : null값을 다른 데이터로 변경하는 함수 */
 
select first_name, last_name, nvl(commission_pct, 0) commission 
from employees;
 
 
 
/* decode() : switch문의 역할을 하는 함수 */
 
select department_id, decode(department_id, 20'마케팅부'60'IT'90'경영','부서')
from employees;
 
 
/* case() : else if 문과 같은 역할을 하는 함수 */
 
select first_name, department_id
 
    case when department_id = 20 then 'marketing'
         when departmnet_id = 60 then '전산실'
         when department_id = 90 then '경영'
         else 'Others'
         and "부서명"
 
 from employees;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] Join  (0) 2017.08.08
[Oracle SQL] GROUP BY, HAVING, ROLLUP  (0) 2017.08.08
[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] 문자 함수  (0) 2017.08.03
[Oracle SQL] Select 문 And,OR, NOT, BETWEEN  (0) 2017.07.29
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Number function */
/* 
    dual : 오라클에서 제공되는 dummy table, 테이블이 없을 때 사용 
    all 
    distinct : 중복제거
*/
 
 
/* abs() : 절대값 */
 
select abs(-23
from dual;
 
 
 
/* sign() */
 
select sign(23), sign(-23), sign(0
from dual;
 
 
 
/* round(n1,n2) : 반올림 */
 
select round(0.123), round(0.543), round(0.125
from dual;
 
 
select round(0.123456789,6), round(0.543465,2), round(0.125254,4
from dual;
 
 
 
/* trunc(n1,n2) : 소수점 자르기 */
 
select trunc(1234.1234567) zero 
from dual;
 
 
select trunc(1234.1234567,0
from dual;
 
 
select trunc(1234.1234567,2
from dual;
 
 
select trunc(1234.1234567,-1
from dual;
 
 
 
/* ceil() : 올림 */
 
select ceil(32.8) ceil 
from dual;
 
 
select ceil(32.3) ceil 
from dual;
 
 
 
/* floor() : 내림 */
 
select floor(32.8) floor 
from dual;
 
 
select floor(32.3) floor 
from dual;
 
 
 
/* power(n1,n2) : 제곱 */
 
select power(4,2) power 
from dual;
 
 
 
/* mod(n1,n2) : 나머지 */
select mod(7,4) mod 
from dual;
 
 
/* sqrt() : 제곱근 */
select sqrt(2), sqrt(3
from dual;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] GROUP BY, HAVING, ROLLUP  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 문자 함수  (0) 2017.08.03
[Oracle SQL] Select 문 And,OR, NOT, BETWEEN  (0) 2017.07.29
[Oracle SQL] 토드 설치  (0) 2017.07.29
반응형





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Character function */
 
/* concat(char1, char2) or || : 문자열 연결*/
 
select concat('Hello','Bye'), concat('Good','Bad'
from dual;
 
 
select 'Hello'||'Bye''Good'||'Bad' 
from dual;
 
 
 
/* initcap(char) : 첫문자 대문자 변환 */
 
select initcap('good mornig'
from dual;
 
 
select initcap('good/bad mornig'
from dual;
 
 
 
/* Lower(char) : 소문자로 변환 */
 
select lower('GOOD'
from dual;
 
 
 
/* Upper(char) : 대문자로 변환 */
 
select upper('good'
from dual;
 
 
 
/* Lpad(expr1,n,expr2) : expr1을 n만큼 왼쪽으로 늘려서 반환 */
 
select lpad('good',6"LAPD1",
       lpad('good',6,'@'"LPAD2",
       lpad('good',8,'L'"LPAD3"
from dual;
 
 
 
/* 
   Rpad(expr1,n,expr2) : expr1을 n만큼 오른쪽으로 늘려서 반환 
   한글은 한글자를 2byte로 계산하여 나머지를 늘임
*/
 
select rpad('good',6"RPAD1",
       rpad('good',7,'#'"RPAD2",
       rpad('good',8,'L'"RPAD3",
       rpad('안녕',7,'#'"RPAD4"
from dual;
 
 
 
/* Ltrim(char,set) : char에서 set으로 지정된 물자를 왼쪽에서 제거 */
 
select ltrim('goodbye'
from dual;
 
 
select ltrim('goodbye',' '
from dual;
 
 
select ltrim('goodbye','g'), ltrim('goodbye','o'), ltrim('goodbye','go'
from dual;
 
 
 
/* Rtrim(char,set) : char에서 set으로 지정된 물자를 오른쪽에서 제거 */
 
select rtrim('goodbye'
from dual;
 
 
select rtrim('goodbye','go'
from dual;
 
 
select rtrim('goodbye','e'), rtrim('goodbye','ye'
from dual;
 
 
/* Substr(char,pos,leng) : 문자열 일부 반환 */
 
select substr('good morning john',8,4
from dual;
 
 
select substr('good morning john',8
from dual;
 
 
select substr('good morning john',-4
from dual;
 
 
select substr('good morning john',-4,2/*pos가 음수이므로 문자열 끝에서부터 2자리 가져옴*/
from dual; 
 
 
select substr('good morning john',-4,0)  /*length가 0이므로 결과 없음*/
from dual;
 
 
 
/* Substrb(char,pos,byte) : 문자열 일부 반환 */
 
select substrb('good morning john',8,4)  /*byte 수 만큼 가져옴*/
from dual;
 
 
 
/* Replace(char,src_str,rep_str) : 문자열 일부를 대체해서 반환 */
 
select replace('good morning Tom','morning','evenning'
from dual;
 
 
 
/* Translate(expr,frm_str,to_str) : 문자자체를 일대일로 대응, 변환 */
 
select replace('You are not alone''You''We') replaces, 
/*결과 We are not alone*/
       translate('You are not alone''You''We')translates  
/*결과 We are net alene : 문자'o'를 'e'로 변환*/
from dual;
 
 
/* Trim([Leading,Trailing,Both],[trim_chr][FROM]trim_src) : 
trim_src에서 trim_chr을 앞,뒤 혹은 모두에서 제거한 결과 반환 */
 
select trim(leading from 'good'
from dual;
 
 
select length(trim(leading from ' good ')) 
from dual; /*앞(왼쪽)의 공백 삭제하므로 결과 5출력*/
 
 
select length(trim(trailing from ' good ')) 
from dual; /*뒤(오른쪽)의 공백 삭제하므로 결과 5출력*/
 
 
select length(trim(both from ' good ')) 
from dual; /*양쪽의 공백 삭제하므로 결과 4출력*/
 
 
select trim(leading 'g' from 'good'
from dual; /*왼쪽의 g를 삭제하므로 결과 ood출력*/
 
 
select trim(trailing 'd' from 'good'
from dual; /*오른쪽의 d를 삭제하므로 결과 ood출력*/
 
 
select trim(both 'g' from 'good'
from dual; /*양쪽의 g를 삭제하므로 결과 ood출력*/
 
 
 
/* ASCII(char) : ASCII코드 값 반환 */
 
select ascii('A'
from dual;
 
 
/* Instr(string,src_str,pos,occur) : 특정 문자열의 위치를 찾아 반환 */
 
select instr('good morning john','or',1
from dual;
 
 
select instr('good morning john','n'1,3
from dual;
 
 
 
/* Length(char) : 문자열 길이 반환 */
 
select length('good'
from dual;
 
 
  cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] Select 문 And,OR, NOT, BETWEEN  (0) 2017.07.29
[Oracle SQL] 토드 설치  (0) 2017.07.29
[SQL] 기초함수 예제  (0) 2017.07.07

+ Recent posts