반응형

/********************************************************************************************
-- Title : [2k] 후보키/대체키의 Relation Point 잡기
-- Reference : dbrang.tistory.com
-- Key word : 후보키, 대체키, 릴레이션, relation
********************************************************************************************/
drop table t1,t2
create table t1 (a int not null primary key, b int not null)
create table t2 (c int not null primary key, d int null)

insert into t1 values (1,1)
insert into t1 values (2,1)
insert into t2 values (100,1)
insert into t2 values (200,2)

-- t1.a 키 있음.
alter table t2 -- 성공
add constraint fk_t2 foreign key (d) references t1(a)

alter table t2
drop constraint fk_t2

-- t2.d 키 없음
alter table t1 -- 키가 없어 에러
add constraint fk_t1 foreign key (a) references t2(d)

create unique index ix_t2
on t2(d)

alter table t1 -- 성공(그냥 NC로 인덱싱하면 Relation 안된다.)
add constraint fk_t1 foreign key (a) references t2(d)

반응형

+ Recent posts