메뉴 건너뛰기

조회 수 3864 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

게시판 만들기 프로젝트 도중 BoardMapper.xml의 SQL 쿼리문을 작성하다가 궁금증을 해결하기 위해 순서를 건너뛰고 카테고리를 하나 만들어 글을 쓰게 되었습니다. 


학원에서는 오라클을 쓰다가 집에서 이것저것 공부해보겠다고 데이타베이스를 MySQL로 써보자 하고 다운받아 설치해본것이 화근이 되었던것 같아요. SQL쿼리문이 다를것이다라는건 상상도 못했거든요. 그래도 모르는 부분은 부딪혀 보고, 해결해낸다면 그만한 뿌듯함은 없는것 같기도 해요.


<!-- 글쓰기 시작-->
    <insert id="insertBoard" parameterType="BoardDto">
        insert into tbl_board(seq,name,email,homepage,title,content,password,
        count,regdate,pos,depth) values(seq_num.nextval,#{name},#{email},#{homepage},
        #{title},#{content},#{password},0,sysdate,0,0)
    </insert>    
<!-- 글쓰기 종료 -->    

게시판을 만들고 Mapper파일의 입력하는 insert부분에서 작동이 안됬습니다. tbl_board는 게시판 테이블이름이고, 글쓰기 기능으로 시퀀스(일련번호)를 포함한 name,email 등 여러가지를 입력하고 values값에 위와같이 넣어준 코드죠. 찾아보았더니 [4번째]줄인 seq_num.nextval 이 Oracle구문이라 하더군요. 


똑같은 말이지만 학원에서는 seq를 올려주려면 nextval로 줘야 한다고 배웠기 때문이죠. 그래서 많은 시간을 검색 한 결과 nextval을 MySQL에서 어떻게 사용하는지 답을 찾았습니다.



 방법 1) 


1
2
3
4
    <insert id="insertBoard" parameterType="BoardDto">    
        insert into tbl_board(seq,name,email,homepage,title,content,password,count,regdate,pos,depth)
        values((select *from (select max(seq)+1 from tbl_board) next),#{name},#{email},#{homepage},#{title},#{content},#{password},0,curdate(),0,0)
    </insert>



seq.nextval → (select * from (select max(seq)+1 from tbl_board)로 바꾸는 방법입니다.

(select max(seq)+1 from tbl_board) 을 썻을 경우 오류가 생기기 때문에

앞에 select *from 으로 한번 더 묶어줍니다.



 방법2)


1
2
3
4
    <insert id="insertBoard" parameterType="BoardDto">    
        insert into tbl_board(seq,name,email,homepage,title,content,password,count,regdate,pos,depth)
        select case count(*) when 0 then 1 else max(seq) + 1 end,#{name},#{email},#{homepage},#{title},#{content},#{password},0,curdate(),0,0 from tbl_board
    </insert><!--end -->
cs


seq.nextval → slect case count(*) when 0 then 1 else max(seq) + 1 end  로 바꾸는 방법입니다.

이 방법은 데이타베이스에 초기에 seq값을 안 들어가있을 경우

즉. seq가 0일 때도 무리없이 실행시킬수 있기 때문에 테스트하기에 가장 좋습니다. 추천~!! 



  1. 전체 텍스트 검색과 파티션

    Date2017.12.22 Views3868
    Read More
  2. 전체 텍스트 검색과 파티션

    Date2017.12.22 Views3946
    Read More
  3. 테이블 파티셔닝

    Date2017.12.22 Views4940
    Read More
  4. 날짜 관련 함수 모음.

    Date2017.12.22 Views3976
    Read More
  5. SQL 고급

    Date2017.12.22 Views6035
    Read More
  6. MySQL 대용량 DBMS 개선 사례

    Date2017.12.28 Views8013
    Read More
  7. MySQL 연결 속도

    Date2017.12.28 Views3917
    Read More
  8. 데이터베이스 파티셔닝이란

    Date2017.12.28 Views4262
    Read More
  9. MySQL 에서 테이블에 이미 존재하는 값으로 UPDATE 하는 경우

    Date2018.07.18 Views1533
    Read More
  10. MySQL DB 에 한글 utf8 문자열 INSERT 오류 해결 방법

    Date2018.07.18 Views2039
    Read More
  11. InnoDB 스토리지 엔진에서 테이블의 최대 저장 row 개수

    Date2018.07.18 Views1675
    Read More
  12. MyISAM 스토리지 엔진에서 테이블의 최대 저장 row 개수

    Date2018.07.18 Views1558
    Read More
  13. MySQL 클라이언트/서버 프로토콜

    Date2018.07.18 Views2750
    Read More
  14. mysql-bin 로그를 SQL 문으로 변환한는 방법

    Date2018.07.18 Views1672
    Read More
  15. 특정 테이블만 replication 하거나 제외하는 방법

    Date2018.07.18 Views2822
    Read More
  16. MySQL 마스터/마스터 replication 에서 AUTO_INCREMENT 문제 해결 방법

    Date2018.07.18 Views1941
    Read More
  17. MySQL replication SQL 문 실행 오류 해결 방법

    Date2018.07.18 Views1608
    Read More
  18. MySQL〃오라클의 nextval을 MySQL에서 사용하기

    Date2018.07.24 Views3864
    Read More
  19. MySQL Table 복구 - Got error 127 from storage engine

    Date2018.07.24 Views4367
    Read More
  20. Mysql Join 해부(Left, Right, Outer, Inner Join

    Date2018.10.02 Views1276
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 Next
/ 7

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved