งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Integrity Constraints

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Integrity Constraints"— ใบสำเนางานนำเสนอ:

1 Integrity Constraints

2 Integrity Constraints
หมายถึงกรรมวิธีที่ DBMS อนุญาตให้ผู้ใช้ ใส่กฎเกณฑ์ลงไปบนฐานข้อมูลเพื่อให้แน่ใจว่าข้อมูลจะถูกต้อง กล่าวคือข้อมูลอยู่ภายใต้กฎเกณฑ์ตามที่ผู้ใช้กำหนด ภายหลังจากที่มีการเปลี่ยนแปลงเกิดขึ้น การเปลี่ยนแปลงที่ว่านี้ อาจเป็นการแก้ไขข้อมูล ลบข้อมูลเดิม การเพิ่มข้อมูลใหม่ การใส่ Integrity Constraints เพื่อเป็นการบอกให้ DBMS ทราบถึงข้อกำหนด เงื่อนไขในการให้ค่าที่เป็นไปได้กับตัวข้อมูลเพื่อป้องกันการ update ค่าที่ไม่ตรงตามกฎเข้าไปในฐานข้อมูล

3 การควบคุมความคงสภาพความถูกต้อง
กฎเกณฑ์ควบคุมความคงสภาพความถูกต้อง แบ่งลักษณะการควบคุมออกเป็น 3 ประเภท Domain Constraints Referential Integrity Triggers

4 Domain Constraints เป็นการใส่กฎเกณฑ์ควบคุมความคงสภาพขั้นพื้นฐานที่สุด เพราะเป็นการใส่กฎเกณฑ์เพื่อกำหนด data type และเพื่อกำหนดขอบเขตของค่าต่างๆ ที่เป็นไปได้ให้กับ attributes ดังนั้น หากมีการ insert หรือ update ฐานข้อมูล DBMS จะทำการตรวจสอบว่าค่าใหม่นั้นอยู่ในกรอบของ domain หรือไม่

5

6 Referential Integrity
คือการตั้งกฎเกณฑ์ควบคุมความคงสภาพความสัมพันธ์ระหว่าง primary key ของรีเลชั่นหนึ่ง กับ foreign key ของอีกรีเลชั่นหนึ่ง Referential Integrity: Basic Concept กฎเกณฑ์ควบคุมความคงสภาพระบุว่า ถ้ามี 2 รีเลชั่น (r กับ s) ที่มีความสัมพันธ์กัน ถ้าในรีเลชั่น s ซึ่งมี foreign key อันเป็นคีย์นอกที่อ้างอิงถึงคีย์หลักในรีเลชั่น r ดังนั้นสำหรับทุก ๆ ค่าของ foreign key ใน s จะต้องมีค่าเท่ากับค่าของ attributes ที่ถูกกำหนดเป็นคีย์หลักใน tuple ใด tuple หนึ่ง บนรีเลชั่น r ถ้าสมมุติมีการ natural Join ของ r กับ s อาจเกิดกรณีที่ว่าบางแถว (tr) ในรีเลชั่น r ไม่มีคู่ join กับแถว (ts) ใน s กล่าวคือ There is no ts such that tr[R  S] = ts[R  S] เราเรียกแถวที่ join กับแถวอื่นในอีกรีเลชั่นหนึ่งไม่ได้เลยว่า dangling tuples.

7  (r2)  k (r1) กฎการอ้างอิง สามารถเขียนได้ว่า
foreign key ของ r2 ต้องเป็น subset ของ Primary key ของ r1 ความสำคัญชองกฎข้อนี้คือ เมื่อมีการอ้างอิงจากรีเลชั่นหนึ่งไปยังอีกรีเลชั่นหนึ่งแล้ว ระบบการจัดการฐานข้อมูลต้องรับประกันว่าข้อมูลในรีเลชั่นทั้ง 2 จะต้องมีตัวตนเสมอ

8

9

