[DB] ORACLE - 6์ผ


ORACLE



์˜ค๋ผํด 6์ผ

[ํ…Œ์ด๋ธ”์ƒ์„ฑ]

create table ํ…Œ์ด๋ธ”๋ช…(์ปฌ๋Ÿผ๋ช…1 ์ปฌ๋Ÿผํƒ€์ž… [์ œ์•ฝ์กฐ๊ฑด],
์ปฌ๋Ÿผ๋ช…2 ์ปฌ๋Ÿผํƒ€์ž… [์ œ์•ฝ์กฐ๊ฑด],.....);


  • ๋ฌธ์ž๋กœ ์‹œ์ž‘(30์ž์ด๋‚ด) : ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž,์ˆซ์ž,ํŠน์ˆ˜๋ฌธ์ž( _ , $ , # ),ํ•œ๊ธ€
  • ์ค‘๋ณต๋˜๋Š” ์ด๋ฆ„์€ ์‚ฌ์šฉ ์•ˆ๋จ
  • ์˜ˆ์•ฝ์–ด(create, table, column๋“ฑ)์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค
  • ์ž๋ฃŒํ˜•
  • number : number(์ „์ฒด์ž๋ฆฌ,์†Œ์ˆ˜์ดํ•˜), number โ†’ ์ˆซ์žํ˜•(๊ฐ€๋ณ€ํ˜•)
  • int : ์ •์ˆ˜ํ˜• ์ˆซ์ž(๊ณ ์ •ํ˜•)
  • varchar/varchar2 : ๋ฌธ์ž,๋ฌธ์ž์—ด(๊ฐ€๋ณ€ํ˜•) โ†’ ์ตœ๋Œ€ 4000byte
  • char : ๋ฌธ์ž,๋ฌธ์ž์—ด(๊ณ ์ •ํ˜•) ==> 2000byte
  • date : ๋‚ ์งœํ˜•
  • clob : ๋ฌธ์ž์—ด => ์ตœ๋Œ€ 4GB
  • blob : ๋ฐ”์ด๋„ˆ๋ฆฌํ˜•(๊ทธ๋ฆผ,์Œ์•…,๋™์˜์ƒ..) ===> ์ตœ๋Œ€4GB


-์ œ์•ฝ์กฐ๊ฑด-

not null : ํ•ด๋‹น์ปฌ๋Ÿผ์— NULL์„ ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ํ•จ (์ปฌ๋Ÿผ)

unique : ํ•ด๋‹น์ปฌ๋Ÿผ ๋˜๋Š” ์ปฌ๋Ÿผ ์กฐํ•ฉ๊ฐ’์ด ์œ ์ผํ•˜๋„๋ก ํ•จ (์ปฌ๋Ÿผ,ํ…Œ์ด๋ธ”)

primary key : ๊ฐ ํ–‰์„ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ํ• ์ˆ˜ ์žˆ๋„๋กํ•จ (์ปฌ๋Ÿผ,ํ…Œ์ด๋ธ”)

references table(column) :

ํ•ด๋‹น ์ปฌ๋Ÿผ์ด ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” (๋ถ€๋ชจ)ํ…Œ์ด๋ธ”์˜ ํŠน์ • (์ปฌ๋Ÿผ,ํ…Œ์ด๋ธ”) ์ปฌ๋Ÿผ๊ฐ’๋“ค๊ณผ ์ผ์น˜ํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” NULL์ด ๋˜๋„๋ก ๋ณด์žฅํ•จ

check : ํ•ด๋‹น์ปฌ๋Ÿผ์— ํŠน์ • ์กฐ๊ฑด์„ ํ•ญ์ƒ ๋งŒ์กฑ์‹œํ‚ค๋„๋กํ•จ (์ปฌ๋Ÿผ, ํ…Œ์ด๋ธ”)

[์ฐธ๊ณ ] primary key = unique + not null

ex) 
idx           ์ผ๋ จ๋ฒˆํ˜ธ    primary key
id            ์•„์ด๋””        unique
name           ์ด๋ฆ„        not null
phone          ์ „ํ™”๋ฒˆํ˜ธ
address         ์ฃผ์†Œ   
score         ์ ์ˆ˜        check
subject_code   ๊ณผ๋ชฉ์ฝ”๋“œ     
hire_date      ์ž…ํ•™์ผ      ๊ธฐ๋ณธ๊ฐ’(์˜ค๋Š˜๋‚ ์งœ)
marriage       ๊ฒฐํ˜ผ    	check  


