반응형

컬럼 이름 변경

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;
반응형

+ Recent posts