Trigger on same table in oracle - oracle

I have the table employee_leave :
/
I need to create a trigger so that when a manager ( is_manager='Y') is deleted from this table, the manager's subordinate/employee with the managers employee_id as his/her employee_manager_id, should have employee_manager_id set as null on deletion.
How can i write this and what kind of triggers would help the same table would be updated after deletion.

You do not need a trigger, or any code at, all for this. Create a FK from EMPLOYEE_MANAGER_ID to EMPLOYEE_MANAGER_ID with on delete set null option.
CREATE TABLE "EMPLOYEE_LEAVE"
( "EMPLOYEE_ID" NUMBER NOT NULL ENABLE,
"EMPLOYEE_NAME" VARCHAR2(100) NOT NULL ENABLE,
"EMPLOYEE_EMAIL" VARCHAR2(100) NOT NULL ENABLE,
"EMPLOYEE_MANAGER_ID" NUMBER,
"EMPLOYEE_USERNAME" VARCHAR2(400),
"EMPLOYEE_LEAVE_NORMAL" NUMBER NOT NULL ENABLE,
"EMPLOYEE_LEAVE_LOP" NUMBER,
"EMPLOYEE_IS_MANAGER" CHAR(1) NOT NULL ENABLE,
"EMPLOYEE_IS_ADMIN" CHAR(1) NOT NULL ENABLE,
"EMPLOYEE_CONTACT_NO" VARCHAR2(100),
"EMPLOYEE_PASSWORD" VARCHAR2(100),
CONSTRAINT "EMPLOYEE_PK" PRIMARY KEY ("EMPLOYEE_ID")
USING INDEX ENABLE
, constraint empmgr_2_emp foreign key (employee_manager_id)
references employee_leave(employee_id) on delete set null
);

Related

"ORA-00904 invalid identifier" when adding foreign key constraint

I'm trying to create a "diagnosis" table(last table), but something is wrong with the 2 lines of code that creates Foreign Key. I'm sure that's where the problem lies because when I comment them out, I can create the table. The first 4 tables can be created with no errors, so no problem with them. Thank you for reading my question.
I have tried to spot some syntax errors but to no avail.
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
I expected that the table can be created with no error.
Your diagnosis table refers to out_pat_ID and in_pat_ID as columns which should serve as foreign keys, but these columns don't actually exist in the table definition. Try adding them as one possible fix here:
CREATE TABLE diagnosis (
dia_ref number(12) NOT NULL,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8),
out_pat_ID number(5),
in_pat_ID number(5)
CONSTRAINT dia_pk PRIMARY KEY (dia_ref),
CONSTRAINT dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINT dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
diagnosis table need add out_pat_ID and in_pat_ID column
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
✓
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
✓
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
✓
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
✓
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5) not null,
in_pat_ID number(5) not null,
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
✓
db<>fiddle here
If you are allowing only one patient per diagnosis, then you should have a check constraint as well. Your comment and data structure are out-of-sync:
CREATE TABLE diagnosis (
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5),
in_pat_ID number(5),
constraint chk_diagnosis_pat_id
check ( (out_pat_id is not null and in_pat_id is null) or
(out_pat_id is null and in_pat_id is not null)
),
constraint dia_pk primary key (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
constraint dia_out_fk foreign key (out_pat_ID) references out_patient (out_pat_ID),
constraints dia_in_fk foreign key (in_pat_ID) references in_patient (in_pat_ID)
);
That said, separate tables for in-patient and out-patient do not look correct. Or, at the very least, you need a persons table with information about a person over time. Then you can have separate "appointments" and "registrations" to determine if they make an out-patient appointment or register for in-patient care.
Personally I prefer to define single-column constraints inline as part of the column definition. Then you don't have to repeat the column name, and for FK constraints you can let the column inherit its parent's datatype so it is less error-prone.
create table in_patient
( in_pat_id number(5) constraint in_pat_pk primary key
, in_pat_name varchar2(20)
, in_pat_add varchar2(50)
, in_pat_dob date
, in_pat_history varchar2(100)
, in_mode_payment varchar2(20)
, in_start_date date
, in_end_date date
);
create table out_patient
( out_pat_id number(5) constraint out_pat_pk primary key
, out_pat_name varchar2(20)
, out_pat_add varchar2(50)
, out_pat_dob date
, out_pat_history varchar2(100)
, out_mode_payment varchar(20)
, out_date_of_visit date
);
create table diagnosis
( dia_ref number(12) constraint dia_pk primary key
, dia_type varchar2(20)
, dia_date date
, dia_result varchar2(8)
, dia_out_fk references out_patient (out_pat_id)
, dia_in_fk references in_patient (in_pat_id)
);
If dia_result can only be 'POSITIVE' or 'NEGATIVE' then I'd suggest adding a check constraint to enforce this.
Probably some more columns could be defined as not null. It's a good idea to define every possible constraint you can think of.