-์ œ์•ฝ์กฐ๊ฑดํ™•์ธ-

constraint_name:์ด๋ฆ„

constraint_type:์œ ํ˜•

โ€‹ p:primary key

โ€‹ u:unique

โ€‹ r:reference

โ€‹ c:check, not null

search_condition : check์กฐ๊ฑด ๋‚ด์šฉ

r_constraint_name : ์ฐธ์กฐํ…Œ์ด๋ธ”์˜ primary key ์ด๋ฆ„

delete_rule : ์ฐธ์กฐํ…Œ์ด๋ธ”์˜ primary key ์ปฌ๋Ÿผ์ด ์‚ญ์ œ๋ ๋•Œ ์ ์šฉ๋˜๋Š” ๊ทœ์น™

โ€‹ (no action, set null, cascade๋“ฑ)


-์‚ญ์ œ RULE-

on delete cascade : ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ณ ,ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ์ดํ„ฐ๋„ ์‚ญ์ œ

on delete set null : ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ณ ,ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” NULL๋กœ ๋ฐ”๊ฟˆ

on delete restricted : ์‚ญ์ œ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋ฉด ์‚ญ์ œํ•  ์ˆ˜ ์—†์Œ(๊ธฐ๋ณธ๊ฐ’)


-์ˆ˜์ • RULE-

on update cascade : ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด,ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ์ดํ„ฐ๋„ ์ˆ˜์ •


[ํ…Œ์ด๋ธ”์ˆ˜์ •]

-๊ตฌ๋ฌธ-

alter table ํ…Œ์ด๋ธ”๋ช… 
     add  ์ปฌ๋Ÿผ๋ช… ๋ฐ์ดํ„ฐํƒ€์ž… [์ œ์•ฝ์กฐ๊ฑด]
     add  constraint ์ œ์•ฝ์กฐ๊ฑด๋ช… ์ œ์•ฝ์กฐ๊ฑดํƒ€์ž…(์ปฌ๋Ÿผ๋ช…)
     modify ์ปฌ๋Ÿผ๋ช… ๋ฐ์ดํ„ฐํƒ€์ž… 
     drop  column ์ปฌ๋Ÿผ๋ช… [cascade constraints]
     drop  primary key [cascade] | union (์ปฌ๋Ÿผ๋ช…,.....) [cascade] .... | constraint 
     ์ œ์•ฝ์กฐ๊ฑด๋ช… [cascade]


-์ด๋ฆ„๋ณ€๊ฒฝ-

alter table ๊ธฐ์กดํ…Œ์ด๋ธ”๋ช… rename to ์ƒˆํ…Œ์ด๋ธ”๋ช…
rename ๊ธฐ์กดํ…Œ์ด๋ธ”๋ช… to ์ƒˆํ…Œ์ด๋ธ”๋ช…
alter table ํ…Œ์ด๋ธ”๋ช… rename column ๊ธฐ์กด์ปฌ๋Ÿผ๋ช… to ์ƒˆ์ปฌ๋Ÿผ๋ช…
alter table ํ…Œ์ด๋ธ”๋ช… rename constraint ๊ธฐ์กด์ œ์•ฝ์กฐ๊ฑด๋ช… to ์ƒˆ์ œ์•ฝ์กฐ๊ฑด๋ช…


[ํ…Œ์ด๋ธ”๋ณต์‚ฌ]

  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ํ…Œ์ด๋ธ”์ƒ์„ฑ๋ฐ ํ–‰(๋ ˆ์ฝ”๋“œ)๋ณต์‚ฌ
  • ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋ณต์‚ฌํ•œ๊ฒฝ์šฐ not null์„ ์ œ์™ธํ•œ ์ œ์•ฝ์กฐ๊ฑด์€ ๋ณต์‚ฌ์•ˆ๋จ

-๊ตฌ๋ฌธ -

create table ํ…Œ์ด๋ธ”๋ช…([์ปฌ๋Ÿผ๋ช…1,์ปฌ๋Ÿผ๋ช…2.....]) as ์„œ๋ธŒ์ฟผ๋ฆฌ 

-๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌ -

create table ํ…Œ์ด๋ธ”๋ช…1 
as select * from ํ…Œ์ด๋ธ”๋ช…2 where 1=0


[์‹œํ€€์Šค]

  • ์ˆœ์ฐจ์ ์œผ๋กœ ์ •์ˆ˜๊ฐ’์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฐ์ฒด
create sequence ์‹œํ€€์Šค๋ช…
[increment by ์ฆ๊ฐ€๊ฐ’] [start with ์‹œ์ž‘๊ฐ’]
[maxvalue ์ตœ๋Œ€๊ฐ’ | minvalue ์ตœ์†Œ๊ฐ’]
[cycle | nocycle]
[cache | nocache]


increment by ์ฆ๊ฐ€๊ฐ’ : ์ฆ๊ฐ€/๊ฐ์†Œ ๊ฐ„๊ฒฉ(๊ธฐ๋ณธ๊ฐ’ : 1)

start with : ์‹œ์ž‘๋ฒˆํ˜ธ(๊ธฐ๋ณธ๊ฐ’ : 1)

maxvalue / minvalue : ์‹œํ€€์Šค์˜ ์ตœ๋Œ€/์ตœ์†Œ๊ฐ’์ง€์ •

cycle/nocycle : ์ตœ๋Œ€/์ตœ์†Œ๊ฐ’์— ๋„๋‹ฌ ์‹œ ๋ฐ˜๋ณต์—ฌ๋ถ€๊ฒฐ์ •

cache / nocache : ์ง€์ •ํ•œ ์ˆ˜๋Ÿ‰๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์„ฑ์—ฌ๋ถ€๊ฒฐ์ • (์ตœ์†Œ๊ฐ’ : 2, ๊ธฐ๋ณธ๊ฐ’ : 20)


[ insert ]

  • ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ(์ƒˆ๋กœ์šดํ–‰)์ถ”๊ฐ€

  • insert into ํ…Œ์ด๋ธ”๋ช… [ (column1, column2, .....)]  values (value1,value2,.....)
    
    • column๊ณผ values์˜ ์ˆœ์„œ์ผ์น˜
    • column๊ณผ values์˜ ๊ฐœ์ˆ˜ ์ผ์น˜


[ update ]

  • ํ…Œ์ด๋ธ”์— ํฌํ•จ๋œ ๊ธฐ์กด ๋ฐ์ดํ„ฐ์ˆ˜์ •

  • ์ „์ฒด ๋ฐ์ดํ„ฐ ๊ฑด์ˆ˜(ํ–‰์ˆ˜)๋Š” ๋‹ฌ๋ผ์ง€์ง€ ์•Š์Œ

  • ์กฐ๊ฑด์— ๋งž๋Š” ํ–‰(๋˜๋Š” ์—ด)์˜ ์ปฌ๋Ÿผ๊ฐ’์„ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค

  • update ํ…Œ์ด๋ธ”๋ช… set ์ปฌ๋Ÿผ๋ช…1=value1, ์ปฌ๋Ÿผ๋ช…2=value2 ..... [where ์กฐ๊ฑด์ ˆ]
    
    • where ์ด ์ƒ๋žต์ด ๋˜๋ฉด ์ „์ฒดํ–‰์ด ๊ฐฑ์‹ 

    • set์ ˆ์€ ์„œ๋ธŒ์ฟผ๋ฆฌ์‚ฌ์šฉ๊ฐ€๋Šฅ, default์˜ต์…˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ


[ delete ]

  • ํ…Œ์ด๋ธ”์— ํฌํ•จ๋œ ๊ธฐ์กด๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ

  • ํ–‰ ๋‹จ์œ„๋กœ ์‚ญ์ œ๋˜๋ฏ€๋กœ ์ „์ฒดํ–‰์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง

  • delete [from] ํ…Œ์ด๋ธ”๋ช… [where ์กฐ๊ฑด์ ˆ];
    
    • where์„ ์ƒ๋žตํ•˜๋ฉด ์ „์ฒดํ–‰์ด ์‚ญ์ œ๋จ
    • ๋ฐ์ดํ„ฐ๋Š” ์‚ญ์ œ๋˜๊ณ  ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ์œ ์ง€๋จ


