PL SQL trigger When Insert a table insert into another Table - oracle

I attached two table below I need a trigger when insert do_chd table in same time insert in do_chd_hist table And DO_MST table to DO_MST_HIST TAbLE
create table do_mst
( fdo_no varchar2(14) not null,
fdo_date date not null,
code_db varchar2(11),
db_name varchar2(25),
code_sdb varchar2(11),
sdb_name varchar2(25),
code_sst varchar2(11),
sst_name varchar2(25),
constraint fdo_no_pk primary key(fdo_no)
);
create table do_mst_hist
( fdo_no varchar2(14) not null,
fdo_date date not null,
code_db varchar2(11),
db_name varchar2(25),
code_sdb varchar2(11),
sdb_name varchar2(25),
code_sst varchar2(11),
sst_name varchar2(25),
constraint fdo_no_pk primary key(fdo_no)
);
create table do_chd
( fdo_no varchar2(14) not null,
itemcode varchar2(11) not null,
name varchar(27) not null,
unit_price varchar2(11),
req_qty number(11) not null,
total_price number(14),
unit varchar2(7),
constraint fdo_no_fk foreign key(fdo_no)
references do_mst(fdo_no)
);
create table do_chd_hist
( fdo_no varchar2(14) not null,
itemcode varchar2(11) not null,
name varchar(27) not null,
unit_price varchar2(11),
req_qty number(11) not null,
total_price number(14),
unit varchar2(7),
constraint fdo_no_fk foreign key(fdo_no)
references do_mst(fdo_no)
);
Can anybody help me to write this trigger?

While I agree with what being said in the comments, I don't think there's a reason to think twice about writing up a create trigger syntax, and I'm happy to show you the solution to your problem:
CREATE OR REPLACE TRIGGER do_chd_insert_trg
AFTER INSERT do_chd
FOR EACH ROW
DECLARE
sal_diff number;
BEGIN
insert into do_chd_hist
values (new.fdo_no, new.itemcode, new.name, new.unit_price, new.req_try, new.total_price, new.unit);
END;
/
You can use this syntax to generate the trigger to the second table, after changing the respectable column names.
You can read more about oracle trigger syntax here -
https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm

Related

TRIGGERS_function

please help me to find the solution i have a project .
so my project is about a mobile repair store , we have some tables :
customers, mobiles, repairjob
//customer table
CREATE TABLE CUSTOMERS (
CUSTOMER_CPR NUMBER(9) PRIMARY KEY,
FIRST_NAME VARCHAR2(30) NOT NULL,
MID VARCHAR2(30)
LAST_NAME VARCHAR2(30) NOT NULL,
EMAIL VARCHAR2(30) NOT NULL UNIQUE,
HOME_TEL NUMBER(8) UNIQUE,
HOUSE_NO VARCHAR2(30) NOT NULL,
ROAD_NO VARCHAR2(30) NOT NULL,
BLOCK_NO VARCHAR2(30) NOT NULL,
CITY VARCHAR2(30) NOT NULL
);
//mobiles table
CREATE TABLE MOBILES (
MOB_ID NUMBER(8,0) PRIMARY KEY,
MODEL VARCHAR2(30) NOT NULL,
MOB_DECRIPTION VARCHAR2(100),
SERIAL_NUM VARCHAR2(30) UNIQUE,
MAKE VARCHAR2(50),
customer_cpr number(9),
);
//repair job table
CREATE TABLE REPAIR JOB (
JOB_NUM NUMBER primary key,
customer_cpr number(9)
EMP_ID NUMBER(8,0) NOT NULL,
DATE_RECEIVED DATE,
DATE_TO_RETURN DATE,
ITEM_ID NUMBER
);
so my question is to make a trigger function , we have assumed that the customer cannot have more than 3 repair jobs or can't fix more than 3 mobiles at one time until it finishes all. so how i make a triiger function ?

I am getting an error as invalid identifier but i couldnt find how

CREATE TABLE customer_details_1035
(
cust_ID Number(5) NOT NULL,
cust_last_name Varchar2(20) NOT NULL,
cust_mid_name Varchar2(4),
cust_first_name Varchar2(20),
account_no Number(5) Primary key,
account_type Varchar2(10) NOT NULL,
bank_branch Varchar2(25) NOT NULL,
cust_email Varchar2(30),
)
This is the error I get:
ORA-00904: : invalid identifier
Some dialects of SQL allow (or tolerate) a comma (,) after the last column specification in a create table statement.
According to the syntax diagrams, Oracle SQL doesn't allow this.
(So, the SQL parser will be looking for a column name (identifier) after the last ,. It finds a ) instead ... which is not a valid identifier.)
Solution: remove the extraneous comma.
CREATE TABLE customer_details_1035
(
cust_ID Number(5) NOT NULL,
cust_last_name Varchar2(20) NOT NULL,
cust_mid_name Varchar2(4),
cust_first_name Varchar2(20),
account_no Number(5) Primary key,
account_type Varchar2(10) NOT NULL,
bank_branch Varchar2(25) NOT NULL,
cust_email Varchar2(30), --Remove this comma(,)
);

Table using Foreign Key Oracle

I am having a problem linking two tables in oracle 11g express addition. I have looked on this site for assistance, but I am unable to find what I need.
My BOOKS table was loaded up using the following statement:
CREATE TABLE STATES(
ST VARCHAR2(2) PRIMARY KEY,
STATES VARCHAR2(20) NOT NULL
);
The CUSTOMERS table that I am trying to load is written as such:
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
FOREIGN KEY ST REFERENCES STATES(ST)
);
However, when I run the statement I get the following error:
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
I believe that the error is in the FOREIGN KEY portion of the statement, as I can load the table without that statement.
You have syntax error while declaring foreign key constraint
Correct one:
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
CONSTRAINT FK_CUSTOMERS FOREIGN KEY (ST) REFERENCES STATES(ST)
);
or
CREATE TABLE CUSTOMERS(
CUST_ID NUMBER(10) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
STREET_ADDRESS_1 VARCHAR2(30) NOT NULL,
STREET_ADDRESS_2 VARCHAR2(20),
CITY VARCHAR2(20) NOT NULL,
ST VARCHAR2(2),
ZIP_CODE VARCHAR2(10) NOT NULL,
PHONE_NUMBER_1 VARCHAR2(20)NOT NULL,
PHONE_NUMBER_2 VARCHAR2(20),
EMAIL VARCHAR2(40) NOT NULL,
CREDIT_LIMIT NUMBER(7,2) NOT NULL,
FOREIGN KEY (ST) REFERENCES STATES(ST)
);

how to insert using timestamp oracle

I made a 'Kereta' table:
CREATE TABLE Kereta (
ID_Kereta NUMBER(3) CONSTRAINT kr_id_kr_pk PRIMARY KEY,
Nama_Kereta VARCHAR2(30) CONSTRAINT kr_nama_kr_nn NOT NULL,
Jam_Keberangkatan TIMESTAMP CONSTRAINT kr_jk_nn NOT NULL,
Jam_Tiba TIMESTAMP CONSTRAINT kr_jt_nn NOT NULL,
Stasiun_Asal VARCHAR2(20) CONSTRAINT kr_sa_nn NOT NULL,
Stasiun_Tujuan VARCHAR2(20) CONSTRAINT kr_st_nn NOT NULL
)
Then i tried to insert 13.00 to Jam_Keberangkatan field. I can't insert one by one because of NOT NULL constraint, what should i do?

Oracle SQL Foreign Key Issues

I am trying to create tables as well as references but keep getting the following error:
ERROR at line 4:
ORA-00907: missing right parenthesis
...and have searched high and low with no clear way on how to resolve this. I have the following:
drop table financing_plans;
CREATE TABLE financing_plans (plan_id CHAR(10) PRIMARY KEY,
institution VARCHAR2(15) NOT NULL,
Loan_type VARCHAR2(10) NOT NULL,
min_down NUMBER(10,2) NOT NULL,
max_loan_amount NUMBER(10,2) NOT NULL,
Percentage NUMBER(10,2) NOT NULL,
Max_term NUMBER(10,25) NOT NULL);
drop table sale_financings;
CREATE TABLE sale_financings (sale_id CHAR(10) PRIMARY KEY,
Down_pay VARCHAR2(25) NOT NULL,
Loan_term VARCHAR2(18) NOT NULL,
FOREIGN KEY ("plan_uid") REFERENCES financing_plans(plan_id)
FOREIGN KEY ("sale_uid") REFERENCES sales(sale_id));
drop table sales;
CREATE TABLE sales (sale_id CHAR(10) PRIMARY KEY,
Salesperson_ID VARCHAR2(25) NOT NULL,
Cust_ID VARCHAR2(10) NOT NULL,
VIN VARCHAR2(10) NOT NULL,
Gross_sale_price NUMBER(10,2) NOT NULL,
Mileage NUMBER(10,2) NOT NULL,
sale_date DATE,
Vehicle_status VARCHAR2(10) NOT NULL);
Any solution, anyone?
Thanks,
You have to create columns for the foreign key at first, and then create the foreign keys:
CREATE TABLE financing_plans (
plan_id CHAR(10) PRIMARY KEY
, institution VARCHAR2(15) NOT NULL
, Loan_type VARCHAR2(10) NOT NULL
, min_down NUMBER(10,2) NOT NULL
, max_loan_amount NUMBER(10,2) NOT NULL
, Percentage NUMBER(10,2) NOT NULL
, Max_term NUMBER(10,25) NOT NULL
);
CREATE TABLE sales (
sale_id CHAR(10) PRIMARY KEY
, Salesperson_ID VARCHAR2(25) NOT NULL
, Cust_ID VARCHAR2(10) NOT NULL
, VIN VARCHAR2(10) NOT NULL
, Gross_sale_price NUMBER(10,2) NOT NULL
, Mileage NUMBER(10,2) NOT NULL
, sale_date DATE
, Vehicle_status VARCHAR2(10) NOT NULL
);
CREATE TABLE sale_financings (
sale_id CHAR(10) PRIMARY KEY
, Down_pay VARCHAR2(25) NOT NULL
, Loan_term VARCHAR2(18) NOT NULL
, plan_id char(10) not null
, sale_uid char(10) not null
, CONSTRAINT constraint_name_fk FOREIGN KEY (plan_id) REFERENCES financing_plans(plan_id)
, constraint constraint_name_fk2 foreign key (sale_uid) references sales(sale_id)
);
It looks like you're creating the FK reference to sales(sale_id) in sale_financings before you create the sales table. Also, I would check to be sure table exits before dropping it.

Resources