วันพุธที่ 9 กันยายน พ.ศ. 2558

DB2 คำสั่งต่างๆ


คำสั่งต่างๆบนฐานข้อมูล DB2


คำสั่งการ export ข้อมูล


export to d:\business.txt of del

select * from cmb.business fetch first 10 rows only;

(ใช้ comma เป็นตัวคั่นระหว่างฟิลด์ และดึงเฉพาะ 10 รายการแรกที่เจอ)



export to d:\employee.del of del  MODIFIED BY COLDEL|
select title,fname,lname
from employee.user  where office ='99100000';

( ใช้ pipe |  เป็นตัวคั่นระหว่างฟิลด์)

คำสั่งลบตาราง

drop table cmb.user;


คำสั่งสร้างตาราง


CREATE TABLE  cmb.user (
        "ID"                VARCHAR(9) NOT NULL PRIMARY KEY,
        "NAME"               CHAR(150),
        "POSITION"         VARCHAR(150),
        "OFFICENAME"         VARCHAR(100),
        "OFFICEID"       CHAR(8),
        "ADDDATE"            DATE
) IN "DATA1" INDEX IN  "INDEX1" ;


คำสั่ง drop primarykey


ALTER TABLE cmb.user DROP PRIMARY KEY;

คำสั่งให้สิทธิ

ให้สิทธิ select,insert,update,delete บนตาราง cmd.user  กับ user bbc01
GRANT  select,insert ,update,delete ON TABLE cmd.user TO USER bbc01;




คำสั่งเพิ่มฟิลด์เข้าตาราง


ALTER TABLE TESTDSS.EST_RD ADD COLUMN YEAR_BUD CHARACTER (4) ;



คำสั่งสร้าง index

CREATE INDEX  I01_cmb_logfile ON  cmb.LOGFILE (id);

คำสั่งสร้าง index แบบ function


ตัวอย่าง ต้องการสร้าง index โดยให้ฟิลด์เป็น upper() ก่อน
แทนที่จะสร้างฟิลด์ใหม่ เราสามารถสร้าง function upper ให้เป็นตัว index ได้เลย
เริ่มมีใน db2 เวอร์ชั่น 10.5 ครับ
db2 create index  inx1 on emp.user(upper(v_name)) allow reverse scan;


คำสั่งเกี่ยวกับวันที่


select year(current date-1 years - 6 months),month(current date-1 years - 1 months) from emp.user first 1 rows only



แปลงตัวอักษรเป็นตัวเล็ก

lcase(cname)




ใช้เงื่อนไขในคำสั่ง

select fname,lname,case sex  when 'M' then 'ชาย' ELSE 'หญิง'  END as sexdesc  from emp.user;

คำสั่งอื่นๆ

select substr(id,1,1),count(*) from user.employee group by   substr(id,1,1) with ur;

ดูรายชื่อ schema

list tables for schema off




import ข้อมูลเข้าตาราง


import from "C:\source\รายชื่อบริษัท.txt" of del
 MESSAGES "d:\import.log" insert into cmb.business;



import from d:\food.txt of del
insert into cmb.food (name,price);

import พร้อมสร้างตารางให้ด้วย ตาม pc/ixf
import from "d:\import\abc.office.ixf" of ixf
 insert into abc.office;

import แบบเลือกระบุฟิลด์ 
import from "D:\DOCPOP\\mainMeter.csv" of del MODIFIED BY COLDEL|
METHOD P ( 1,8,9) MESSAGES "d:\df"

insert into  cti.tmpdata  (tmpid,pin,name);


import แบบใช้ tab delimited
import from "D:\user_outside.txt" OF DEL MODIFIED BY COLDEL0X09
insert into test.user_outside (id,authority);




ดูโครงสร้างตาราง และคำสั่ง create โดยละเอียด


db2look -d db_name -e -l -o db_name.dll -u userid -w password


คำสั่ง union VS union all

คำสั่ง union รวมรายการ ถ้ารายการซ้ำกัน
คำสั่ง union all  ไม่รวมรายการ ถ้ารายการซ้ำกัน


ใส่ลำดับที่ กำกับ ทุกรายการ โดยเรียงลำดับจากฟิลด์ fname และตัดมาตั้งแต่ลำดับที่ 1-500

select * from
( select col.*,row_number() over (order by col.fname asc) as rn from 
( select 'คนไทย' AS human,title,fname,lname from cmb.emp1
union all 
 select 'คนจีน' AS human,title,fname,lname from cmb.emp2 
) as col 
) as allrows
where rn between 1 and 500 with ur


with ur ต่อท้ายคำสั่ง

คำสั่ง with ur จะใช้เพื่อให้สามารถ เข้าไป query ข้อมูลในตารางที่กำลังมีการปรับปรุงข้อมูลอยู่ เช่น update delete ได้โดยไม่ต้องรอให้ให้การทำงานปรับปรุงข้อมูลในตารางนั้นเสร็จก่อน


คำสั่ง update

update  user a
set uid=(select uid from  cmb.name b where a.id=b.id)
where mon='06' and year='2558' and office='10058' and (uid is null or length(rtrim(uid))<11) with ur;


แก้ปัญหากรณีข้อมูลในฟิลด์ มีการตัดบรรทัด ทำให้ข้อมูลใช้ไม่ได้
ให้ใช้คำสั่งนี้ในการดึงข้อมูลออกมา

select field1,field2,REPLACE(REPLACE(name, CHR(10), ' '), CHR(13), ' ')  from xxx.ooo2014
where year=2014 and mon=1
 order by tmpid asc with ur;


แทนค่า null ในฟิลด์ ให้เป็นเลข 0

coalesce(a.TOT,0)


ใช้ pipe ต่อข้อความ

SELECT   a.title||' '||a.name as name FROM  cmb.user a

ผู้ดูแล


- kill เตะ user  ออก
db2 list application |grep ตามด้วยชื่อ user |awk '{print $3}' |xargs -i db2 force application 'ตามด้วยชื่อ user'




ข้อควรระวัง


- คำสั่ง in ทำงานเร็วกว่า not in มาก เท่าที่ทดลองระยะเวลาต่างกันประมาณ 4 เท่า  ให้พยายามใช้คำสั่ง in แทน not in








ไม่มีความคิดเห็น:

แสดงความคิดเห็น