국비교육 33일차 Oracle : 제약조건 - 제약조건 이어서, 조회 방법, 추가/삭제/수정/변경
constraint 제약명 foreign key(제약컬럼명) references 부모테이블명(식별자컬럼명) on delete cascade
constraint 제약명 foreign key(제약컬럼명) references 부모테이블명(식별자컬럼명) on delete set null
constraint 제약명 check(status in(0,1))
alter table 테이블명
modify 컬럼명 null;
alter table 테이블명
modify 컬럼명 not null;
select *
from user_constraints
where table_name = '테이블명';
select *
from user_cons_columns
where table_name = '테이블명';
select A.constraint_name, A.constraint_type, A.search_condition, A.r_constraint_name,
A.status, A.index_name, B.column_name, B.position
from user_constraints A JOIN user_cons_columns B
on A.constraint_name = B.constraint_name
where A.table_name = '테이블명';
select A.constraint_name, A.constraint_type, A.search_condition, A.r_constraint_name,
A.status, A.index_name, B.column_name, B.position
from user_constraints A JOIN user_cons_columns B
on A.constraint_name = B.constraint_name
where A.table_name = '테이블명' and constraint_type = 'R';
-- r_constraint_name 참조받는 부모테이블의 제약 조건
-- PK_TBL_NEW_MEMBER_MEMBER_ID 회원의 멤버아이디 테이블 참조
-- 교체
alter table 테이블명
modify 컬럼명 null;
-- 삭제
delete from 테이블명
where 컬럼명= '값';
-- 업데이트
update 테이블명 set 컬럼명 = null
where 컬럼명 = '값';
alter table 테이블명 add constraint 제약조건명 primary key(컬럼명);
alter table 테이블명 add constraint 제약조건명 unique(컬럼명);
alter table 테이블명 add constraint 제약조건명 check( ... );
alter table 테이블명 add constraint 제약조건명 foreign key(컬럼명) references 부모테이블명(식별자컬럼명);
alter table 테이블명 add constraint 제약조건명 foreign key(컬럼명) references 부모테이블명(식별자컬럼명) on delete casecade;
alter table 테이블명 add constraint 제약조건명 foreign key(컬럼명) references 부모테이블명(식별자컬럼명) on delete set null;
alter table 테이블명
rename constraint 현재사용중인제약조건명 to 새로운제약조건명;
alter table 테이블명 drop constraint 제약조건명;
-- NOT NULL 제약의 경우, 위의 방법과 아래 방법 모두 사용 가능
alter table 테이블명 modify 컬럼명 null;
-- 어떤 테이블에 primary key 제약조건을 삭제할 경우에는 위의 방법처럼 해도 되고, 또는 아래처럼 해도 된다.
alter table 테이블명 drop primary key;
-- primary key는 유일하기 때문이다.
alter table 테이블명 drop constraint 제약조건명;
-- 테이블명 이(가) 변경되었습니다.
alter table 테이블명 add constraint 제약조건명 check( 컬럼명 in (바꿀 내용) );
-- Table 테이블명 이(가) 변경되었습니다.
-- 비활성화
alter table 테이블명 disable constraint 제약조건명;
-- 활성화
alter table 테이블명 enable constraint 제약조건명;