[ transaction์ฒ˜๋ฆฌ ]

  • ์ผ์˜ ์‹œ์ž‘๊ณผ ๋์ด ์™„๋ฒฝํ•˜๊ฒŒ ๋งˆ๋ฌด๋ฆฌ(commit)

  • ์ฒ˜๋ฆฌ๋„์ค‘ ์ธํ„ฐ๋ŸฝํŠธ(interrupt:์žฅ์• )๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋˜๋Œ์•„์˜ด(rollback)




ex1 ) ํ…Œ์ด๋ธ” : test

create table test(
id number(5),
name char(10),
address varchar2(50));


ex2 ) ํ…Œ์ด๋ธ”๋ช… : user1

create table user1(
idx   number primary key,
id   varchar2(10) unique,
name  varchar2(10) not null,
phone  varchar2(15),
address varchar2(50),
score  number(6,2) check(score >=0 and score <= 100),
subject_code number(5),
hire_date date default sysdate,
marriage  char(1) default 'N' check(marriage in('Y','N')));


ex3 ) ์ œ์•ฝ์กฐ๊ฑดํ™•์ธ

select constraint_name, constraint_type
from user_constraints
where table_name='USER1';


ex4 ) ํ…Œ์ด๋ธ”๋ช… : user2

create table user2(
idx   number    constraint PKIDX primary key,
id   varchar2(10) constraint UNID unique,
name  varchar2(10) constraint NOTNAME not null,
phone  varchar2(15),
address varchar2(50),
score  number(6,2)  constraint CKSCORE check(score >=0 and score <= 100),
subject_code number(5),
hire_date date default sysdate,
marriage  char(1) default 'N' constraint CKMARR check(marriage in('Y','N')));


ex5 ) ์ œ์•ฝ์กฐ๊ฑดํ™•์ธ

select constraint_name, constraint_type
from user_constraints
where table_name='USER2';


ex6 ) ์ถ”๊ฐ€

insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(1,'aaa','kim','010-000-0000','์„œ์šธ',75,100,'2010-08-01','Y');

insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(1,'aaa','kim','010-000-0000','์„œ์šธ',75,100,'2010-08-01','Y'); 
โ†’ ๋ฌด๊ฒฐ์„ฑ์ œ์•ฝ์กฐ๊ฑด์— ์œ„๋ฐฐ(์ด์œ : idx 1 ์ค‘๋ณต) 

insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'aaa','kim','010-000-0000','์„œ์šธ',75,100,'2010-08-01','Y'); 
โ†’ ๋ฌด๊ฒฐ์„ฑ์ œ์•ฝ์กฐ๊ฑด์— ์œ„๋ฐฐ(์ด์œ : id aaa ์ค‘๋ณต) 

insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','','010-000-0000','์„œ์šธ',75,100,'2010-08-01','Y');
โ†’ NULL์„ ("HR"."USER1"."NAME") ์•ˆ์— ์‚ฝ์ž…ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค

insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','lee','010-000-0000','์„œ์šธ',120,100,'2010-08-01','Y');
โ†’ ์ฒดํฌ์ œ์•ฝ์กฐ๊ฑด์— ์œ„๋ฐฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค(์ด์œ : score๊ฐ€ 0~100์‚ฌ์ด์˜ ์ˆ˜ ์ด์–ด์•ผํ•จ)

insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','lee','010-000-0000','์„œ์šธ',75,100,'2010-08-01','K');
โ†’ ์ฒดํฌ์ œ์•ฝ์กฐ๊ฑด์— ์œ„๋ฐฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค(์ด์œ :marriage๊ฐ€ Y ๋˜๋Š” N์ด์–ด์•ผํ•จ) 

insert into user1(idx,id,name,phone,address,score,subject_code,hire_date,marriage)
values(2,'bbb','lee','010-000-0000','์„œ์šธ',75,100,'2010-08-01','N');


ex7 ) ํ…Œ์ด๋ธ” ๋ชฉ๋ก ํ™•์ธ

select * from tab;


ex8 ) ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉํ™•์ธ

select * from user1;
select * from user2;


ex9 ) ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐํ™•์ธ

desc user1; (= describe user1)


ex10 ) ์ œ์•ฝ์กฐ๊ฑด์˜ ์ด๋ฆ„์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์€๊ฒฝ์šฐ