10 Triggers Triggers are rules managed by a DBMS. Because a trigger involves an event, a condition, and a sequence of actions, it also is known as an event-condition-action rule. Writing the action part or trigger body is similar to writing a procedure or a function except that a trigger has no parameters. Triggers are executed by the rule system of the DBMS not by explicit calls as for procedures and functions. Triggers เป็น stored Procedure ประเภทหนึ่งซึ่งจะ effect ทุกครั้งที่มีการเปลี่ยนแปลงข้อมูลภายในตารางเกิดขึ้น ทั้งนี้เป็นผลมาจากการทำ Insert, Update และ Delete การทำงานของ Triggers จะคล้ายกับการปลุกเพื่อให้มีการทำงานเกิดขึ้นตามเหตุการณ์ที่เกี่ยวข้องกับการเปลี่ยนแปลงข้อมูล Triggers ควรถูกใช้ในเรื่องของการดูแลข้อมูลให้ถูกต้องทั้ง Data Integrity และ Referential Integrity ตลอดจนในส่วนของ Encapsulate Business Rules

11 Motivation and Classification of Triggers
Triggers are widely implemented in DBMSs because they have a variety of uses in business applications. The typical uses of triggers :- Complex integrity constraints: Integrity constraints that cannot be declaratively specified by check constraints in CREATE TABLE statements. Transition constraints: Integrity constraints that compare the values before and after an update occurs. Update propagation: Update derived columns in related tables. Exception reporting: Create a record of unusual conditions as an alternative to rejecting a transaction.

12 Triggers, which are part of SQL:1999, classifies triggers by granularity, timing, and applicable event. For granularity, a trigger can involve each row affected by an SQL statement or an entire SQL statement. Row triggers are more common than statement triggers. For timing, a trigger can fire before or after an event. Typically, triggers for constraint checking fire before an event, while triggers updating related tables and performing other actions fire after an event. For applicable event, a trigger can apply to INSERT, UPDATE, and DELETE statements.

13 ระบบจะ execute คำสั่ง Trigger โดยอัตโนมัติ ขณะมีการปรับปรุงฐานข้อมูล การออกแบบกลไก Trigger เราต้องระบุ 1. ระบุเงื่อนไขการทำงานให้กับ Trigger 2. ระบุผลลัพธ์ที่ต้องการ และลำดับขั้นตอนการทำงานเพื่อให้ได้ผลลัพธ์นั้น ตัวอย่างการทำงานของ Triggers สมมุติให้ธนาคาร อนุญาตให้ลูกค้าเบิกเงินเกินบัญชี แต่ไม่ต้องการให้มียอดเงินฝากในรีเลชั่น account เป็นค่าติดลบ ดังนั้น ธนาคารอาจจัดการในลักษณะที่ตัดบัญชีเงินฝากของลูกค้าผู้นี้ให้เป็น 0 และทำการเพิ่มเรคอร์ดของลูกค้าเข้าไปในรีเลชั่น loan และ borrower โดยทางธนาคารจะนำเอาเลขที่บัญชีเงินฝาก (account_number) มาทำป็นเลขที่เงินกู้ (loan_number) และนำยอดที่เกินบัญชีมาลงไว้เป็นจำนวนเงินกุ้ จากตัวอย่างนี้ เงื่อนไขในการรัน Trigger คือ การทำการ update รีเลชั้น account เฉพาะแถวที่มีจำนวนเงินbalance เป็นค่าติดลบ

14

15 ทำความเข้าใจตาราง INSERTED และ DELETED
เมื่อมีการทำงานร่วมกับ Triggers จะเกิดตารางชั่วคราวพิเศษ 2 ตารางคือ Inserted และ Deleted โดยเก็บอยู่ที่หน่วยความจำและจะใช้งานได้เพียงภายในช่วงระยะการทำงานของ Triggers หนึ่งๆ เท่านั้น สำหรับตารางที่กล่าวถึงนี้ เป็นตารางเปล่าที่ยังไม่มีข้อมูลใดๆ แต่จะมีโครงสร้างทั้งชื่อและจำนวนคอลัมน์ ตลอดจนประเภทของข้อมูลเหมือนกันกับตารางที่ Triggers อาศัยอยู่ทุกประการ ในแต่ละครั้งของการเกิดเหตุการณ์ที่เกี่ยวข้องกับการเปลี่ยนแปลงแก้ไขข้อมูลจะเป็นให้มีการเพิ่มแถว ข้อมูลให้กับตารางพิเศษ ดังนี้ INSERT เมื่อมีการเพิ่มแถวข้อมูลให้กับตารางข้อมูลที่ Triggers อาศัยอยู่ ระบบจะเพิ่มแถวข้อมูลเดียวกันนี้ไปยังตาราง INSERTED ด้วย DELETE เมื่อมีการลบแถวข้อมูลออกจากตารางข้อมูลที่ Triggers อาศัยอยู่ ระบบจะเพิ่มแถวข้อมูลที่ถูกลบออกนี้ ไปเก็บไว้ยังตาราง DELETED ด้วย UPDATE เมื่อมีการอัพเดทข้อมูลในตารางข้อมูลที่ Triggers อาศัยอยู่ ระบบจะทำงาน 2 ขั้นตอน กล่าวคือ มีการนำแถวข้อมูลก่อนการอัพเดทไปเก็บไว้ โดยเพิ่มแถวข้อมูลดังกล่าวไปเก็บไว้ยังตาราง DELETED ก่อน จากนั้นระบบจะนำแถวข้อมูลที่ได้ถูกแก้ไขเรียบร้อยแล้วไปเก็บไว้ยังตาราง INSERTED ด้วย ตารางพิเศษที่เก็บข้อมูลเนื่องจากการใช้คำสั่ง INSERT, DELETE, และ UPDATE นี้ ช่วยให้ Triggers มีความสามารถในการตรวจสอบข้อมูลและดักจับการทำงานได้ตามต้องการ

