Oracle:触发将相关表B中的行替换为空间表A,以获取具有相同ID

时间:2018-03-26 13:41:13

标签: sql oracle triggers arcgis

数据库/触发图

Database / Trigger Diagram

我在Oracle空间数据库中工作,我想知道每次在表B中更新匹配记录时是否有人可以向我显示一个SQL触发器来替换表A中的整行。所以换句话说当一个记录在表B通过表A中的匹配F_ID更新相同的记录,用表B中的新数据替换。

这个奇怪系统的原因是由于ArcGIS收集器应用程序的权限级别。我们希望现场工作人员能够通过应用程序编辑所有列,但无法创建或移动任何资产。另一个主要原因是保持数据模型的完整性,并将所有其他信息放在相关表中。这种特殊设置将允许工作人员编辑相关表中的数据而不具有对主表的编辑权限(因此他们不能移动资产的位置),然后触发器将使用新数据更新表A.

表B是一个相关表,实际上列数多于表A,表A是空间表,其列仅与数据模型相关。

原谅我的不良触发器我仍在学习与他们合作并且有限制 了解如何使用它们。我知道我的这一切都错了,可能需要一个if声明,如果表B F_ID ==表A F_ID然后替换等等。

表B也是与表A的多对一关系。这背后的想法是表B将保留所有先前资产的记录,因为我们有一个资产管理软件,其相关的工作订单基于另一个唯一的T_ID字段。 F_ID表示资产位置ID,T_ID是资产的唯一ID,因此资产管理软件将能够保留指向旧资产的链接以进行记录和分析。

我很感激有用的反馈/帮助,并希望避免被告知我正在尝试的是可怕的或破坏数据库管理规范等。请帮助我,如果不是至少是好的和建设性的。我非常感谢能提供的任何帮助。

create or replace TRIGGER "REPLACE_RDATA" AFTER INSERT ON TABLE B 
FOR EACH ROW 
BEGIN
  INSERT INTO TABLE A (F_ID,DBH,SPECIES,HEIGHT,FAMILY,NOTES)
  VALUES (:new.DBH,:new.SPECIES,:new.HEIGHT,:new.FAMILY,:new.NOTES);
END;

例如:

表A

F_ID,   DBH,   Species,   Height,   Family,   Notes,   T_ID
1,   10.5,   Acer rubrum,   25,   Sapindaceae,   Gifted by person xyz,   1
2,   28.2,   Carya illinoinensis,   39,   Juglandaceae,   Next to building 2,   2
3,   26,   Pinus virginiana,   52.5,   Pinaceae,   Planted by xyz for opening celebration,   3

表B

F_ID,   DBH,   Species,   Height,   Family,   Notes,   T_ID
1,   10.5,   Acer negundo,   25,   Sapindaceae,   Gifted by person xyz: misidentified,   1
2,   31,   Carya illinoinensis,   42,   Juglandaceae,   Next to building 2,   2
3,   26,   Pinus virginiana,   52.5,   Pinaceae,   Planted by xyz for opening celebration,   3
2,   3,   Carya ovata,   15,   Juglandaceae,   Replaced the pecan tree,   4

更新了表A

F_ID,   DBH,   Species,   Height,   Family,   Notes,   T_ID
1,   10.5,   Acer negundo,   25,   Sapindaceae,   Gifted by person xyz: misidentified,   1
2,   3,   Carya ovata,   15,   Juglandaceae,   replaced the pecan tree,   2
3,   26,   Pinus virginiana,   52.5,   Pinaceae,   Planted by xyz for opening celebration,   3

2 个答案:

答案 0 :(得分:0)

假设表A中的每一行都有一行。

更改触发器,以便在更新时触发。 插入时插入table_A,更新时更新table_a。

create or replace TRIGGER "REPLACE_RDATA" AFTER INSERT OR UPDATE ON TABLE B 
FOR EACH ROW 
BEGIN
  IF INSERTING THEN
     INSERT INTO TABLE A (F_ID,DBH,SPECIES,HEIGHT,FAMILY,NOTES)
     VALUES (:new.DBH,:new.SPECIES,:new.HEIGHT,:new.FAMILY,:new.NOTES);
  ELSE
     UPDATE TABLE_A 
     set DBH = :new.DBH
     ... -- rest of the columns
     where F_ID = :new.F_ID;
  END IF;
END;

答案 1 :(得分:0)

只要向table_b添加一行,就可以使用一个过程(而不是触发器),它会插入table_b并更新table_a。示例(分别使用Oracle 12c和11g测试):

