전체 글 (74) 썸네일형 리스트형 [ORACLE] SQL - 테이블 원본그대로 복사하기, column만 복사하기, 특정조건의 데이터만 복사하기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 --원본테이블 복사하는 방법 create table emp1 as select * from employees; --만약 80번 부서만 복사하고 싶다면? create table emp2 as select * from employees where department_id = 80; --칼럼 이름만 복사해오고 싶다면? create table emp3 as select * from employees where 1=0; --특정 colum이름만 복사해오고 싶다면? create table emp4 as select employee_id, first_name, salary, hire_date from employees where 1=0;.. [Oracle] SQL - interval 데이터타입 활용하기 공부를 하다가 보니 table을 생성할 때, datatype으로 'interval year(3) to month' 'interval day(3) to second' 라고 선언해서 사용하는 것을 보고 이번 포스팅을 하게 되었습니다. 1 2 3 4 5 create table cc( today date, year1 interval year(3) to month, day1 interval day(3) to second ); cs 위처럼 cc 라는 table을 생성하였습니다. 변수이름 : year1 - 데이터타입 : interval year(3) to month 에는 실제 데이터를 insert할때 interval '개월간격' to month(3) interval '연간격' to year(3) 를 넣을 수 있습니다.. [ORACLE] SQL - SELECT 문제 선수가 속해 있는 팀의 평균키보다 작은 선수들의 정보를 출력하시오. 풀이1. 1 2 3 4 5 6 7 8 select aa.team_id as "팀코드", player_name as "선수명", position as "포지션", back_no as "백넘버", height as "키",bb.avh from player_t aa, ( select team_id, avg(height) avh from player_t group by team_id ) bb where aa.team_id = bb.team_id and aa.height [ORACLE] SQL - FUNCTION 아래 FUNCTION함수는 매개 변수로 받은 두 값을 합쳐서 리터해줍니다. 1 2 3 4 5 6 CREATE OR REPLACE FUNCTION aa(value1 int, value2 int) return number as begin return value1 + value2; end; Colored by Color Scripter cs SELECT를 통해서 확인해보았습니다. 1 select aa(14,21) from dual; cs [ORACLE] SQL - 프로시저 (1개의 매개변수를 받고 1개를 출력) 1개의 매개변수를 받고 1개를 출력하도록 하는 프로시저 예시입니다. 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 --번호 입력을 위한 sequence를 생성하였습니다. create sequence hh_sequence; --프로시저를 실행하였을 때의 값을 저장하기 위한 테이블을 생성하였습니다. create table hh_table (id_num number(38), first_name varchar2(20)); --프로시저를 만들때, 매개변수로 v_fname을 받고 v_lname을 출력하도록 하였습니다. create or replace procedure hh_pro ( v_fname in employee.. [ORACLE] SQL - 프로시저 (매개변수가 1개인 경우, 2개인 경우) *제 자신의 이해를 돕기 위해 프로시저 이름을 무의미하게 만들었음을 미리 알려드립니다.* 프로시저를 생성할때 명령문 실행(ctrl+enter)가 아닌 스크립트 실행(f5)를 사용합니다. 1 2 3 4 5 6 7 8 9 10 11 12 CREATE OR REPLACE procedure hh (f_name in employees.first_name%type) as v_sal number(10); begin select salary into v_sal from employees where first_name = f_name; dbms_output.put_line(f_name||' '||v_sal); end hh; / Colored by Color Scripter cs 아래 execute를 사용하여 실행하면 됩.. [ORACLE] SQL - 스토어드 프로시저(Procedure) 1 2 3 4 5 6 7 8 9 10 11 12 create or replace procedure sp_print5 is num number :=1; begin while(num [ORACLE] SQL 반복문 - FOR문, WHILE문[ORACLE] SQL 반복문 - FOR문, WHILE문 WHILE문 1 2 3 4 5 6 7 8 9 10 11 12 13 declare num number :=1; begin Loop dbms_output.put_line('while:'||num); num := num + 1; if(num>5) then exit; end if; end loop; end; / Colored by Color Scripter cs FOR문 1 2 3 4 5 6 7 8 9 declare begin for i in 1..5 loop dbms_output.put_line('for:'||i); end loop; end; / Colored by Color Scripter cs WHILE 조건문 1 2 3 4 5 6 7 8 9 declare num number :=1; begin while.. [ORACLE] SQL -- if문 예시 12345678910111213141516171819202122declareemp_record2 employees%rowtype;begin SELECT * into emp_record2 FROM employees where employee_id = 101; if(emp_record2.commission_pct is null) then dbms_output.put_line('null이다 '||emp_record2.salary*12); else dbms_output.put_line('null이 아니다 '||emp_record2.salary*12+emp_record2.salary*emp_record2.commission_pct); end if; dbms_output.put_line(emp_record2.emp.. [ORACLE] SQL - TYPE의 is record 활용하기 1. is record 안에 변수명, 타입을 넣어준다. 2. 만든 타입으로 변수(aa)를 만든다. 3. select ~~ into를 이용하여 column의 값들을 aa에 넣는다. 4. aa.해당 변수를 써서 해당 값을 읽어올수있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 declare type aa_type is record( v_fname employees.first_name%TYPE, v_lname employees.last_name%TYPE, v_sal employees.salary%TYPE, v_deptno employees.department_id%TYPE ); aa aa_type; begin SELECT first_name,last_name,.. [Oracle] SQL문 - TYPE과 FOR문 활용 예시 1. aa_type이라는 타입을 만듭니다. 2. employees.first_name%type를 써서 원래 employees의 first_name의 타입을 그대로 가져옵니다. 3. index by binary_integer를 붙여 딕셔너리 형식으로 숫자를 넣고 값을 입력할수 있도록 만듭니다. 4. for 문을 활용해서 employees 테이블의 fist_name를 하나씩 읽어오고 이를 bb에 담아서 for문을 돌립니다. 5. idx 는 1씩 추가하도록 만들어 aa(숫자)의 숫자를 하나씩 늘리고 담아온 bb의 first_name을 aa(idx에 담습니다.) 6. for cc를 활용해서 값을 읽어옵니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 DECLARE type aa_type i.. [ORACLE] SQL - TRIGGER(DELETE시 데이터 백업 예시) 안녕하세요. TRIGGER를 활용해서 테이블에서 DELETE시, 별도의 TABLE 에 알아서 백업해주는 코드입니다. 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 --ABCD 테이블 생성 create TABLE ABCD (id number,name nvarchar2(5)); insert into ABCD values(1,'유재석'); insert into ABCD values(2,'박명수'); insert into ABCD values(3,'하동훈'); --ABCD_DEL_LIST 테이블 생성(ABCD에서 삭제한 데이터를 백업할 테이블) --where 1 = 0을 활용하여 column이름만 가져왔습니다. c.. 이전 1 ··· 3 4 5 6 7 다음