16 CREATE TRIGGER borrow_insert on borrow for insert as update book
BookID BookName avail B01 Finance Y B02 Accounting B03 Database B04 Network B05 Compiler CREATE TRIGGER borrow_delete on borrow for delete as update book set avail = 'Y' where bookID in (select bookID from deleted); Create Trigger Book_Update On book For update As If update (BookID) Begin Update borrow Set BookID = (Select BookID from inserted where BookID in (Select BookID from deleted)) CREATE TRIGGER borrow_insert on borrow for insert as update book set avail = 'N' where BookID in (Select BookID from inserted);

17 ตัวอย่าง Triggers ที่ช่วย automate application ตาม business logic ทั่วๆไป
//create trigger overdraft_trigger after update of balance on account Create trigger overdraft_trigger after update on account Referencing new row as nrow For each row When nrow.balance < 0 Begin atomic insert into borrower (select customer_name, account_number from depositor where nrow.account_number = depositor.account_number) insert into loan values (nrow.account_number, nrow.branch_name, -nrow.balance) update account set balance = 0 where account.account_number = nrow.account_number end

18 CREATE TRIGGER tr_InsUpdtmpSales
On tmp_sales for insert, update as int = i.qty from inserted as i < 20 begin print 'No record to insert or update' print 'Because quantity must more than 20' rollback transaction end

19 CREATE TRIGGER tr_InsUpdtmpSales2
On tmp_sales for update as if update(qty) int = i.qty from inserted as i < 20 begin print 'No record to insert or update' print 'Because quantity must more than 20' ROLLBACK TRANSACTION end else print 'Triggers not done because qty column is not updated'

20 CREATE TRIGGER tr_DeltmpSales
On tmp_sales for delete as int = d.qty from deleted as d > 30 begin print 'No record to be deleted' print 'Because quantity more than 30' rollback transaction end

21 การทำ Cascading Delete ด้วย Triggers
Trigger มีประโยชน์อย่างมากในเรื่องของการทำ Cascading Delete กล่าวคือเมื่อมีการลบแถวข้อมูลจากตารางหลักแล้ว แถวข้อมูลของตารางรองที่มี Foreign key เชื่อมกับ Primary key ของตารางหลักจะถูกลบตามไปด้วย CREATE TRIGGER tr_DeltmpOrders On tmp_orders for delete as delete tmp_orderdetails from deleted as d where tmp_orderdetails.orderid = d.orderid; CREATE TRIGGER tr_DeltmpOrderDetails On tmp_orderdetails int raiserror ('%d rows are going to be deleted from tmpOrderDetails', 0,

22 CREATE TRIGGER tr_DelTitle
on tmp_titles after delete as delete tmp_titleAuthor from tmp_titleAuthor tmta, deleted d where tmta.title_id = d.title_id print 'Deleted from tmp_titleauthor Table‘ CREATE TRIGGER tr_DelTitleAuthor on tmp_titleauthor After delete as delete tmp_authors from tmp_authors tma, deleted d where tma.au_id = d.au_id print 'Deleted from tmp_authors Table'

23 CASE STUDY


ดาวน์โหลด ppt Integrity Constraints

งานนำเสนอที่คล้ายกัน


Ads by Google