-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.integrity.sql.txt
More file actions
373 lines (255 loc) ยท 11 KB
/
9.integrity.sql.txt
File metadata and controls
373 lines (255 loc) ยท 11 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
๏ปฟ-- 9.integrity.sql
-- DB ์์ฒด์ ์ผ๋ก ๊ฐ์ ์ ์ธ ์ ์ฝ ์ฌํญ ์ค์
-- ๊ฐ๋ฐ์ ๋ฅ๋ ฅ + ์์คํ
(HW/SW) ๋ค ์ ๊ธฐ์ ์ผ๋ก ์ฐ๊ณ
-- ์ค์ ์ ๊ฐ๋ฐ์์ ์ฑ
์(๋ฐ์ดํฐ๋ฅผ ๋ฌด๊ฒฐํ๊ฒ ๊ด๋ฆฌ? ์ค๋ณตํ์ฉ? nullํ์ฉ ๋จ ์ค๋ณต ๋ถํ? ๊ฒ์ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ก?)
/*
1. table ์์ฑ์ ์ ์ฝ์กฐ๊ฑด์ ์ค์ ํ๋ ๊ธฐ๋ฒ
2. ์ ์ฝ ์กฐ๊ฑด ์ข
๋ฅ
emp์ dept์ ๊ด๊ณ
2-1. PK[primary key] - ๊ธฐ๋ณธํค, ์ค๋ณต๋ถ๊ฐ, null๋ถ๊ฐ, ๋ฐ์ดํฐ๋ค ๊ตฌ๋ถ์ ์ํ ํต์ฌ ๋ฐ์ดํฐ
: not null + unique
:ํต์ฌ data
emp์ empno/dept์ deptno
2-2. not null - ๋ฐ๋์ ๋ฐ์ดํฐ ์กด์ฌ
:๋ณดํธ์ ์ผ๋ก ์ ์ฝ์กฐ๊ฑด๋ช
์ ์ ๋ช
์ํ์ง ์์
2-3. unique - ์ค๋ณต ๋ถ๊ฐ
: null๊ฐ์ ํ์ฉ
2-4. check - table ์์ฑ์ ๊ท์ ํ ๋ฒ์์ ๋ฐ์ดํฐ๋ง ์ ์ฅ ๊ฐ๋ฅ
: ๊ฐ๋ฐ ์ธ์ด ๊ด์ ์์ if๋ฌธ๊ณผ ๋์ผํ๋ค ๊ฐ์ฃผ
2-5. default - insert์์ ํน๋ณํ ๋ฐ์ดํฐ ๋ฏธ์ ์ฅ์์๋ ์๋ ์ ์ฅ๋๋ ๊ธฐ๋ณธ ๊ฐ
- ์๋ฐ ๊ด์ ์๋ ๋ฉค๋ฒ ๋ณ์ ์ ์ธ ํ ๊ฐ์ฒด ์์ฑ ์งํ ๋ฉค๋ฒ ๋ณ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ด๊ธฐํ
* 2-6. FK[foreign key]
- ์ธ๋ํค[์ฐธ์กฐํค], ๋ค๋ฅธ table์ pk๋ฅผ ์ฐธ์กฐํ๋ ๋ฐ์ดํฐ
- table๊ฐ์ ์ฃผ์ข
๊ด๊ณ๊ฐ ํ์ฑ
- pk ๋ณด์ table์ด ๋ถ๋ชจ, ์ฐธ์กฐํ๋ table์ด ์์
- ๋ถ๋ชจ์ ๋ฏธ ์กด์ฌํ๋ ๋ฐ์ดํฐ๋ฅผ ์์์์ ์๋ก ์์ฑ๊ฐ๋ฅ? ๋ถ๊ฐ
- ์์ table๋ค์ด ์กด์ฌํ ๊ฒฝ์ฐ ๋ถ๋ชจtable๋ง ์ญ์ ๊ฐ๋ฅ? ๋ถ๊ฐ
- ํด๊ฒฐ์ฑ
: ๊ด๊ณ๋ฅผ ํด์ ๋ ๊ฐ๋ฅํ๋ ๊ฐ๊ธ์ ๋ถ์์ค๊ณ์ ์๋ฒฝํ๋ฆฌ๋งํผ ๊ณ ๋ฏผํ ์ค๊ณ
3. ์ฌ์ฉ์ ์ ์ํ๋ ์ ์ฝ ์กฐ๊ฑด์ ์ ์ฝ ์กฐ๊ฑด๋ช
๋ช
์ํ๋ ๋ฐฉ๋ฒ
3-1. oracle engine์ด ๊ธฐ๋ณธ์ ์ผ๋ก ์ค์
- ์ฌ์ฉ์๊ฐ ์ ์ฝ ์กฐ๊ฑด์ ๋ณ๋์ ์ด๋ฆ์ ๋ถ์ฌํ์ง ์์ผ๋ฉด ์ค๋ผํด ์์ฒด์ ์ผ๋ก SYS_์์ํ๋ ์ด๋ฆ์ ์๋ ๋ถ์ฌ
- SYS_Xxxx
3-2. sql๊ฐ๋ฐ์๊ฐ ์ง์ ์ค์
- table๋ช
_์ปฌ๋ผ๋ช
_์ ์ฝ์กฐ๊ฑด๋ช
๋ฑ ๊ธฐ์ ..๋จ ์์๋ ์์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
: dept์ deptno์ด ์ ์ฝ์กฐ๊ฑด๋ช
PK_DEPT
pk_dept_deptno
- ์ฝ์ด ์ฌ์ฉ๋ ๊ฐ๋ฅ[๋ถ์, ์ค๊ณ์ ์ฉ์ด์ฌ์ ์ ์ํ ์์ฑ ๊ถ์ฅ]
4. ์ ์ฝ์กฐ๊ฑด ์ ์ธ ์์น
4-1. ์ปฌ๋ผ ๋ ๋ฒจ ๋จ์
- ์ปฌ๋ผ์ ์ธ ๋ผ์ธ์ ์ ์ฝ์กฐ๊ฑด ์ค์
4-2. ํ
์ด๋ธ ๋ ๋ฒจ ๋จ์
- ๋ชจ๋ ์ปฌ๋ผ ์ ์ธ ์งํ ๋ณ๋๋ก ์ ์ฝ์กฐ๊ฑด ์ค์
5. ์ค๋ผํด ์์ฒด ํน๋ณํ table
- ์ ๋ณด๋ง ์ ๊ณตํด์ฃผ๋ ์ฌ์ table๋ค์ด ๋ค์ ์กด์ฌ
- ์ฌ์ฉ์๋ค์ select๋ง ๊ฐ๋ฅ
- oracle db๊ฐ ์์ฒด์ ์ผ๋ก insert/updateํจ
5-1. user_constraints
- ์ ์ฝ์กฐ๊ฑด ์ ๋ณด ๋ณด์ table
- ๊ฐ๋ฐ์๊ฐ table์ ๋ฐ์ดํฐ๊ฐ ์ง์ ์์ ๋ถ๊ฐ
- select constraint_name, constraint_type, table_name
from user_constraints;
- table๋ช
์ด ๋ฌด์กฐ๊ฑด ๋๋ฌธ์๋ก ์ ํ๋์ด ์ ์ฅ
๋ฐ๋ผ์ table๋ช
์ ๋ฐ์ดํฐ๋ก ์ฌ์ฉ ์ ๋๋ฌธ์๋ก๋ง ์ ์ฉํด์ผ ํจ
6. ์ด๋ฏธ ์กด์ฌํ๋ table์ ์ ์ฝ์กฐ๊ฑด ์์ (์ถ๊ฐ, ์ญ์ )๋ช
๋ น์ด
6-1. ์ ์ฝ์กฐ๊ฑด ์ถ๊ฐ
alter table ํ
์ด๋ธ๋ช
add constraint ์ ์ฝ์กฐ๊ฑด๋ช
์ ์ฝ์กฐ๊ฑด(์ปฌ๋ผ๋ช
);
alter table dept01 add constraint dept01_deptno_pk primary key(deptno);
6-2. ์ ์ฝ์กฐ๊ฑด ์ญ์ (drop)
- table์ญ์
alter table ํ
์ด๋ธ๋ช
cascade constraint;
alter table ํ
์ด๋ธ๋ช
drop ์ ์ฝ์กฐ๊ฑด๋ช
;
alter table dept01 drop primary key;
6-3. ์ ์ฝ์กฐ๊ฑด ์์ ๋นํ์ฑํ
alter table emp01 disable constraint emp01_deptno_fk;
6-4. ์ ์ฝ์กฐ๊ฑด ํ์ฑํ
alter table emp01 enable constraint emp01_deptno_fk;
*/
1. ๋์
๋๋ฆฌ table ๊ฒ์
select * from user_constraints;
desc user_constraints;
--2. ์ฌ์ฉ์ ์ ์ ์ ์ฝ ์กฐ๊ฑด๋ช
๋ช
์ํ๊ธฐ
/*DB์์ฒด ๊ธฐ๋ฅ์ ์ค๋ณต ๋ถํ ์ฑ๊ฒฉ์ ์
ํ
์ฌ์ฉ์๋ค์ด ์ค๋ณต ๋ฐ์ดํฐ insert ์๋์์ error๋ฐ์
๋ฐฉ๋ฒ 1 : ์ปฌ๋ผ ์ ์ธ์ ํ์
๋ค๋จ์
comstraint ์ ์ฝ์กฐ๊ฑด ๋ช
์ ์ ์ฝ์กฐ๊ฑดํ์
constraint emp-2_empno_nn not null
emp02_empno__nn : ์ฌ์ฉ์๋ค์ด ์ค์ ํ ์ด๋ฆ (๋๊ตฌ๋ ์ ์ ์๋ ์ด๋ฆ์ผ๋ก ์ค์ ๊ถ์ฅ)
emp02 table์ empno ์ปฌ๋ผ์ not null์ด์ด์ผ ํจ
not null : ์ ์ฝ์กฐ๊ฑด ํ์
*/
--์ฐธ๊ณ : not null์ ํด๋นํ๋ ๊ฒฝ์ฐ์ ํํด์๋ง ์ ์ฝ์กฐ๊ฑด๋ช
๋ช
์ ์ํด๋ ๊ด์ฐฎ์
--empno ๊ฐ์ ์ค๋ณต์ ํ์ฉ, ์๋ฏธ๊ฐ ์์.. ์๋ชป๋ ์ค๊ณ
drop table emp02;
create table emp02(
empno number(4) constraint emp02_empno_nn not null,
ename varchar2(10)
);
select constraint_name, constraint_type, table_name
from user_constraints where table_name='EMP02';
-- 2-2. ์ปฌ๋ผ ๋ ๋ฒจ ๋จ์์ unique
--empno์ unique ์ ์ฝ์กฐ๊ฑด ์ค์
drop table emp01;
create table emp01(
empno number(4) constraint emp01_empno_u unique,
ename varchar2(20) not null
);
insert into emp01 values (1, 'tester');
insert into emp01 values (1, 'tester2');
ORA-00001: unique constraint (SCOTT.EMP01_EMPNO_U) violated
-- 2-3. table ๋ ๋ฒจ ๋จ์์ ์ ์ฝ์กฐ๊ฑด ์ค์ ๋ฐฉ์?
drop table emp01;
create table emp01(
empno number(4),
ename varchar2(20) not null,
constraint emp01_empno_u unique(empno)
);
insert into emp01 values (1, 'tester');
insert into emp01 values (1, 'tester2');
ORA-00001: unique constraint (SCOTT.EMP01
--3. ์ฌ์ฉ์ ์ ์ ์ ์ฝ์กฐ๊ฑด๋ช
์ค์ ํ ์๋ฐฐ์ ์ถ๋ ฅ๋๋ ๋ฉ์ธ์ง์ ์ฌ์ฉ์์ ์ ์ ์ฝ์กฐ๊ฑด๋ช
-- ํ์ธ ๊ฐ๋ฅ : not null์ ์ ์ธํ ์ ์ฝ์กฐ๊ฑด๋ช
์ ์๋ฌ ๋ฐ์์ ๊ฐ์์ ์ธ ํ์ธ ๊ฐ๋ฅ
drop table emp02;
create table emp02(
empno number(4) constraint emp02_empno_nn not null,
ename varchar2(10)
);
insert into emp02 values(1, 'tester');
insert into emp02 values(1, 'tester');
-- not null์์๋ ๋ฐ์ดํฐ๋ฅผ null๋ก insert ์๋์ ๋ฐ์๋ ์๋ฌ4
--ORA-01400: cannot insert NULL into ("SCOTT"."EMP02"."EMPNO")
insert into emp02 (ename)values('tester');
--4. ์ ์ฝ์กฐ๊ฑด๋ช
์ ์ค๋ผํด ์์ง์ด ์๋์ ์ผ๋ก ์ง์
-- ์๋ฌ ๋ฐ์์ SYS_xxxx๋ก ์ถ๋ ฅ๋จ
drop table emp02;
create table emp02(
empno number(4) unique,
ename varchar2(10)
);
select constraint_name, constraint_type, table_name
from user_constraints where table_name='EMP02';
insert into emp02 values(1, 'tester');
insert into emp02 values(1, 'master');
-- not null์ ์ ์ธํ ๋ชจ๋ ์ ์ฝ์กฐ๊ฑด์ ๊ฐ๊ธ์ ์ ์ฝ์กฐ๊ฑด๋ช
์ ์ฌ์ฉ์๋ค์ด ๋ช
์ ๊ถ์ฅ
-- ๋ช
๋ช
๊ท์น : table๋ช
๊ฑธ๋ผ๋ช
์ ์ฝ์กฐ๊ฑด ์กฐํฉ
--5. pk์ค์ : ์ ์ธ ์์น์ ๋ฐ๋ฅธ ๊ตฌ๋ถ ํ์ต
/* table ์์ฑ์ ์ฌ์ฉ์ ์ ์ ์ ์ฝ์กฐ๊ฑด๋ช
์ ๋ช
์ํ์ง ์์ ๊ฒฝ์ฐ ์ค๋ผํด์ SYS_xxx๋ก ์๋ ์์ฑ
*/
drop table emp02;
create table emp02(
empno number(4) primary key,
ename varchar2(20) not null
);
insert into emp02 values(1, 'tester');
-- ์ค๋ณต ๋ถํ : insert into emp02 values(1, 'master');
-- ORA-00001: unique constraint (SCOTT.SYS_C007009) violated
-- ์ ์ฝ ์กฐ๊ฑด๋ช
๋ช
์ํ๋ฉด์ table ์์ฑ
drop table emp02;
-- ์ปฌ๋ผ ๋ ๋ฒจ ๋จ์์ ์ ์ฝ์กฐ๊ฑด ์ค์ ๋ฐฉ์
create table emp02(
empno number(4) constraint emp02_empno_pk primary key,
ename varchar2(20) not null
);
insert into emp02 values(1, 'tester');
-- insert into emp02 values(1, 'master');
-- ORA-00001: unique constraint (SCOTT.EMP02_EMPNO_PK) violated
--table ๋ ๋ฒจ ๋จ์์ ์ ์ฝ์กฐ๊ฑด ์ค์ ๋ฐฉ์
--6. ์ธ๋ํค[์ฐธ์กฐํค]
-- ์ด๋ฏธ ์ ์ฝ ์กฐ๊ฑด์ด ์ค์ ๋ dept table์ pk์ปฌ๋ผ์ธ deptno๊ฐ์ ๊ธฐ์ค์ผ๋ก emp02์ deptno์๋ ๋ฐ์(์ฐธ์กฐํค, ์ธ๋ํค, fk)
drop table emp02;
create table emp02(
empno number(4) constraint emp02_empno_pk primary key,
ename varchar2(20) not null,
deptno number(2) constraint emp02_deptno_fk references dept(deptno)
);
insert into emp02 values(1, 'SMITH', 10);
-- empno๊ฐ ์ค๋ณต ๋ฐ๋ผ์ pk์๋ฐฐ
insert into emp02 values(1, 'SMITH', 10);
-- ๋ถ๋ชจ table์๋ ๋ฏธ ์กด์ฌํ๋ ๋ฐ์ดํฐ๋ก insert ์๋ ๋ฐ๋ผ์ fk์๋ฐฐ
insert into emp02 values(11, 'SMITH2', 50);
/*
CO : constraint_type
P : PK ์๋ฏธ emp02_empno_pk
R : FK ์๋ฏธ emp02_deptno_fk
C : constraint์ ์ฝ์ - ์ด๋ฆ ๋ฏธ ๋ถ์ฌ์ ์ฆ not null์ ์๋ฏธ
CONSTRAINT_NAME CO TABLE_NAME
------------------ -------- -------
PK_DEPT P DEPT
PK_EMP P EMP
FK_DEPTNO R EMP
SYS_C007012 C EMP02
EMP02_EMPNO_PK P EMP02
EMP02_DEPTNO_FK R EMP02
--7. 6๋ฒ์ ๋ด์ฉ์ table ๋ ๋ฒจ ๋จ์๋ก ์ค์ ํด ๋ณด๊ธฐ
/* ์ฐธ์กฐํค ์ค์ ์ ํ์์ ๋ณด : ์ฃผ table๊ณผ ์ฐธ์กฐํ๋ ค๊ณ ํ๋ ์ฃผ table ์ ์ปฌ๋ผ๋ช
ํ์
(dept, deptno)
ํ table์์ ์ด๋ค ์ปฌ๋ผ์ ์ฐธ์กฐ๋ฐ์ ๊ฒ์ธ์ง
(emp02, deptno)
*/
drop table emp02;
create table emp02(
empno number(4),
ename varchar2(20) not null,
deptno number(2),
constraint emp02_empno_pk primary key (empno),
constraint emp02_deptno_fk references dept(deptno)
)
insert into emp02 values(1, 'tester', 10);
-- ์ค๋ฅ : ์ค๋ณต insert into emp02 values(1, 'tester', 10);
-- ์ค๋ฅ : insert into emp02 values(2, 'master', 60);
select * from emp02;
--8. emp01๊ณผ dept01 table ์์ฑ
drop table dept01;
drop table emp01;
create table dept01 as select * from dept;
create table emp01 as select * from emp;
select table_name, constraint_type, constraint_name
from user_constraints
where table_name='DEPT01';
--9. ์ด๋ฏธ ์กด์ฌํ๋ table์ ์ ์ฝ์กฐ๊ฑด ์ถ๊ฐํ๋ ๋ช
๋ น์ด
select table_name, constraint_type, constraint_name
from user_constraints
where table_name='DEPT01';
-- ? emp01์ ์ ์ฝ์กฐ๊ฑด ์ถ๊ฐํด ๋ณด๊ธฐ
select table_name, constraint_type, constraint_name
from user_constraints
where table_name='EMP01';
select table_name, constraint_type, constraint_name
from user_constraints
where table_name='DEPT01';
--10. ์ฐธ์กฐ ๋นํ๋ key์ ์ปฌ๋ผ์ด๋ผ ํ๋๋ผ๋ ์์ table์์ ๋ฏธ ์ฌ์ฉ๋๋ ๋ฐ์ดํฐ์ ํํด์๋
-- ์ญ์ ๊ฐ๋ฅ
-- emp01์ด ์ด๋ฏธ ์ฐธ์กฐํ๋ ๋ฐ์ดํฐ๊ฐ ์๋ dept01 table ์ญ์ ํด๋ณด๊ธฐ
--11.์ฐธ์กฐ๋๋ ์ปฌ๋ผ ๋ฐ์ดํฐ๋ผ ํ๋๋ผ๋ ์ญ์ ๊ฐ๋ฅํ ๋ช
๋ น์ด
-- *** ํ์
์์ ๋ถ๋์ดํ๊ฒ ์ด๋ฏธ ๊ฐ๋ฐ์ค์ table ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํด์ผ ํ ๊ฒฝ์ฐ๊ฐ ๊ฐํน ๋ฐ์
-- ์์ ์กด์ฌ ์ ๋ฌด ์์ ๋ฌด์ํ๊ณ ๋ถ๋ชจ table์ญ์
drop table emp01;
create table emp01(
name varchar2(20) not null,
age number(3) constraint emp01_age_ck check(age between 1 and 100)
);
insert into emp01 values('master', 10);
--12. check : if ์กฐ๊ฑด์๊ณผ ๊ฐ์ด ์ ์ฅ ์ง์ ์ ๋ฐ์ดํฐ์ ์ ํจ ์ ๋ฌด ๊ฒ์ฆํ๋ ์ ์ฝ์กฐ๊ฑด
-- age๊ฐ์ด 1~100๊น์ง๋ง DB์ ์ ์ฅ
drop table emp01;
insert into emp01 values('master', 10);
-- ์๋ฌ ๋ฐ์ : 1~100๊น์ง๋ง ์ ํจ
insert into emp01 values('master', 102);
select * from emp01;
select table_name, constraint_type, constraint_name
from user_constraints where table_name='EMP01';
/*
*/
-- 13.? gender๋ผ๋ ์ปฌ๋ผ์๋ ๋ฐ์ดํฐ๊ฐ M ๋๋(or) F๋ง ์ ์ฅ๋์ด์ผ ํจ
drop table emp01;
create table emp01(
gender varchar(1) constraint emp01_gender_ck check (gender in ('F', 'M'))
);
insert into emp01 values('master', 'F');
-- ์ค๋ฅ : insert into emp01 values('master', 'T');
select * from emp01;
--14. default : insert์์ ๋ฐ์ดํฐ๋ฅผ ์๋ตํด๋ ์๋์ผ๋ก db์ ์ ์ฅ๋๋ ๊ธฐ๋ณธ๊ฐ
drop table emp01;
insert into emp01 (id) values('master');
insert into emp01 values('tester', 'M');
select * from emp01;