반응형
--테이블 구조 변경
한번 만들어진 테이블이라고도 하더라도 테이블 구조를 변경 할 수 있다. 이 기능으로 인해 업무변화에 유연하게
대처할 수 있다.
--테이블 구조 변경 실습
create table links(
link_id serial primary key,
title varchar(512) not null,
url varchar(1024) not null unique
);
alter table links add column active boolean; --active 칼럼 추가
alter table links drop column active; --컬럼 삭제
alter table links rename column title to link_title; --컬럼 이름 변경
alter table links add column target varchar(10); --컬럼 추가
alter table links alter column target set default '_blank'; --타겟 컬럼의 디폴트 값을 설정
insert into links(link_title, url)
values('PostgreSQL Turorial','http://www.postgresqltutorial.com/'); --타겟 컬럼은 null로 행 입력됨
select * from links; --디폴트 값이 들어가져있음
alter table links add check (target in ('_self','_blank','_parent','_top')); --체크 제약 조건
insert into links(link_title, url, target)
values('PostgreSQL','http://www.postgresql.org/','whatever'); --체크 제약 조건에 없는 값 넣어보기
--SQL Error [23514]: ERROR: new row for relation "links" violates check constraint "links_target_check"
insert into links(link_title, url, target)
values('PostgreSQL','http://www.postgresql.org/','_self'); --들어감
반응형
'DataBase' 카테고리의 다른 글
[SQL]칼럼 추가 (0) | 2021.09.25 |
---|---|
[SQL]테이블 이름 변경 (0) | 2021.09.25 |
[SQL]CTALS (0) | 2021.09.25 |
[SQL] 데이터 타입 (0) | 2021.09.15 |
[SQL] 외부 데이터 넣기 IMPORT, 출력하기 EXPORT (0) | 2021.09.15 |
댓글