数据库问题?求解答 oracle数据库,怎么实行主表和子表的数据同时添加,比如说订单表和订单明细表,当我在一个表单里面添加时,那么明细表的外键,也就是订单表的主键怎么获取到明细表中,听别人说用事务或触发器,做了下,没有做出来,请大哥们解答 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 create table dept(deptid int);create table emp(empid int, deptid int);create or replace trigger trg_testbefore insert or update of deptid on emp for each rowbegin insert into dept(deptid) select :new.deptid from dual where not exists(select * from dept where deptid = :new.deptid);end;SQL> insert into emp(empid, deptid) values (1, 1); 1 row inserted SQL> select * from dept; DEPTID--------------------------------------- 1 SQL> insert into emp(empid, deptid) values (2, 2); 1 row inserted SQL> select * from dept; DEPTID--------------------------------------- 1 2 SQL> update emp set deptid = 3 where empid = 1; 1 row updated SQL> select * from dept; DEPTID--------------------------------------- 1 2 3我不喜欢触发器 不太建议用触发器1)要么在程序中控制1)要么表结构考虑重建一下,利用约束,ON UPDATE CASCADE的关键字 rman catalog的问题 oracle数据库in语句中用regexp_replace的问题 如何写一条把多个表记录合并的SQL语句 oracle transparent gateway 连 5个库 求oracle基于web 的管理相关资料 远程培训的风险性 config.ora文件可以没有吗,没有了是不是在init.ora中有什么参数标明 ERWIN能不能自动 显示目标数据库中的表与表之间的关系呢? 老傻求SQL 怎样在JDBC中调用Oracle的一个存储过程,并且此存储过程返回一个结果集,放在ResultSet中? oracle试题 修改固定列值
create table dept(deptid int);
create table emp(empid int, deptid int);create or replace trigger trg_test
before insert or update of deptid on emp for each row
begin insert into dept(deptid)
select :new.deptid
from dual
where not exists(select * from dept where deptid = :new.deptid);
end;
SQL> insert into emp(empid, deptid) values (1, 1);
1 row inserted
SQL> select * from dept;
DEPTID
---------------------------------------
1
SQL> insert into emp(empid, deptid) values (2, 2);
1 row inserted
SQL> select * from dept;
DEPTID
---------------------------------------
1
2
SQL> update emp set deptid = 3 where empid = 1;
1 row updated
SQL> select * from dept;
DEPTID
---------------------------------------
1
2
3
我不喜欢触发器
1)要么在程序中控制
1)要么表结构考虑重建一下,利用约束,ON UPDATE CASCADE的关键字