Oracle:触发将行从相关 table B 替换为空间 table A 以获取具有相同 ID 的记录
Oracle: Trigger to replace row from related table B to spatial table A for records with equal IDs
数据库/触发器图
我在 Oracle 空间数据库中工作,我想知道是否有人可以向我展示一个 SQL 触发器来替换 table A 中的整行,每次在 table B。换句话说,当 table B 中的记录通过匹配 table 中的 F_ID 更新相同的记录时,A 将替换为 table 中的新数据] B.
这个奇怪系统的原因是由于 ArcGIS 收集器应用程序的权限级别。我们希望现场工作人员能够通过应用程序编辑所有列,但不能创建或移动任何资产。另一个主要原因是为了保持我们数据模型的完整性,并将所有其他信息放在相关 table 中。这种特殊设置将允许工作人员编辑相关 table 中的数据,而无需编辑主要 table 的权限(因此他们无法移动资产的位置),然后触发器将更新 table A 与新数据。
Table B 是一个相关的 table 并且实际上比 table A 有更多的列并且 table A 是空间 table 只有列与数据模型相关。
请原谅我的错误触发我仍在学习与他们一起工作并且有一个限制
了解如何与他们合作。我知道我完全错了,可能需要一个关于 if table B F_ID == table A F_ID 然后替换等的 if 语句
另外tableB与tableA是多对一的关系。这背后的想法是tableB会保留之前所有资产的记录,因为我们有一种资产管理软件,具有基于另一个独特 T_ID 字段的相关工作订单。 F_ID 表示资产位置 ID,T_ID 是资产的唯一 ID,因此资产管理软件将能够为旧资产保留 link 以供记录和分析。
我将不胜感激 feedback/help 并希望避免被告知我正在尝试的事情很糟糕或违反了数据库管理规范等。请帮助我,如果不是,至少要友善和有建设性。我真的很感激能提供的任何帮助。
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;
例如:
Table一个
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
TableB
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
已更新Table一个
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
假设 table A 中有一行对应 table B 中的每一行。
更改触发器,使其在更新时也触发。
插入时插入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;
您可以使用一个过程(而不是触发器),只要向 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
Table_B - 对于 "observations"(空)
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;
/
测试程序:添加一些"observations".
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 已经填充。
数据库/触发器图
我在 Oracle 空间数据库中工作,我想知道是否有人可以向我展示一个 SQL 触发器来替换 table A 中的整行,每次在 table B。换句话说,当 table B 中的记录通过匹配 table 中的 F_ID 更新相同的记录时,A 将替换为 table 中的新数据] B.
这个奇怪系统的原因是由于 ArcGIS 收集器应用程序的权限级别。我们希望现场工作人员能够通过应用程序编辑所有列,但不能创建或移动任何资产。另一个主要原因是为了保持我们数据模型的完整性,并将所有其他信息放在相关 table 中。这种特殊设置将允许工作人员编辑相关 table 中的数据,而无需编辑主要 table 的权限(因此他们无法移动资产的位置),然后触发器将更新 table A 与新数据。
Table B 是一个相关的 table 并且实际上比 table A 有更多的列并且 table A 是空间 table 只有列与数据模型相关。
请原谅我的错误触发我仍在学习与他们一起工作并且有一个限制 了解如何与他们合作。我知道我完全错了,可能需要一个关于 if table B F_ID == table A F_ID 然后替换等的 if 语句
另外tableB与tableA是多对一的关系。这背后的想法是tableB会保留之前所有资产的记录,因为我们有一种资产管理软件,具有基于另一个独特 T_ID 字段的相关工作订单。 F_ID 表示资产位置 ID,T_ID 是资产的唯一 ID,因此资产管理软件将能够为旧资产保留 link 以供记录和分析。
我将不胜感激 feedback/help 并希望避免被告知我正在尝试的事情很糟糕或违反了数据库管理规范等。请帮助我,如果不是,至少要友善和有建设性。我真的很感激能提供的任何帮助。
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;
例如:
Table一个
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
TableB
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
已更新Table一个
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
假设 table A 中有一行对应 table B 中的每一行。
更改触发器,使其在更新时也触发。 插入时插入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;
您可以使用一个过程(而不是触发器),只要向 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
Table_B - 对于 "observations"(空)
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;
/
测试程序:添加一些"observations".
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 已经填充。