1장_2절 DDL(Data Definition Language : 데이터 정의어)

2절. DDL(Data Definition Language : 데이터 정의어)
1.데이터유형
1)Character(s) : char,char로 표현, 고정길이 문자열, 할당된 길이만큼 빈공간으로 저장됨
2)varchar(s) : varchar2,varchar로 표현, 가변길이 문자열, 공간이 가변적으로 저장됨
3)numeric : number,10가지타입으로 표현, 정수.실수등 숫자정보, (정수자리,소수자리)로 저장됨
4)date : 날짜.시간정보

​2.Create table
1)sql문
- create table 테이블명(
칼럼명1 데이터타입 default형식,
칼럼명2 데이터타입 default형식,
칼럼명3 데이터타입 default형식,
... ,
constraint 제약조건이름 제약조건
);
#주의사항
-table명은 단수형, 중복x, table명과 column명은 문자로 시작, 예약어사용x, '-'사용x

2)제약조건
-primary key(기본키) : unique key제약 + not null제약
-unique key(고유키) : column값이 행마다 다름
-not null : null값의 입력 금지
-check : 입력값의 범위 제한 by 논리식
-foreign key(외래키) : 

3)describe 테이블명;
-현재 존재하는 테이블 확인

3.Alter table
1)add column : 컬럼추가
-alter table 테이블명 add 추가할컬럼명 데이터타입;
ex) alter table player add address varchar2(80);

2)drop column : 컬럼삭제
-alter table 테이블명 drop column 삭제할컬럼명;
ex) alter table player drop column address;

3)modify column : 컬럼변경(데이터형식, 디폴트값, 제약조건)
-alter table 테이블명 modify (
변경할 컬럼명1 데이터유형 디폴트값, not null제약,
변경할 컬럼명2 데이터유형 디폴트값, not null제약,
변경할 컬럼명3 데이터유형 디폴트값, not null제약,
...);
ex) alter table team_temp modify(
orig_yyyy varchar2(8) default '20020129' not null); 

4)rename column : 컬럼'이름' 변경
-alter table 테이블명 rename column 변경하기전 컬럼이름 to 변경할 컬럼이름;
ex) alter table player rename column player_id to temp_id;

5)drop constraint : 제약조건 삭제
-alter table 테이블명 drop constraint 제약조건명;
ex)alter table player drop constraint player_fk;

6)add constraint : 제약조건 추가
-alter table 테이블명 add constraint 제약조건명 제약조건(컬럼명);
ex)alter table player add constraint player_fk foreign key(team_id) references team(team_id);

4.rename table : 테이블'이름'변경
-rename 변경하기전 테이블명 to 변경할 테이블명;
ex)rename team to team_backup;

5. drop table : 테이블 삭제
-drop table 테이블명 cascade constraint;
ex)drop table player;

6.truncate table : 테이블'행 instance'삭제
-truncate table 테이블명;
ex)truncate table player;

#alter table 테이블명 
(add ,drop column,modify column ,rename column //column 추가,삭제,수정,이름변경)
(drop constraint, add constraint // 제약조건 삭제, 추가)

#rename table, drop table, truncate table
-테이블 이름변경, 테이블삭제, 테이블내용삭제

댓글

이 블로그의 인기 게시물

1장_1절 관계형 데이터베이스 개요

1장_5절. Where 조건절