Table_A - “master”,包含所有记录植物的列表,包括最新观察结果:

create table table_a (
  f_id number unique     
, dbh  number       
, species varchar2(64) 
, height number(6,2)     
, family varchar2(64) 
, notes varchar2(4000) 
, t_id number
) ;

insert into table_a ( f_id, dbh, species, height, family, notes, t_id )
select 1                 as F_ID
, 10.5                   as DBH
, 'Acer rubrum'          as Species
, 25                     as Height
, 'Sapindaceae'          as Family
, 'Gifted by person xyz' as Notes
, 1000                   as T_ID from dual union all
select 2, 28.2, 'Carya illinoinensis', 39, 'Juglandaceae', 'Next to building 2', 2000 from dual union all
select 3, 26, 'Pinus virginiana', 52.5, 'Pinaceae', 'Planted by xyz for opening celebration', 3000 from dual ;

Table_A包含......

SQL> select * from table_a ;
F_ID  DBH   SPECIES              HEIGHT  FAMILY        NOTES                                   T_ID  
1     10.5  Acer rubrum          25      Sapindaceae   Gifted by person xyz                    1000  
2     28.2  Carya illinoinensis  39      Juglandaceae  Next to building 2                      2000  
3     26    Pinus virginiana     52.5    Pinaceae      Planted by xyz for opening celebration  3000  

表_ - “观察”(空)

create table table_b (
  f_id number references table_a( f_id )     
, dbh  number       
, species varchar2(64) 
, height number(6,2)     
, family varchar2(64) 
, notes varchar2(4000) 
, t_id number unique
) ;

{1}允许我们向table_b添加记录的过程,{2}在适当的时候更新table_a。

包装规格

create or replace package dendrology
is
  procedure add_record(
     f_id_     number
   , dbh_      number
   , species_  varchar2
   , height_   number
   , family_   varchar2
   , notes_    varchar2
   , t_id_     number
  );
end;
/

包体

create or replace package body dendrology
is
  procedure add_record(
     f_id_     number
   , dbh_      number
   , species_  varchar2
   , height_   number
   , family_   varchar2
   , notes_    varchar2
   , t_id_     number
  ) is
  begin
    -- insert into table_b first
    insert into table_b ( f_id, dbh, species, height, family, notes, t_id )
      values (  f_id_, dbh_, species_, height_, family_, notes_, t_id_  );
    commit ;
    -- update table_A
    update table_a
    set dbh = dbh_
      , species = species_
      , height = height_
      , family = family_
      , notes = notes_
      , t_id = t_id_
    where f_id = f_id_ ;
    commit ;
  end add_record ;
end dendrology;
/

测试程序:添加一些“观察”。

begin
  dendrology.add_record(  
    1, 10.5, 'Acer negundo', 25, 'Sapindaceae', 'Gifted by person xyz: misidentified', 1000 
  ) ;
  dendrology.add_record(  
    2, 31, 'Carya illinoinensis', 42, 'Juglandaceae', 'Next to building 2', 2000
  ) ;
  dendrology.add_record(  
    3, 26, 'Pinus virginiana', 52.5, 'Pinaceae', 'Planted by xyz for opening celebration', 3000
  ) ;
  dendrology.add_record( 
    2,  3, 'Carya ovata', 15, 'Juglandaceae', 'Replaced the pecan tree',   4000
  );
end;
/

检查:TABLE_B

SQL> select * from table_b order by t_id ;
F_ID  DBH   SPECIES              HEIGHT  FAMILY        NOTES                                   T_ID  
1     10.5  Acer negundo         25      Sapindaceae   Gifted by person xyz: misidentified     1000  
2     31    Carya illinoinensis  42      Juglandaceae  Next to building 2                      2000  
3     26    Pinus virginiana     52.5    Pinaceae      Planted by xyz for opening celebration  3000  
2     3     Carya ovata          15      Juglandaceae  Replaced the pecan tree                 4000 

检查:TABLE_A

SQL> select * from table_a ;
F_ID  DBH   SPECIES           HEIGHT  FAMILY        NOTES                                   T_ID  
1     10.5  Acer negundo      25      Sapindaceae   Gifted by person xyz: misidentified     1000  
2     3     Carya ovata       15      Juglandaceae  Replaced the pecan tree                 4000  
3     26    Pinus virginiana  52.5    Pinaceae      Planted by xyz for opening celebration  3000

Dbfiddle here。注意:我们假设在调用过程时已填充TABLE_A。