컬럼 이름 변경

alter table [테이블명] change [변경전 컬럼] [변경후 컬럼] [컬럼 타입];

alter table article change user nickname varchar(30);

 

컬럼 제약 조건 추가

1) not null 추가 : alter table [테이블명] modify [컬럼명] [타입] [제약조건];

2) primary key(기본키) : alter table [테이블명] add primary key(컬럼명);

3) foreign key(외래키) : alter table [테이블명] add constraint [제약조건 이름] foreign key(컬럼명) reference [참조할 테이블](참조 테이블의 참조 컬럼);

// not null
alter table article modify id INT not null;


// 기본키
alter table article add primary key(id);

// 외래키 -> user 테이블의 기본키를 article 테이블 id 컬럼의 외래키로 추가
alter table article add constraint userid_fk foreign key(id) reference user(id);

 

컬럼 추가

alter table [테이블명] add column [컬럼명] [컬럼 타입];

alter table article add column username varchar(30);

 

컬럼 수정

alter table [테이블명] modify [컬럼명] [타입];

alter table article modify id INT;

 

컬럼 삭제

alter table [테이블명] drop column [컬럼명];

alter table article drop column id;

 

컬럼 순서 변경

alter table [테이블명] modify [컬럼명] [컬럼타입] after [앞 컬럼];

# username 뒤에 nickname 컬럼 추가

alter table article modify nickname varchar(30) after username;

오라클 테이블 구조 변경

  테이블을 생성하고 보면 필요한 컬럼을 생성하지 않았거나 컬럼의 데이터 타입이나 길이가 잘못되어 변경해야 할 경우가 생기고 불필요한 컬럼을 제거해야 할 경우도 생깁니다. 이럴 때 alter table 문을 사용하여 컬럼을 추가, 수정, 삭제할 수 있습니다.

컬럼 추가, 변경, 삭제 방법과 예시

<컬럼 추가>

alter table 테이블명 add 컬럼이름 데이터타입(길이) 제약조건;
예) alter table departmend add dno number(2) constraint department_nn not null;

<컬럼 변경>

alter table 테이블명 modify 컬럼이름 데이터타입(길이) 제약조건;
예) alter table departmend modify dno number(2) constraint department_nn not null;

<컬럼 삭제>

alter table 테이블명 drop culumn 컬럼이름;
예) alter table department drop culumn dno;

'DataBase > Oracle' 카테고리의 다른 글

[오라클] 테이블 생성 / 제약조건  (0) 2020.12.25

+ Recent posts