If i insert record in table A and these record should update in table B by using Trigger.How to achieve this.

Answers were Sorted based on User's Feedback



If i insert record in table A and these record should update in table B by using Trigger.How to achi..

Answer / anil

CREATE table trg_tab
(ename VARCHAR(10),
sal NUMBER);
/
CREATE table log_tab
(usr VARCHAR(10),
upd DATE,
ename VARCHAR(10),
sal NUMBER
);
/
CREATE OR REPLACE TRIGGER log_trg
AFTER INSERT ON trg_tab
FOR EACH ROW
DECLARE
v_user VARCHAR(10);
BEGIN
SELECT USER
INTO v_user
FROm DUAL;
INSERT INTO log_tab
VALUES (v_user,SYSDATE,:NEW.ename,:NEW.sal);
END;
/
INSERT INTO trg_tab
SELECT ename,sal FROm emp;
/
SELECT * FROm log_tab;
/

Is This Answer Correct ?    7 Yes 1 No

If i insert record in table A and these record should update in table B by using Trigger.How to achi..

Answer / abhishek jaiswal

Table 1(U_register)
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 FIRST_NAME                 PK                        VARCHAR2(15)
 LAST_NAME                                 NOT NULL VARCHAR2(15)
 DOB                                       NOT NULL DATE
 USER_NAME                                 NOT NULL VARCHAR2(15)
 NEW_PASSWORD                              NOT NULL VARCHAR2(15)
 CONFIRM_PASSWORD                          NOT NULL VARCHAR2(15)
 U_ID                                      NOT NULL NUMBER

Table 2(Login_detail)

 Name                                      Null?    Type
 -------------------------FK---------------- -------- ----------------------------
 ID                                        NOT NULL NUMBER
 USER_NAME                                          VARCHAR2(15)
 NEW_PASSWORD                                       VARCHAR2(15)

Now,We have to write trigger to insert of column 'user_name' and 'new_password' in login_detail table.
To do this we will use After insert trigger.

create or replace 
trigger tgr_login
after insert on u_register
for each row
begin 
insert into login_detail
values (:new.u_id,:new.user_name,:new.new_password);
end tgr_login;

---and It will work .

Is This Answer Correct ?    2 Yes 0 No

If i insert record in table A and these record should update in table B by using Trigger.How to achi..

Answer / abhishekjaiswal

Table 1(u_register) Parent table
 Name                                      Null?                           Type
 ----------------------------------------- -------- ----------------------
 FIRST_NAME                                                             VARCHAR2(15)
 LAST_NAME                           NOT NULL                  VARCHAR2(15)
 DOB                                       NOT NULL                   DATE
 USER_NAME                           NOT NULL                 VARCHAR2(15)
 NEW_PASSWORD                   NOT NULL                 VARCHAR2(15)
 CONFIRM_PASSWORD           NOT NULL                 VARCHAR2(15)
 U_ID                                        NOT NULL                 NUMBER

table 2(login_detail) Child table
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------
 ID                                        NOT NULL NUMBER
 USER_NAME                                          VARCHAR2(15)
 NEW_PASSWORD                                       VARCHAR2(15)

Now we will create trigger to insert column 'user_name','new_password' of u_register into column 'USER_NAME ','NEW_PASSWORD' of login_detail.We will use after insert trigger as 

create or replace 
trigger tgr_login
after insert on u_register
for each row
begin 
insert into login_detail
values (:new.u_id,:new.user_name,:new.new_password);
end tgr_login;

It will work for sure.

Is This Answer Correct ?    1 Yes 0 No

If i insert record in table A and these record should update in table B by using Trigger.How to achi..

Answer / shwetha

CREATE TABLE A1(ID NUMBER(2),NAMES VARCHAR2(30))

CREATE TABLE B1(ID NUMBER(2),NAMES VARCHAR2(30),STATUS VARCHAR2(10))

CREATE OR REPLACE TRIGGER A1B1
AFTER INSERT ON A1
FOR EACH ROW
BEGIN
INSERT INTO B1 VALUES(:NEW.ID, :NEW.NAMES,'Y');
END;

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL PLSQL Interview Questions

Can cursors be part of a trigger body?

0 Answers  


What is the difference between delete and truncate statement in sql?

0 Answers  


Can we insert in view in sql?

0 Answers  


Define the select into statement.

0 Answers  


Can we rename a column in the output of sql query?

0 Answers  






What is fmtonly in sql?

0 Answers  


Can we use commit inside a trigger?

0 Answers  


In a PL/SQL block,which loop type should be used in a performance point of view & Why (as both loops can do the same task) 1) open - Fetch loop 2) for loop

4 Answers   JDA,


Explain what is a database?

0 Answers  


What is scalar function?

0 Answers  


How to change the order of columns in Oracle SQL Plus ?

0 Answers   MCN Solutions,


Explain what is an index?

0 Answers  


Categories