全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1199
推到 Plurk!
推到 Facebook!

難題!!!關于在MS Sqlserver中建立Foreign Key約束的疑問

尚未結案
longjibo
一般會員


發表:18
回覆:18
積分:12
註冊:2002-11-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-01-21 07:40:59 IP:218.16.xxx.xxx 未訂閱
對于兩個表PoHead,Podetail,兩者通過PO_NO進行聯系 其中PoHead結構如下 PO╴NO,PO╴DATE,BUYER Podetail結構如下 PO_NO,SEQ_NO,ITEM_NO 我想在修改了Pohead表中的某個PO_NO值時,SqlServer會自動更新Podetail 表中相應的記錄中的PO_NO值 請問是否可以通過中Podetail中建立一個Foreign Key約束實現. 如果不行的話,有無其它實現辦法讓MSSql Server自動進行更新?
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-01-21 15:49:39 IP:61.219.xxx.xxx 未訂閱
引言: 請問是否可以通過中Podetail中建立一個Foreign Key約束實現.
不行. Foreign Key Constraint 當 Podetail 已經存在有 Pohead.PO_NO 一定是 restrict (限制) Pohead.PO_NO update.
引言: 如果不行的話,有無其它實現辦法讓MSSql Server自動進行更新?
應用 trigger 如下:
create trigger [TriggerName] on PoHead for update as
declare @ins_pn_no [PoHead.PO_NO.datatype],
        @del_pn_no [PoHead.PO_NO.datatype]
begin
  if @@rowcount = 0 return
  if update(PO_NO)
  begin
    declare cur_ins scroll cursor 
       for select PO_NO from inserted
    declare cur_del scroll cursor 
       for select PO_NO from deleted
    open cur_ins
    open cur_del
    fetch cur_ins into @ins_pn_no
    fetch cur_del into @del_pn_no
    while (@@fetch_status=0)
      begin
        update Podetail
          set
            PO_NO=@ins_po_no
          from Podetail
          where
            PO_NO=@del_po_no
        fetch cur_ins into @ins_pn_no
        fetch cur_del into @del_pn_no
      end
    close cur_ins
    close cur_del
    deallocate cur_ins
    deallocate cur_del
  end
end
go    
/* 使用中文很辛苦,中華男兒當自強 */
Mickey
版主


發表:77
回覆:1882
積分:1390
註冊:2002-12-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-01-21 15:57:14 IP:61.219.xxx.xxx 未訂閱
引言:
引言: 請問是否可以通過中Podetail中建立一個Foreign Key約束實現.
不行. Foreign Key Constraint 當 Podetail 已經存在有 Pohead.PO_NO 一定是 restrict (限制) Pohead.PO_NO update.
引言: 如果不行的話,有無其它實現辦法讓MSSql Server自動進行更新?
應用 trigger 如下:
create trigger [TriggerName] on PoHead for update as
declare @ins_pn_no [PoHead.PO_NO.datatype],
        @del_pn_no [PoHead.PO_NO.datatype]
begin
  if @@rowcount = 0 return
  if update(PO_NO)
  begin
    declare cur_ins scroll cursor 
       for select PO_NO from inserted
    declare cur_del scroll cursor 
       for select PO_NO from deleted
    open cur_ins
    open cur_del
    fetch cur_ins into @ins_pn_no
    fetch cur_del into @del_pn_no
    while (@@fetch_status=0)
      begin
        update Podetail
          set
            PO_NO=@ins_po_no
          from Podetail
          where
            PO_NO=@del_po_no
        fetch cur_ins into @ins_pn_no
        fetch cur_del into @del_pn_no
      end
    close cur_ins
    close cur_del
    deallocate cur_ins
    deallocate cur_del
  end
end
go    
"Pohead" delete trigger "Podetail" insert trigger "Podetail" update trigger 可能都須建置(因為不建立 Foreign Key ) /* 使用中文很辛苦,中華男兒當自強 */
/* 使用中文很辛苦,中華男兒當自強 */
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-01-22 14:36:10 IP:210.58.xxx.xxx 未訂閱
Foreign Key 只是一個 constraint, 用以建立 master 與 detail 的 table relation. 但不會自動更新相對應的欄位值, 也就是在作 insert, update, delete 時, 系統會檢驗是否符合這個 constraint, 若不符合的 話, 將會 raise error, 以避免資料的不正確. 要達成自動更新的功能, 就只能利用 trigger 來達到, 隨著你 ap 會異 動的狀況來寫相對應的 trigger. 例入更新 pohead 的 po_no 時, 要更 新 podetail 的 po_no, 但是這個條件會因為你的程式需求而有不同, 所 以 trigger 的部分, 就必須知道 ap 的操作方式才能知道如何進行 trigger 的撰寫.
系統時間:2024-06-16 15:06:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!