DBMS 2
DA, SQL, DB보안 등 실무자를 위한 위한 DB기술 바이블!
DB2 UDB의 SQL을 다음과 같은 요건에 쉽게 사용 할 수 있습니다. Table의 data 삭제 후 삭제된 data를 보고자 할 경우의 예입니다. Table의 data를 update한 후 update 된 row를 보고자 할 경우의 예입니다. Table의 data를 update한 후 update 된 row를 보고자 할 경우의 예입니다. 중복되었는지 아닌지 기준이 되는 column은 partition by 절 다음에 기술하고 이 중복된 data에 대해 특정 column으로 순번을 정하고 할 경우 order by절 다음에 column을 기술합니다. 이렇게 return된 값 중에서 첫 번째 data만 남기고 삭제하고자 할 경우 상위 SQL절에서 rn > 1과 같이 순위에 대한 제어를 합니다. subselect 절에서 return되는 값을 제한하고자 할 경우 subselect 절 내에서 fetch first n rows only 절을 기술합니다. 다른 table과 join하여 조건을 만족시키는 값만 table에서 delete를 하고자 할 경우의 예입니다. 다른 table과 join하여 조건을 만족시키는 값만 update 하거나 만족시키지 않는 값만 update하는 경우의 예입니다. Recursive SQL을 사용하고자 할 경우의 예입니다. Left outer join을 사용하고자 할 경우의 예입니다. Join predicate은 on절에 기술합니다. where절은 두 table간의 outer join을 수행한 결과에 대한 조건을 기술합니다. Left outer join에서 left table에 해당하는 조건은 on 절에 기술하고 right table에 해당하는 조건은 inline view를 통해 먼저 읽어오도록 한 후 left outer join을 하도록 한다. values 문을 통해 시스템 시간을 볼 수 있으며 sysibm.sysdummy1 table에 대한 query를 통해서도 볼 수 있습니다. Values 문을 사용하는 것을 권고합니다. 1) 현재 날짜에서 '-' 을 없애고자 할 경우, 2) 현재날짜와 시간 초만 알고 싶은 경우의 예입니다. date를 character로 conversion 하고자 할 경우의 예입니다. character를 date를 conversion 하고자 할 경우의 예입니다. select문으로부터 null값이 return될 때 원한 값으로 대체하고자 할 경우의 예입니다. Salary가 null이면 0을 return합니다. 많은 row가 들어있는 table의 data를 전부 삭제하고자 할 경우 아래 3가지 중 하나를 사용할 수 있습니다. Import를 사용할 것을 권고합니다.SQL 및 Function Tip
SQL 및 Function Tip
SQL 및 Function Tip
Delete 수행 후 delete 전 data 보기
order by empno
fetch first row only ) );
Update 후 update 된 row 보기
중복된 row중에서 한 row를 제외한 모든 row 삭제하기
insert into ar_dup
( select rownumber() over (partition by ar_id order by sa_id ) as rn
from ar ) where rn > 1;
subselect 절에서의 row 건수 제안하기
From ( select projno, projname, repemp from project
Where deptno = 'D11'
Fetch first 5 rows only ) temp, employee emp
Where temp.repemp = emp.emp_id
correlated delete 하기
where exists ( select 1 from sales s where i.itemno = s.itemno );
correlated update 하기
set a.icust_id = ( select b.icust_id from pltstag.tb_icstcustmap b
where a.src_cust_id = b.cust_id
and b.op_sys_cd = '1' )
update pltstag.ip_cust_mapp_info a
set a.icust_id = ' '
where not exists ( select 1 from pltstag.tb_icstcustmap b
where a.src_cust_id = b.cust_id
and b.op_sys_cd = '1' )
Bill of Material (BOM)
(
SELECT 1, ROOT.PART, ROOT.SUBPART, ROOT.QUANTITY
FROM PARTLIST ROOT
WHERE ROOT.PART = '01'
UNION ALL
SELECT PARENT.LEVEL+1, CHILD.PART, CHILD.SUBPART, CHILD.QUANTITY
FROM RPL PARENT, PARTLIST CHILD
WHERE PARENT.SUBPART = CHILD.PART
AND PARENT.LEVEL < 2
)
SELECT PART, LEVEL, SUBPART, QUANTITY
FROM RPL
Outer Join
FROM emp A LEFT OUTER JOIN customer B
ON A.id = B.sales_rep_id
WHERE B.cust_id = 'AAA'
current timestamp, time, date 보기
Values ( current time )
Values ( current date )
Select current timestamp, current time, current date from sysibm.sysdummy1
values ( hex(current date) ) -> 200401212)
values (substr(hex(current timestamp),1,14)) -> 20040121135656
Values (char( current date, usa) -> 01/21/2004
Values (char( current date, local ) -> 01/21/2004
10 null 값 처리
data truncate
Load from /dev/null of del replace into irg.ar
Alter table itg.ar activate not logged initially with empty table