Oracle - Creating Triggers

I am trying to create a trigger in Oracle whereby we move deleted records to another table. So when deleted column is set to 1, it should move the records from the patient_table to the deleted_patient_table.
Can you please help :)
CREATE TABLE Patient_Table(
PatientID NUMBER(6) Primary Key,
Title char(4) NOT NULL,
Forename varchar2(20) NOT NULL,
Surname varchar2(20) NOT NULL,
Gender char(1) NOT NULL CHECK (Gender in ('M','F')),
DOB date NOT NULL,
TelNo varchar(12) NOT NULL,
Conditions varchar(200) NOT NULL,
Deleted Number(1) NOT NULL CHECK (Deleted in ('0','1'));
-- Table that should contain deleted records --
Create Table Deleted_Patient_table(
PatientID NUMBER(6) Primary Key,
Title char(4) NOT NULL,
Forename varchar2(20) NOT NULL,
Surname varchar2(20) NOT NULL,
Gender char(1) NOT NULL CHECK (Gender in ('M','F')),
DOB date NOT NULL,
TelNo varchar(12) NOT NULL,
Conditions varchar(200) NOT NULL,
Deleted Number(1));
Create or replace trigger trg_del
Before delete on Patient_Table
for each row
Begin
Insert into Deleted_Patient_table value (:old.PatientID,...)
End;
It seems you're soft-deleting your rows. i.e. not deleting but updating related column(Deleted) from 0 to 1. The values of column can be one of two and not null, so you only need to compare only two values of them without nvl operator. During this update operation you can use below trigger to produce log records :
Create or Replace Trigger Trg_Del_Patient
After Update on Patient_Table
For Each Row
Begin
If ( :old.Deleted = 0 and :new.Deleted = 1 ) Then
Insert into Deleted_Patient_table
values(:old.PatientID,:old.Title,:old.Forename,:old.Surname,
:old.Gender,:old.DOB,:old.TelNo,:old.Conditions);
Delete Patient_Table where PatientID = :old.PatientID;
-- Include this Delete statement, if you want to remove after the row has been inserted to the Deleted_Patient_Table
End If;
End;

ORA-00911: invalid character

I am trying to write my first table create script with column definitions. I have tried several things to get this error to go away, but to no avail. Please take a look to see if their is anything obvious in my code below:
CREATE TABLE CD_TYPE (
CD_TYPE VARCHAR2(4) PRIMARY KEY,
CD_FORMAT VARCHAR2(10)
);
CREATE TABLE MANUFACTURER (
MANUFACTURER_NUM NUMBER(3) PRIMARY KEY,
MANUFACTURER_NAME VARCHAR2(30)
);
CREATE TABLE CD_TITLE (
CD_NUM NUMBER(4) PRIMARY KEY,
TITLE VARCHAR2(30),
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
CD_TYPE VARCHAR2(4) FOREIGN KEY,
ACQUIRED_DATE DATE,
ORIGINAL CHAR(1)
);
CREATE TABLE CD_SN (
CD_NUM NUMBER(4) PRIMARY KEY FOREIGN KEY,
SERIAL_NUM VARCHAR2(30) PRIMARY KEY,
NUM_LIC_REMAIN NUMBER(2)
);
The problem is with the columns reading
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
You forgot to specify what table these columns refer to.
You to that with a REFERENCES clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num REFERENCES MANUFACTURER,
cd_type REFERENCES CD_TYPE,
acquired_date date,
original char(1)
);
Alternatively, you can specify the name for the foreign key with a CONSTRAINT ... FOREIGN KEY (...) REFERENCES ... clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num,
cd_type,
acquired_date date,
original char(1),
--
CONSTRAINT cd_title_fk1 FOREIGN KEY (manufacturer_num) REFERENCES manufacturer,
CONSTRAINT cd_title_fk2 FOREIGN KEY (cd_type ) REFERENCES cd_type
)

