기본 콘텐츠로 건너뛰기

라벨이 sql인 게시물 표시

[오라클] update, insert 한번에 (merge into)

두번할꺼 한번에 해주는 편한기능이다. 그렇다고 모든 insert나 update를 이렇게 할필요는 없다. '걍 insert 하믄 되지 뭘 왜 select를 하구있어' 라는 소리를 들을수도 있다. 보통 개발진행중이거나, 배치작업같은데서 종종 필요할듯 싶다. MERGE INTO RequestTable RT -- 이번 쿼리의 대상이 되는 테이블 USING JoinTable JT -- 조인이 필요한 테이블 ON (RT.col1 = JT.col1..) -- 조인조건 WHEN MATCHED THEN -- 조건에 합당할경우 / 데이터가 있는경우 UPDATE SET -- updqte, set 사이에 테이블명을 명시하지 않음 RT.col2 = JT.col2 ,RT.col3 = JT.col3 ,RT.col4 = JT.col4 WHEN NOT MATCHED THEN -- 조건에 안맞음 / 데이터가 없는경우 INSERT (col1, col2, col3) VALUES ( JT.col1 , JT.col2 , JT.col3 ); 여기서는 update, insert만 썼지만 delete도 얼마든지 가능하다.

[오라클] 테이블 복사 (Creat Table As Selcet)

이따금씩 테이블 복제가 필요한 경우가 있다. 보통 개발이나 테스트시에 임시로 백업이 필요한 경우에 종종쓴다 그럴때마다 crate 하고 insert를 별도로 할필요없이  한번에 할 수있다.  create table new_table as select * from org_table org_table을 new_table로 복사하는 sql이다.  Creat Table As Selcet 을 줄여서 CTAS라고도 많이 부르는것 같다.  만약에  해당 테이블의 스키마만 복사하고 싶을때는  where 조건을 추가해서 select구문의 결과값이 하나도 안나오게끔 해주면 된다.  create table new_table as select * from org_table where 1 = 0

sql 개행문자 제거

sql에서 텍스트를 조회한 뒤에 엑셀이나 다른데로 옮길경우 줄바꿈이 되는 경우가 있다. 문장내에 'CR', 'LF'가 들어있어서 발생한다. LF : Line Feed CR : Carriage Return 그럼 해결방법은 LF나 CR을 다른 문자로 바꿔주면 해결 . select '가나다라마바사 아자차카타파하' from dual; select replace(replace( '가나다라마바사 아자차카타파하' ,chr( 10 ), '' ),chr( 13 ), '' ) from dual;

mybatis 기본문법

select 기본 <select id="selectPerson" parameterType="int" resultType="hashmap"> SELECT * FROM PERSON WHERE ID = #{id} </select> 변수 설정은 #{} and #{id} and id like '%'||#{id}||'%' -- concat if문  <if test=""> -  예제도 그렇지만 보통 null 체크할때 많이 쓴다. <select id="findActiveBlogWithTitleLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select> choose,when,otherwise   - 게시판에서 전체/제목/내용에 따라 검색방법을 달리할때 처럼     해당 변수가 조회타입 (?)의 성격을 가질때 사용하면 좋을듯. <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND author_name like ...

[오라클] sid, service name 값 확인

sqlgate나 sqldeveloper같은 툴에서 db에 연결할때,  종종 sid와 service name를 입력해야 한다. 간단하게 설명하면, sid 는 각 인스턴스 별 id service name 은 전체 id 고로 DB장비가 2대로 구성이 되어있으면, 각 sid는 달라도, service name은 같을 수 있다. 자세한 설명은 -> http://blog.naver.com/myshyz/50037204012 sid, service name 확인하는 쿼리 select name from v$database; select instance from v$thread;

mysql oracle 유사 명령어

포트번호 mysql : 3306 oracle : 1521 mssql : 1433 DB 접속 mysql shell > mysql --user=user_name --password=your_password db_name shell > mysql -u user_name -p password db_name oracle shell > sqlplus username/password@db_name DB내 존재하는 테이블 검색 mysql > show tables ; oracle > select * from tabs ; 결과수 제한 mysql > select * from table where ROWNUM < 10 ; oracle > select * from table limit 10 ; NULL 처리 mysql > select IFNULL (field1, 0) from table ; oracle > select NVL (field1, 0) from table ; 현재시간 mysql > select now() from dual ; oracle > select sysdate from dual ; if-else 구문 mysql > select if(field=1, 'X','Y') from table ; oracle > select decode(field,1,'X','Y') from table ;