select constraint_name, constraint_type
from user_constraints
where table_name='USER1';


ex11 ) ์ œ์•ฝ์กฐ๊ฑด์˜ ์ด๋ฆ„์„ ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ

select constraint_name, constraint_type
from user_constraints
where table_name='USER2';


ex12 ) ํ…Œ์ด๋ธ”๋ช… ๋ณ€๊ฒฝ : test โ†’ user3

alter table test rename to user3;


ex13 ) ์ปฌ๋Ÿผ์ถ”๊ฐ€ : user3 โ†’ phone varchar2(15)

alter table user3 add phone varchar2(15);
desc user3;


ex14 ) ์ œ์•ฝ์กฐ๊ฑด์ถ”๊ฐ€ : user3 โ†’ id์— unique ,์ œ์•ฝ์กฐ๊ฑด๋ช… UID2

alter table user3 add constraint UID2 unique(id);
select constraint_name, constraint_type
from user_constraints
where table_name='USER3';


ex15 ) ์ปฌ๋Ÿผ์ถ”๊ฐ€ : user3 โ†’ no number (PK์„ค์ •)

alter table user3 add no number primary key;
desc user3


ex16 ) ๊ตฌ์กฐ๋ณ€๊ฒฝ : user3 โ†’ name char(10) ๋ฅผ varchar2(10)๋กœ ๋ฐ”๊ฟˆ

alter table user3 modify name varchar2(10);
desc user3


ex17 ) ์ปฌ๋Ÿผ์‚ญ์ œ : user3 โ†’ address

alter table user3 drop column address;
desc user3


ex18 ) ํ…Œ์ด๋ธ”์‚ญ์ œ / ํœด์ง€ํ†ต๋น„์šฐ๊ธฐ: user3

drop table user3;
purge recyclebin; โ†’ ํœด์ง€ํ†ต ๋น„์šฐ๊ธฐ

drop table user3 purge; โ†’ ํœด์ง€ํ†ต์— ๋„ฃ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์‚ญ์ œ
flashback table user3 to before drop; โ†’ ํœด์ง€ํ†ต์—์„œ ๋˜์‚ด๋ฆฌ๊ธฐ


ex19 ) ์‹œํ€€์Šค์ƒ์„ฑ / ์‚ญ์ œ

create sequence idx_sql increment by 2 start with 1 maxvalue 9 cycle nocache;

select idx_sql.nextval from dual; โ†’ ๋‹ค์Œ ์‹œํ€€์Šค๊ฐ’ํ‘œ์‹œ(nextval)
select idx_sql.currval from dual; โ†’ ํ˜„์žฌ ์‹œํ€€์Šค๊ฐ’ํ‘œ์‹œ(currtval)
 
drop sequence idx_sql;


ex20 ) ํ…Œ์ด๋ธ”์ƒ์„ฑ๊ณผ ์‹œํ€€์Šค์ ์šฉ

create table book(
no number primary key,
subject varchar2(50),
price number,
year date);

create sequence no_seq increment by 1 start with 1 nocycle nocache;

insert into book(no,subject,price,year)values(no_seq.nextval,'์˜ค๋ผํด ๋ฌด์ž‘์ • ๋”ฐ๋ผํ•˜๊ธฐ',10000,sysdate);
insert into book(no,subject,price,year)values(no_seq.nextval, '์ž๋ฐ” 3์ผ ์™„์„ฑ',15000,'2007-03-01');
insert into book values(no_seq.nextval, 'JSP ๋‹ฌ์ธ ๋˜๊ธฐ',18000,'2010-01-01');

select * from book;


ex21 ) ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋งŒ ๋ณต์‚ฌ

create table user3 as select * from user2 where 1=0;
desc user3;

select constraint_name, constraint_type, search_condition
from user_constraints
where table_name='USER3'; โ† not null์„ ์ œ์™ธํ•˜๊ณ ๋Š” ์ œ์•ฝ์กฐ๊ฑด์ด ๋ณต์‚ฌ ์•ˆ๋จ
             โ† not null์ œ์•ฝ์กฐ๊ฑด๋„ sys_*****๋กœ ๋ณต์‚ฌ๋จ


