ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • oracle 시퀀스
    DataBase/oracle 2020. 3. 22. 01:50

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    /* 시퀀스 : 연속적인 번호를 만들어주는 기능
        create sequence 시퀀스이름
        increment by n : 증가값을 설정 ex) n이 1이면 1씩 증가 / 기본값은 1
        start with n : 시작값 설정 / 기본값은 1
        maxvalue n | nomaxvalue : 시퀀스 최대값
        minvalue n | nominvalue : 시퀀스 최소값 : cycle 옵션일 경우 시작값
        cycle | nocycle : 시퀀스를 순환 사용할 지 설정
        cache n | nocache : 시퀀스의 속도를 개선하기위해 캐싱여부 지정
            
    */
     
    --1) 시퀀스 생성 : 제품번호를 생성하는 시퀀스 만들기
    create sequence seq_serial_no 
    increment by 1
    start with 100
    maxvalue 110
    minvalue 99
    cycle
    cache 2;
     
    create table good(
        good_no number(3),
        good_name varchar2(10)
    );
     
    insert into good 
    values(seq_serial_no.nextval, '제품1'); -- 나는 100번 출력됐는데 인강은 101번 출력됨..뭐지..-_-
     
    select * from good;
     
    insert into good values(seq_serial_no.currval, '제품1'); 
    insert into good values(seq_serial_no.nextval, '제품2');
     
    -- 시퀀스이름.currval : 시퀀스 현재값
    -- 시퀀스이름.nextval : 다음 시퀀스 값
     
    select seq_serial_no.currval from dual;
     
    -- 시퀀스 110번까지 모두 소진하면 99번으로 다시 돌아감 <-- cycle 명령어로 순환을 가능하게 했기때문에!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    create table good2(
        good_no number(3),
        good_name varchar2(10)
    );
     
    create sequence seq_serial_no2 
    increment by 1
    start with 100
    maxvalue 105
    cache 2;
     
    insert into good2 values(seq_serial_no2.nextval, '제품1');
    commit;
     
     
     
    select * from good2;
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

    1) 새로운 테이블과 시퀀스를 만들고

    2) 데이터 하나를 입력 후

    3) system 계정으로 들어가서 shutdown 시킨 후 startup 한 다음

    4) 다시 새로운 데이터를 입력하면!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    SQL> select * from good2;
     
       GOOD_NO GOOD_NAME
    ---------- --------------------
           100 제품1
     
    SQL> SYSTEM 접속
    Connected.
    SQL>
    SQL>
    SQL>
    SQL> shutdown abort;
    ORACLE instance shut down.
    SQL> startup
    ORACLE instance started.
     
    Total System Global Area 1068937216 bytes
    Fixed Size                  2260048 bytes
    Variable Size             796918704 bytes
    Database Buffers          264241152 bytes
    Redo Buffers                5517312 bytes
    Database mounted.
    Database opened.
    SQL>
    SQL> HR접속
    Connected.
    SQL>
    SQL>
    SQL> select * from good2
      2  ;
     
       GOOD_NO GOOD_NAME
    ---------- --------------------
           100 제품1
     
    SQL> insert into good2 values(seq_serial_no2.nextval, 'good2');
     
    1 row created.
     
    SQL> select * from good2;
     
       GOOD_NO GOOD_NAME
    ---------- --------------------
           100 제품1
           102 good2
     
    SQL>
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

    5) cache에 다음값이 101로 설정이 되어있었는데 shutdown으로 초기화시켰기때문에 seq_serial_no2 의 nextval이 102가 되었다...!!?? <-- 이해안감...이건 책을 더 찾아봐야겠다

     ** 서버 다운시키는 명령어 : shutdown abort;

     ** 서버 기동시키는 명령어 : startup

     ** sequence 는 생성또는 드랍시키거나 nextval로 증가시키는것만 해봤는데 다른 설정값들 주는게 흥미로웠다!

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

    oracle PL/SQL_개념  (0) 2020.03.22
    oracle 계층형 쿼리  (0) 2020.03.22
    oracle 뷰  (0) 2020.03.21
    oracle 제약조건  (0) 2020.03.21
    oracle DML  (0) 2020.03.21

    댓글

Designed by Tistory.