การประยุกต์ใช้
Sequence เพื่อสร้างตัวเลข Automatic ให้กับ Table ใน Oracle Database
บางครั้งการทำงานเกี่ยวกับการจัดเก็บข้อมูลลง
Table จะมีข้อมูลบางส่วนที่ถูกจัดเก็บแบบ Sequence ซึ่งส่วนใหญ่จะเป็นข้อมูลจำพวก
Id ต่างๆ ถ้าเป็น Access ก็จะมี Auto running number ให้ หรือ worst
case เราก็จะ Select Id ล่าสุดมา แล้วบวกเข้าไป 1 แล้วจัดเก็บลงไปอีกที
ใน
Article นี้เราจะแนะนำวิธีการทำงานอีกแบบหนึ่ง ซึ่งสะดวกกว่า นั่นคือ
การใช้ Sequence และ Trigger ซึ่งเป็น Utility หนึ่งที่ Oracle มีไว้ให้ใช้งาน
ขั้นตอนแรก
เราสร้าง Table ที่มีการเก็บข้อมูลแบบ Sequence ขึ้นมาก่อน ดังตัวอย่างข้างล่าง
Table ตัวอย่างที่สร้างขึ้นมาเป็น Table เก็บข้อมูลของ Customers
โดยจะมี Field ชื่อ id มี type เป็น number และกำหนด constraint
ไว้ให้เป็น primary key ชื่อ pk_cust
create
table customers (
id number(5) constraint pk_cust primary key,
userid varchar2(50),
password varchar2(50),
first_name varchar2(100),
last_name varchar2(100),
email varchar2(100),
phone varchar2(50),
address varchar2(200),
credit varchar2(10)
);
|
เมื่อเราสร้าง
Table ดังตัวอย่างข้างบนแล้ว ขั้นตอนต่อไป คือ ทำการสร้าง Sequence
ซึ่งในตัวอย่างนี้ เราจะสร้าง Sequence ชื่อ customers_seq โดยใช้
command นี้
create
sequence customers_seq;
|
ขั้นต่อไป
เพื่อความสะดวก และง่ายต่อการจัดเก็บข้อมูลลง Table นี้ เราก็สร้าง
Trigger ขึ้นมาตัวหนึ่ง ให้ทำหน้าที่ check ว่ามีการ insert ข้อมูลลง
Table customers หรือไม่ ถ้ามีการ Insert ข้อมูลลง Table customers
ตัว trigger นี้จะ run sequence ของ field id ใน Table customers
ให้
ดังตัวอย่างในนี้
เราจะสร้าง Trigger ชื่อ customers_ins_trig ให้คอย check ว่าก่อนที่จะ
insert ข้อมูลลง Table customers ให้ไป select ค่า sequence ต่อไปของ
id ที่มีอยู่ แล้วนำไปจัดเก็บไว้ในค่า seq_val ที่เรา declare ไว้
หลังจากนั้นก็ assign ค่า id ใหม่ให้เท่ากับค่า seq_val นี้
create
or replace trigger customers_ins_trig
before insert on customers
for each row
declare
seq_val number;
begin
select customers_seq.nextval
into seq_val from dual;
:new.id := seq_val;
end;
|
เมื่อจะ
Insert ข้อมูลลง Table customers เราก็ไม่ต้องใส่ค่า id เอง ตัว
Trigger จะทำงานให้เราอัตโนมัติ
ทดสอบการทำงานของ
Sequence และ Trigger โดย Insert ข้อมูลลง Table customers ดังตัวอย่างนี้
insert
into customers ( userid, password, first_name, last_name,
email, phone, address, credit )
values ( 'demo1', 'demo1' , 'Exzilla OpenWorld1', 'Campground
Demo', 'tooldemo@exzilla.net', '650-718-7000', '500 Exzilla
Parkway I, Redwood Shores', 'EXCELLENT' );
insert
into customers ( userid, password, first_name, last_name,
email, phone, address, credit )
values ( 'teddy', 'teddybear' , 'Exzilla OpenWorld', 'Campground
Demo', 'teddy@exzilla.net', '650-712-7000', '800 Exzilla
Parkway II, Bluewood Shores', 'POOR' );
insert
into customers ( userid, password, first_name, last_name,
email, phone, address, credit )
values ( 'archer', 'archerB' , 'Exzilla OpenWorld', 'Campground
Demo', 'archer@exzilla.net', '650-248-5555', '550 Exzilla
Parkway III, Blackwood Shores', 'GOOD' );
commit;
|
จะสังเกตว่า
เราไม่ต้องใส่ค่า id แต่ถ้าเรา Select ข้อมูลออกมา ค่า id จะ run
sequence ให้อย่างถูกต้อง
เท่านี้เราก็สามารถ
Insert ข้อมูลลง Table customers ที่มีการ run sequence ของ field
id โดยที่เราไม่ต้องไปเขียน code ที่ front-end อีก
|