ex22 ) ํ…Œ์ด๋ธ”(idx โ†’ bunho, name โ†’ irum, address โ†’ juso) ์„ ๋ณต์‚ฌํ•˜๊ณ  id๊ฐ€ bbb์ธ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์‹œ์˜ค

์›๋ณธํ…Œ์ด๋ธ” : user1 / ์‚ฌ๋ณธํ…Œ์ด๋ธ” : user4

create table user4(bunho, irum, juso) 
as select idx, name, address from user1 where id='bbb';
select * from user4;


ex23 ) ํ…Œ์ด๋ธ” ์ƒ์„ฑํ›„ ํ–‰์ถ”๊ฐ€

ํ…Œ์ด๋ธ”๋ช… : dept

deptno number โ†’ ๊ธฐ๋ณธํ‚ค, ์ œ์•ฝ์กฐ๊ฑด๋ช…(DNO)

dname varcahr2(30) โ†’ ๋„ ํ—ˆ์šฉ์•ˆ๋จ, ์ œ์•ฝ์กฐ๊ฑด๋ช…(DNAME)

ํ…Œ์ด๋ธ”๋ช… : emp

empno number โ†’ ๊ธฐ๋ณธํ‚ค,์ œ์•ฝ์กฐ๊ฑด๋ช…(ENO)

ename varchar2(30) โ†’ ๋„ํ—ˆ์šฉ์•ˆ๋จ, ์ œ์•ฝ์กฐ๊ฑด๋ช…(ENAME)

deptno number โ†’ ์™ธ๋ž˜ํ‚ค, ์ œ์•ฝ์กฐ๊ฑด๋ช…(FKNO),

๋Œ€์ƒ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์ฐธ์กฐํ•˜๋Š”๋ฐ์ดํ„ฐ๋Š” NULL๋กœ ๋ฐ”๊ฟˆ

create table dept(

deptno number constraint DNO primary key,

dname varchar2(30) constraint DNAME not null);


create table emp(

empno number constraint ENO primary key,

ename varchar2(30) constraint ENAME not null,

deptno number, constraint FKNO foreign key(deptno) references dept on delete set null);


insert into dept(deptno,dname) values(10,'๊ฐœ๋ฐœ๋ถ€');

insert into dept(deptno,dname) values(20,'์˜์—…๋ถ€');

insert into dept(deptno,dname) values(30,'๊ด€๋ฆฌ๋ถ€');


insert into dept(dname) values(40,'๊ฒฝ๋ฆฌ๋ถ€'); โ†’ ORA-00913: ๊ฐ’์˜ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์Šต๋‹ˆ๋‹ค

insert into dept(deptno,dname) values(40,'๊ฒฝ๋ฆฌ๋ถ€');

 
insert into emp(empno, ename, deptno) values(100,'๊ฐ•ํ˜ธ๋™',10);

insert into emp(empno, ename, deptno) values(101,'์•„์ด์œ ',20);


insert into emp(empno, ename, deptno) values(102,'์œ ์žฌ์„',50); 

โ†’ 50๋ฒˆ๋ถ€์„œ ์—†์Œ(๋ฌด๊ฒฐ์„ฑ์ œ์•ฝ์กฐ๊ฑด์œ„๋ฐฐ)-๋ถ€๋ชจํ‚ค๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค


insert into emp(empno, ename, deptno) values(103,'์ดํšจ๋ฆฌ',40);

insert into emp(empno, ename) values(105,'์žฅ๋™๊ฑด');


insert into emp(ename, deptno) values('๊ณ ์†Œ์˜',10);

โ†’ primary key๋Š” NULLํ—ˆ์šฉ ์•ˆํ•จ
 

commit;


ex24 ) ์‚ญ์ œ

delete from dept;
select * from dept;
rollback;
select * from dept;

delete from dept where deptno=40;
select * from dept;
select * from emp;


ex25 ) ์ˆ˜์ •(update)

emp ํ…Œ์ด๋ธ” ์žฅ๋™๊ฑด ์‚ฌ์›์˜ ๋ถ€์„œ๋ฒˆํ˜ธ๋ฅผ 30์œผ๋กœ ์ˆ˜์ •ํ•˜์‹œ์˜ค

update emp set deptno=30 where ename='์žฅ๋™๊ฑด';
select * from emp;
commit;