ORA-00907: missing right parenthesis Error while creating a table?

I am new to oracle,
I have created two tables using following queries,
CREATE TABLE employee
(
emp_name VARCHAR(20) NOT NULL,
street VARCHAR(50) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(emp_name)
)
and
CREATE TABLE company
(
comp_name VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(comp_name)
)
Now I am trying to create another table using some foreign keys,
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
Getting ERROR : ORA-00907: missing right parenthesis
I have also tried with
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
constraint wemployee FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
constraint wcompany FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
But getting same error.
Can any one tell me that where I am doing mistake?
I'm no expert in oracle, but are you allowed to specify the (10) in salary int(10) NOT NULL?
1: you should have a table called "test" with two columns, id and testdata. (This is just a dumb quick example, so I won't bother to specify any constraints on id.)
create table test (id number, testdata varchar2(255));
2: Next we'll create a sequence to use for the id numbers in our test table.
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
You could change "start with 1" to any number you want to begin with (e.g. if you already have 213 entries in a table and you want to begin using this for your 214th entry, replace with "start with 214"). The "increment by 1" clause is the default, so you could omit it. You could also replace it with "increment by n" if you want it to skip n-1 numbers between id numbers. The "nomaxvalue" tells it to keep incrementing forever as opposed to resetting at some point.i (I'm sure Oracle has some limitation on how big it can get, but I don't know what that limit is).
3: Now we're ready to create the trigger that will automatically insert the next number from the sequence into the id column.
create trigger test_trigger
before insert on test
for each row beginselect test_seq.nextval into :new.id from dual;
end;
/
There are two different ways to create a table with constraints:
1)
create table department(
deptno number(5) primary key,
deptname varchar2(30),
empno number(5) references emp(empno));
2)
create table department(
deptno number(5),
deptname varchar2(30),
empno number(5),
constraint pkey_deptno primary key(deptno),
constraint fkey_empno foreign key(empno) references Emp(empno));
When creating the index inline with the rest of the table creation statement try dropping the FOREIGN KEY part:
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
emp_name REFERENCES employee(emp_name),
comp_name REFERENCES company(comp_name)
)
See this question for more details:
ORA-00907: missing right parenthesis

Insert query showing an error in oracle10g

I created a table in oracle10g using following query......
CREATE TABLE "EMPLOYEESTASKS"
( "EMPLOYEEID" NUMBER,
"TASKDATE" VARCHAR2(40),
"STATUS" NUMBER,
"CUSTOMERID" NUMBER,
"ADDRESS" VARCHAR2(400) NOT NULL ENABLE,
"TASKTIME" VARCHAR2(40) NOT NULL ENABLE,
"VISITDATE" VARCHAR2(40),
"VISITTIME" VARCHAR2(40),
CONSTRAINT "EMPLOYEESTASKS_PK" PRIMARY KEY ("EMPLOYEEID", "TASKDATE", "TASKTIME") ENABLE,
CONSTRAINT "EMPLOYEESTASKS_FK" FOREIGN KEY ("EMPLOYEEID")
REFERENCES "EMPLOYEES" ("ID") ON DELETE CASCADE ENABLE
)
Table was created successfully... but the problem is while iam trying to insert a row into the table it is showing the error
ORA-01722: invalid number
The query i used is ,
insert into employeestasks values(12305,'30-11-2011','09:00',0,45602,'Sarpavaram Junction ,kakinada',null,null)
What is that invalid number..??
It look like your columns in the table are ordered employeeid, taskdate, status, and you're trying to insert '09:00' into status, which is a number. This is no good. You need to use the same order of columns or specify which value is for which column.
Also, you really like capslock, huh?

Resources