DB/Oracle
[Oracle] 오라클 제약조건 Primary key(기본키)
mndev
2024. 2. 7. 09:41
기본키(Primary Key)
- 데이터의 품질을 높이기 위해 특정 컬럼에 중복된 데이터와
null
값을 입력하지 못하게 하는 방법 - 테이블을 생성할 때 같이 정의
- 테이블당 하나만 정의 가능
-- 컬럼 생성과 primary key제약 추가
-- null은 입력할 수 없음
create table dept2
(deptno number(10) constraint dept2_deptno_pk primary key,
dname varchar(14),
loc varchar(14));
생성된 제약을 확인
select a.constraint_name, a.constraint_type, b.column_name
from user_constraints a, user_cons_columns b
where a.table_name = 'dept2'
and a.constraint_name = b.constraint_name;
기본키 추가 및 수정
테이블 생성시 제약을 생성하지 않고 생성했다면, alter
명령어를 사용해 테이블을 수정할 수 있음
create table dept2
(deptno number(10),
dname varchar(13),
loc varchar(10));
alter table dept2 add constraint dpet2_deptno_pk primary key(deptno);
중복과 NULL 허용 여부
PRIMARY: 중복x, NULL x
UNIQUE: 중복x, NULL O
NOT NULL: 중복O, NULL x