Programming/SQL

[SQL] 제약조건의 추가 및 제거

reeme 2020. 12. 16. 15:23

제약조건의 추가 및 제거

테이블을 생성한 후 제약조건을 추가하거나 제거할 때 활용

point) 제약조건 이름을 지정해 둬야 추가나 제거가 쉬움

 

추가

ALTER TABLE 테이블명 ADD [CONSTRAINT 제약조건명 ] 제약조건 ( 속성명 )

제거

ALTER TABLE 테이블명 DROP CONSTRAINT 제약조건명  

 

 

primary key 추가

alter table 테이블명 add constraint primary key (컬럼명);

 

foreign key 추가

alter table 테이블명 add constraint 제약조건이름 foreign key (컬럼명) references 부모테이블명 (pk컬럼명) on delete cascade / on update cascade;

 

not null 추가

alter table 테이블명 modify 컬럼명 자료형 constraint 제약조건명 not null;

 

제약조건 삭제

alter table 테이블명 drop constraint 제약조건명;

 

제약조건 삭제 (foreign key)

alter table 테이블명 foreign key 제약조건명;