Oracle
-
[Oracle] 값으로 테이블, 컬럼명 찾기Oracle 2022. 5. 11. 07:59
가끔 데이터가 어느 테이블, 컬럼에 있는지 찾아야할 때가 있다. 이럴 때 사용할 수 있는 쿼리이다. SELECT 'SELECT ''' || TABLE_NAME || ''', ''' || column_name || ''',' || COLUMN_NAME || ' FROM ' || TABLE_NAME || ' WHERE ' || COLUMN_NAME || '= ''값'' UNION ' FROM USER_TAB_COLUMNS WHERE DATA_TYPE like 'VARCHAR%'; 위 쿼리의 값 부분에 원하는 데이터를 입력하고 실행을하면 값을 찾을 수 있는 쿼리가 결과값으로 생성된다. 이 결과값을 모두 선택하여 실행시켜보면 테이블 이름과 컬럼 명을 알 수가 있다.
-
RANK, DENSE_RANK 함수Oracle 2021. 5. 24. 15:45
오라클에서 순위를 매기는 함수 중 RANK, DENSE_RANK 함수가 있다 특정 범위(PARTITION) 내에서 순위를 구할 수도 있고 전체 범위에 대한 순위를 구할 수도 있다 RANK 와 DENSE_RANK 의 차이점은 문법을 활용하는 방법은 똑같으나 동일한 순위를 하나의 건수로 취급하는 건 DENSE_RANK 이다 예를들어 RANK 의 경우 : 1등, 2등, 2등, 4등 DENSE_RANK 의 경우 : 1등, 2등, 2등, 3등 DENSE_RANK 의 경우 2등이 2개이지만 하나의 건수로 취급하여 다음 등수는 3등이 된다 함수의 사용법은 아래와 같다 전체 범위에서 속성을 기준으로 순위를 구함 RANK() OVER(ORDER BY 속성 DESC/ASC) 속성1을 특정범위로 하여 속성2를 기준으로 순위..
-
Oracle EXISTS 서브쿼리 - 존재하는지 안하는지 확인하는 방법Oracle 2021. 5. 24. 12:00
Oracle 에서 해당 조건을 만족하는 행이 있는지 없는지 확인하기 위해 EXISTS 라는 서브쿼리가 있다 예를 들면 선수정보를 가지고 있는 PLAYER 라는 테이블이 있다 PLAYER_ID PLAYER_POSITION TEAM_ID PLAYER TEAM_ID 가 K02 이고 PLAYER_POSITION 이 GK 인 선수를 조회하고 싶다는 쿼리를 작성해 보자 SELECT * FROM PLAYER P1 WHERE P1.TEAM_ID = 'K02' AND EXISTS ( SELECT 1 FROM PLAYER P2 WHERE P1.TEAM_ID = P2.TEAM_ID AND P2.POSITION = 'GK' ); EXISTS 는 무조건 서브쿼리로 작성해야한다 또한 메인쿼리에서 사용한 컬럼을 그대로 사용할 수 ..
-
CASE - 값에 따라 다르게 SELECT 문 만드는 법Oracle 2021. 5. 21. 10:39
조회하는 값에 따라 SELECT 문에 다른 속성값을 표현하고 싶다면 CASE 문을 사용하면 된다 = DECODE 와 기능이 같은 문법. 조건이 여러개가 붙는다면 코드가 지저분해져서 다중조건이라면 DECODE 를 사용하는 편이 낫다 CASE 사용법1 - 속성, 값 비교 CASE 속성 WHEN 값1 THEN 속성이_값1일때의_값 WHEN 값2 THEN 속성이_값2일때의_값 ELSE 전부_거짓일때의_값 END 위의 구문을 SELECT 절의 속성자리에 넣어주면된다 CASE 사용법2 - 조건으로 비교 CASE WHEN 조건1 THEN 조건1이_참일때의_값 WHEN 조건2 THEN 조건2가_참일때의_값 ELSE 전부_거짓일때의_값 END 예제 VM_CHECK_SHEET 에서 STAMP, MODEL_NAME 그리고 ..
-
DATE Type Select 해서 다른 테이블에 Insert 하기Oracle 2021. 3. 2. 13:13
DATE Type data 를 Select 해서 그대로 다른 테이블에 Insert 를 하는 경우 위와 같이 시간이 없어진 data 가 Insert 된다 이와 같은 경우 DATE data 를 -> TO_CHAR() 후 -> TO_DATE() 로 저장시키면 시간을 그대로 저장할 수 있다 자세히 살펴보자 1. TO_CHAR("원하는 Column", "Column 의 Data 양식") 으로 Select 를 한다 select TO_CHAR(REQUEST_DATE,'YYYY/MM/DD AM HH:MI:SS') AS CONVERT_DATE from print_queue2 order by request_date desc; 2. 저장하고자 하는 테이블에 TO_DATE("저장할 값", "저장할 Data 의 양식") 으로 ..
-
auto incrementOracle 2020. 12. 9. 10:42
oracle 은 mysql 과는 다르게 auto increment 를 지원하지 않는다 auto increment 를 하기 위해서는 SEQUENCE 를 사용해야한다 create sequence topic_seq start with 1 increment by 1; use example insert into topic values(topic_seq.nextval, '새로운 글' || topic_seq.nextval, '새로운 내용입니다', sysdate); topic_seq.nextval 으로 사용하면된다 - 시퀀스명.NEXTVAL : 사용가능한 다음 시퀀스값 반환 - 시퀀스명.CURRVAL : 현재 시퀀스값을 반환