forked from HyangahLEE/DB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalhostSCOTT_13.sql
180 lines (172 loc) · 4.24 KB
/
localhostSCOTT_13.sql
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
-- 2018.07.04(수) : SCOTT
-- 네이버 검색 : 삽입 이상
-- "삽입 이상 데이터베이스 개론 기술/공학 > 컴퓨터/통신/IT" - 클릭
-- 밑으로 스크롤 후 "정규화의 개념" 클릭
select *
from tabs
where table_name like 'SA%';
select * from sample;
-- 사원 부서
select *
from emp, dept
where emp.deptno = dept.deptno;
--
update emp
set dname = 'sales'
where empno=7369;
-- 오전 모델링..
-- 오후 PL/SQL 프로시저...
create or replace procedure up_프로시저명
(
argument [(in) 입력용,out 출력용,in out입출력용] datatype,
argument [(in) 입력용,out 출력용,in out입출력용] datatype,
argument [(in) 입력용,out 출력용,in out입출력용] datatype
)
is[as] -- declare 익명 프로시저
-- 변수,상수,커서 등 선언부 ;
begin
exception
end;
--
drop procedure 저장프로시저명;
-- num, name, kor, eng, mat, tot, savg, res
desc score;
--
select * from score;
-- insert into score 검색...
create or replace procedure up_insertScore
is
begin
insert into score (num, name, kor, eng, mat, tot, savg, res)
values (1007,'양욱형',98,78,66, 242,80.67, '합격' );
commit;
-- exception
end;
-- Procedure UP_INSERTSCORE이(가) 컴파일되었습니다.
-- Procedure UP_INSERTSCORE 저장 프로시저 실행..
-- 1) execute 문으로 실행.
execute UP_INSERTSCORE;
-- PL/SQL 프로시저가 성공적으로 완료되었습니다.
-- 2) 익명프로시저 안에서 실행
begin
UP_INSERTSCORE;
end;
select *
from user_constraints
where table_name like 'SCO%';
-- 1007 num 학생 정보 삭제
delete from score where num = 1007;
commit;
-- pk_score_num pk 제약 조건 추가..
alter table score
add ( constraint pk_score_num primary key(num) );
--
-- 3) 다른 저장 프로시저 안에서 실행
select *
from score;
-- 입력 폼에서 번,이,국,영,수 입력.....
create or replace procedure up_insertScore
(
pnum in number -- 프로서지의 인자 datatype 에는 size X
, pname in varchar2
, pkor in score.kor%type :=0
, peng in number :=0
, pmat in number :=0
)
is
-- 변수,상수,커서 등등 선언부
vtot number(3) := 0; -- 세미콜론
vsavg number(5,2);
vres varchar2(20) := '합격';
begin
vtot := pkor + peng + pmat;
vsavg := vtot / 3 ;
-- vres 합,불,과락
-- if 문 / case 문
case
when (vsavg < 60) then vres :='불합격';
when (pkor <40 or peng < 40 or (pmat) < 40 ) then vres :='과락';
else vres :='합격';
end case ;
insert into score (num, name, kor, eng, mat, tot, savg, res)
values (pnum,pname,pkor,peng,pmat, vtot,vsavg, vres );
commit;
-- exception
end;
-- Procedure UP_INSERTSCORE이(가) 컴파일되었습니다
desc score;
--
execute UP_INSERTSCORE(1008,'김동준',89,77,94);
exec UP_INSERTSCORE(1009,'강필규',23,98,76);
exec UP_INSERTSCORE(1010,'강필규');
exec UP_INSERTSCORE(1011,'이혜원',pmat=>50);
--
select * from score;
-- score 테이블에 rk 컬럼 추가
alter table score
add ( rk number );
--
select * from score;
-- execute up_makeRank;
create or replace procedure up_makeRank
is
vrank number;
begin
-- for 커서
for vrec in (select * from score)
loop
select count(*)+1
into vrank
from score
where tot > vrec.tot;
update score
set rk = vrank
where num = vrec.num;
end loop;
commit;
-- exception
end;
-- 프로시저 실행
exec up_makeRank;
--
select *
from score
order by rk asc
;
-- up_updateScore 프로시저
-- num , X
-- name/kor/eng/mat 수정
select * from score;
exec up_updateScore(1009,'강필구',1,2,3);
create or replace procedure up_updateScore
(
pnum in number -- 프로서지의 인자 datatype 에는 size X
, pname in varchar2
, pkor in score.kor%type
, peng in number
, pmat in number
)
is
-- 변수,상수,커서 등등 선언부
vtot number(3) := 0; -- 세미콜론
vsavg number(5,2);
vres varchar2(20) := '합격';
begin
vtot := pkor + peng + pmat;
vsavg := vtot / 3 ;
-- vres 합,불,과락
-- if 문 / case 문
case
when (vsavg < 60) then vres :='불합격';
when (pkor <40 or peng < 40 or (pmat) < 40 ) then vres :='과락';
else vres :='합격';
end case ;
update score
set name = pname, kor=pkor, eng=peng, mat =pmat
, tot = vtot, savg = vsavg, res = vres
where num = pnum;
commit;
-- up_updateScore 프로시저 안에서 또 다른 프로시저 호출
up_makeRank;
-- exception
end;