SQL 실습 정리 - 1

 본 실습은 Oracle9i introduction to Oracle9i SQL vol2의 '14장 sql 강습' 을 토대로 진행했습니다.


※완성된 전체 테이블 구조(기본 SCOTT 계정 사용)

※완성된 전체 테이블 표

-MEMBER table

-RENTAL table

-RESERVATION table

-TITlE table

-TITLE_COPY table

※ 테이블 기본 생성
DDL 구문 중 CREATE 이용

CREATE TABLE table 명(
    열 이름1 타입 [제약 명,] 
    [열 이름2 타입 ... ]
);

또는

CREATE TABLE table 명(
    열 이름1 타입[,]
    [열 이름2 타입...]
    ...
    ,CONSTRAINT 제약 조건 제약 명(제약 지정 열) [외래키의 경우 : REFERENCES 테이블 명(해당 테이블의 기본키)]
);

※사용된 키 개념

기본키 Primary key : 테이블 당 반드시 하나씩 존재해야 하며, 최소성(단일로 존재), 유일성(테이블 내 단 한개)을 만족함


외래키 foreign key : 테이블 간의 관계에 해당하는 키, 다른 테이블의 기본키를 참조함


※도움이 될 키 개념

슈퍼키(복합 기본키) : 유일성은 만족하나, 두 개 이상의 열이 하나의 키로 구성하게 되어 최소성을 상실하는 복합적 기본키


후보키 : 유일성, 최소성을 만족하는 키(기본키가 될 수 있는 후보)


대체키 : 키본키가 되지 못한 후보키


unique key : null 값을 제외한 값의 중복을 허용하지 않는다


※사용 제약 조건

default : 값을 삽입할 때, 별도로 값의 명시를 하지 않는 경우, 지정된 기본값을 대신 삽입함

▶기본 형식

create table(

...

    열 이름 default 기본 값

...

);


check : 지정 값 이외에는 삽입이 불가

▶기본 형식

create table(

...

    --지정 값들 외의 값은 삽입이 불가

    열 이름 check ( 해당 열 in (지정 값 1, 지정 값 2, 지정 값 3, ... ))

...

);


not null : null 값을 허용하지 않음

▶기본 형식

create table(

...

    열 이름 not null

...

);



▶테이블 생성 문제-1

MEMBER table 생성 조건









-MEMBER table 생성 스크립트

create table member(
    member_id number(10),
    last_name varchar2(25) constraint member_last_name_nn not null,
    first_name varchar2(25),
    address varchar2(100),
    city varchar2(30),
    phone varchar2(15),
    join_date date default sysdate constraint member_join_date_nn not null,
    constraint member_memberId_pk primary key(member_id)
);


TITLE table 생성 조건



-TITLE table 생성 스크립트

create table title(
    title_id number(10),
    title varchar2(60) constraint title_title_nn not null,
    description varchar2(400) constraint title_descreiption_nn not null,
    rating varchar2(4) ,
    category varchar2(20),
    release_date date,
    constraint title_title_id_pk primary key(title_id),
    constraint title_rating_ck check (rating in ('G','PG','R','NC17','NR')),
    constraint title_category_ck  check (category in ('DRAMA','COMEDY','ACTION','CHILD','SCIFI','DOCUMEN','TARY'))
);


TITLE_COPY table 생성 조건



-TITLE_COPY table 생성 스크립트

create table title_copy(
    copy_id number(10),
    title_id number(10),
    status varchar2(15) constraint title_copy_status_nn not null,
    constraint title_copy_copy_id_Title_id_pk primary key(copy_id, title_id),
    constraint title_copy_title_id_fk foreign key(title_id) references title(title_id),
    constraint title_copy_status_ck check (status in ('AVAILABLE','DESTROYED','RENTED','RESERVED'))
);


-RENTAL table 제약 조건


RENTAL table 생성 스크립트
create table rental(
    book_date date default sysdate,
    member_id number(10),
    copy_id number(10),
    act_ret_date date,
    exp_ret_date date default sysdate+2,
    title_id number(10),
    constraint rental_BD_MI_CI_TI_pk primary key(book_date, member_id, copy_id, title_id),
    constraint rental_member_id_fk foreign key(member_id) references member(member_id),
    constraint rental_copy_id_title_id_fk foreign key(copy_id, title_id) references title_copy(copy_id, title_id)
);


-RESERVATION table 제약 조건
업로드 중: 총 8314바이트 중 8314바이트가 업로드되었습니다.


RESERVATION table 생성 스크립트
create table reservation(
    res_date date,
    member_id number(10),
    title_id number(10),
    constraint reservation_RD_MI_TI_pk primary key(res_date, member_id, title_id),
    constraint reservation_member_id_fk foreign key(member_id) references member(member_id),
    constraint reservation_title_id_fk foreign key(title_id) references title(title_id)
);

댓글