Crescendo Code
Create, Alter 명령어로 제약 조건 (Constraint) 넣기 본문
◆ 테이블 생성(Create) 시 제약조건 부여
1. not null
- 컬럼 항을 추가할 때, 해당하는 컬럼 항의 데이터 타입 뒤에 not null 기재
2. primary
- 컬럼 항을 추가할 때, 해당하는 컬럼 항의 데이터 타입 뒤에 primary key 기재
- 컬럼 항을 추가할 때, 마지막 항으로 constraint primary key (컬럼명) 기재
3. unique
- 컬럼 항을 추가할 때, 해당하는 컬럼 항의 데이터 타입 뒤에 unique 기재
- 컬럼 항을 추가할 때, 마지막 항으로 constraint unique (컬럼명) 기재
4. foreign key
- 컬럼 항을 추가할 때, 마지막 항으로 constraint foreign key (컬럼명) references 참조_테이블명 (참조_컬럼명) 기재
5. 복합키 ( primary key, unique )
- 컬럼 항을 추가할 때, 마지막 항으로 constraint unique/primary key (컬럼명1, 컬럼명2 ... ) 기재
◆ 테이블 수정(Alter) 으로 제약조건 부여
1. not null 부여
- alter table 테이블명 modify 컬럼명 컬럼_데이터타입 not null;
2. null 부여
- alter table 테이블명 modify 컬럼명 컬럼_데이터타입;
- alter table 테이블명 modify 컬럼명 컬럼_데이터타입 null;
3. primary key 부여
- alter table 테이블명 modify 컬럼명 컬럼_데이터타입 primary key;
- alter table 테이블명 add constraint primary key (컬럼명);
4. unique 부여
- alter table 테이블명 modify 컬럼명 컬럼_데이터타입 unique;
- alter table 테이블명 add constraint unique (컬럼명);
5. foreign key 부여
- alter table 테이블명 add foreign key (컬럼명) references 참조_테이블명 (참조_컬럼명)
- alter table 테이블명 add constraint foreign key (컬럼명) references dept_c (참조_컬럼명);
6. primary key 제거
- alter table 테이블명 drop constraint primary key;
7. unique 삭제
- alter table 테이블명 drop constraint 컬럼명;
8. foreign key 제거
- alter table 테이블명 drop constraint (참조키_이름);
- alter table 테이블명 drop foreign key (참조키_이름);
9. 완전한 foreign key 삭제
- alter table 테이블명 drop index 컬럼명;
'데이터베이스 > MariaDB' 카테고리의 다른 글
Window 환경에서의 MariaDB 설치 (0) | 2023.03.29 |
---|---|
데이터베이스 MariaDB의 유래와 특징 (0) | 2023.03.29 |