I am trying to make 3 tables 2 with foreign keys to the rest. I keep getting error codes when I try to insert data into the table so I figured I would isolate the create tables and attempt to fix there. The code is as follows:
-- Spool the execution output to a text file. This spooled file serves as logs.
spool on
spool hw2.lst
set echo on
-- All the DROP TABLE in the beginning.
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE companies CASCADE CONSTRAINTS;
DROP TABLE producer CASCADE CONSTRAINTS;
-- Creates Table CUSTOMER.
CREATE TABLE CUSTOMER (
cust_ID NUMBER(10),
last_name VARCHAR2(20) NOT NULL,
first_name VARCHAR2(20) NOT NULL,
MI VARCHAR2(1) NOT NULL,
street VARCHAR2(30) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(30) NOT NULL,
zip_code VARCHAR2(20) NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(cust_ID));
-- Creates Table SALE_VEHICLE.
CREATE TABLE SALE_VEHICLE (
VIN NUMBER(20),
trade_ID NUMBER(10),
make VARCHAR2(30) NOT NULL,
model VARCHAR2(30) NOT NULL,
type VARCHAR2(30) NOT NULL,
where_frome VARCHAR2(30) NOT NULL,
wholesale_cost NUMBER(8,2)NOT NULL,
CONSTRAINT producer_pk PRIMARY KEY(VIN));
-- Creates Table SALE.
CREATE TABLE SALE (
sale_ID NUMBER(6),
cust_ID NUMBER(10),
salesperson_ID NUMBER(10),
VIN NUMBER(20),
date DATE NOT NULL,
vehicle_status VARCHAR2(20) NOT NULL,
milage VARCHAR2(20) NOT NULL,
gross_sale_price NUMBER(8,2)NOT NULL,
CONSTRAINT sale_pk PRIMARY KEY(sale_ID))
CONSTRAINT customers_fk2 FOREIGN KEY(cust_ID) REFERENCES CUSTOMER(cust_ID))
CONSTRAINT VIN_fk FOREIGN KEY(VIN) REFERENCES SALE_VEHICLE(VIN));
-- spool off command to turn off the spool with it you may not get the all the content spooled.
set echo off
spool off
This is what is echoed after I attempt to run the sql file:
SQL>
SQL>
SQL> -- All the DROP TABLE in the beginning.
SQL>
SQL> DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> DROP TABLE companies CASCADE CONSTRAINTS;
DROP TABLE companies CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> DROP TABLE producer CASCADE CONSTRAINTS;
DROP TABLE producer CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL>
SQL> -- Creates Table CUSTOMER.
SQL>
SQL> CREATE TABLE CUSTOMER (
2 cust_ID NUMBER(10),
3 last_name VARCHAR2(20) NOT NULL,
4 first_name VARCHAR2(20) NOT NULL,
5 MI VARCHAR2(1) NOT NULL,
6 street VARCHAR2(30) NOT NULL,
7 city VARCHAR2(30) NOT NULL,
8 state VARCHAR2(30) NOT NULL,
9 zip_code VARCHAR2(20) NOT NULL,
10 CONSTRAINT customer_pk PRIMARY KEY(cust_ID));
CREATE TABLE CUSTOMER (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL> -- Creates Table SALE_VEHICLE.
SQL>
SQL> CREATE TABLE SALE_VEHICLE (
2 VIN NUMBER(20),
3 trade_ID NUMBER(10),
4 make VARCHAR2(30) NOT NULL,
5 model VARCHAR2(30) NOT NULL,
6 type VARCHAR2(30) NOT NULL,
7 where_frome VARCHAR2(30) NOT NULL,
8 wholesale_cost NUMBER(8,2)NOT NULL,
9 CONSTRAINT producer_pk PRIMARY KEY(VIN));
CREATE TABLE SALE_VEHICLE (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL>
SQL> -- Creates Table SALE.
SQL>
SQL> CREATE TABLE SALE (
2 sale_ID NUMBER(6),
3 cust_ID NUMBER(10),
4 salesperson_ID NUMBER(10),
5 VIN NUMBER(20),
6 date DATE NOT NULL,
7 vehicle_status VARCHAR2(20) NOT NULL,
8 milage VARCHAR2(20) NOT NULL,
9 gross_sale_price NUMBER(8,2)NOT NULL,
10 CONSTRAINT sale_pk PRIMARY KEY(sale_ID))
11 CONSTRAINT customers_fk2 FOREIGN KEY(cust_ID) REFERENCES CUSTOMER(cust_ID))
12 CONSTRAINT VIN_fk FOREIGN KEY(VIN) REFERENCES SALE_VEHICLE(VIN));
date DATE NOT NULL,
*
ERROR at line 6:
ORA-00904: : invalid identifier
SQL>
SQL> -- spool off command to turn off the spool with it you may not get the all the content spooled.
SQL>
SQL> set echo off
Any help would be great thanks.
I figured it out here is the answer:
-- Spool the execution output to a text file. This spooled file serves as logs.
spool on
spool hw2.lst
set echo on
-- All the DROP TABLE in the beginning.
DROP TABLE CUSTOMER CASCADE CONSTRAINTS;
DROP TABLE SALE_VEHICLE CASCADE CONSTRAINTS;
DROP TABLE SALE CASCADE CONSTRAINTS;
-- Creates Table CUSTOMER.
CREATE TABLE CUSTOMER (
cust_ID NUMBER(10),
last_name VARCHAR2(20) NOT NULL,
first_name VARCHAR2(20) NOT NULL,
MI VARCHAR2(1) NOT NULL,
street VARCHAR2(30) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(30) NOT NULL,
zip_code VARCHAR2(20) NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY(cust_ID));
-- Creates Table SALE_VEHICLE.
CREATE TABLE SALE_VEHICLE (
VIN NUMBER(20),
trade_ID NUMBER(10),
make VARCHAR2(30) NOT NULL,
model VARCHAR2(30) NOT NULL,
type VARCHAR2(30) NOT NULL,
where_frome VARCHAR2(30) NOT NULL,
wholesale_cost NUMBER(8,2)NOT NULL,
CONSTRAINT producer_pk PRIMARY KEY(VIN));
-- Creates Table SALE.
CREATE TABLE SALE (
sale_ID NUMBER(6),
cust_ID NUMBER(10),
salesperson_ID NUMBER(10),
VIN NUMBER(20),
date_received DATE NOT NULL,
vehicle_status VARCHAR2(20) NOT NULL,
milage VARCHAR2(20) NOT NULL,
gross_sale_price NUMBER(8,2)NOT NULL,
CONSTRAINT sale_pk PRIMARY KEY(sale_ID),
CONSTRAINT customers_fk2 FOREIGN KEY(cust_ID) REFERENCES CUSTOMER(cust_ID),
CONSTRAINT VIN_fk FOREIGN KEY(VIN) REFERENCES SALE_VEHICLE(VIN));
-- spool off command to turn off the spool with it you may not get the all the content spooled.
set echo off
spool off
Related
CREATE TABLE visitors
(
V_ID NUMBER(6)
CONSTRAINT pk_visitors_V_ID PRIMARY KEY,
V_FN VARCHAR2(15) NOT NULL,
V_LN VARCHAR2(15) NOT NULL,
V_Contact NUMBER(10) NOT NULL,
V_Address CHAR(10) NOT NULL,
DoB DATE NOT NULL,
Covid_Vaccine NUMBER(1)
CONSTRAINT ck_visitors_Covid_Vaccine CHECK (Covid_Vaccine IN ('1','2','3'))
R_ID NUMBER(4)
CONSTRAINT fk_residents_R_IDREFERENCES residents(R_ID),
Date DATE NOT NULL
);
I'm trying to create this table on Apex Oracle for SQL but I keep getting this error:
ORA-00907: missing right parenthesis.
That error is for missing comma after Covid_Vaccine column declaration. But after correcting this you will get invalid identifier error since date is reserve word . So I have changed the column name to ColDate.
You will face another error ORA-02253: constraint specification not allowed here for not putting reference key word in R_ID column declaration. Please try below create table statement (it's working now).
create table residents(R_ID NUMBER(4) PRIMARY KEY);
CREATE TABLE visitors (
V_ID NUMBER(6) CONSTRAINT pk_visitors_V_ID PRIMARY KEY,
V_FN VARCHAR2(15) NOT NULL,
V_LN VARCHAR2(15) NOT NULL,
V_Contact NUMBER(10) NOT NULL,
V_Address CHAR(10) NOT NULL,
DoB DATE NOT NULL,
Covid_Vaccine NUMBER(1) CONSTRAINT ck_visitors_Covid_Vaccine CHECK (Covid_Vaccine IN ('1','2','3')),
R_ID NUMBER(4) CONSTRAINT fk_residents_R_IDREFERENCES references residents(R_ID),
ColDate DATE NOT NULL
);
db<>fiddle here
Various errors (missing comma, invalid column name (date is reserved for datatype), missing references keyword). When fixed:
SQL> CREATE TABLE visitors (
2 v_id NUMBER(6) CONSTRAINT pk_visitors_v_id PRIMARY KEY,
3 v_fn VARCHAR2(15) NOT NULL,
4 v_ln VARCHAR2(15) NOT NULL,
5 v_contact NUMBER(10) NOT NULL,
6 v_address CHAR(10) NOT NULL,
7 dob DATE NOT NULL,
8 covid_vaccine NUMBER(1) CONSTRAINT ck_visitors_covid_vaccine CHECK
9 ( covid_vaccine IN ( '1', '2', '3' ) ),
10 r_id NUMBER(4) CONSTRAINT fk_residents_r_idreferences
11 REFERENCES residents(r_id),
12 c_date DATE NOT NULL );
Table created.
SQL>
Here is a logical model that I have.
Below are table structures and their constraints
Doctor
CREATE TABLE doctor (
doctor_id NUMBER(4) NOT NULL,
doctor_title VARCHAR2(2) NOT NULL,
doctor_fname VARCHAR2(50),
doctor_lname VARCHAR2(50),
doctor_phone CHAR(10) NOT NULL
);
ALTER TABLE doctor ADD CONSTRAINT doctor_pk PRIMARY KEY ( doctor_id );
Procedure
CREATE TABLE procedure (
proc_code NUMBER(5) NOT NULL,
proc_name VARCHAR2(100) NOT NULL,
proc_description VARCHAR2(300) NOT NULL,
proc_time NUMBER(3) NOT NULL,
proc_std_cost NUMBER(7, 2) NOT NULL
);
ALTER TABLE procedure ADD CONSTRAINT procedure_pk PRIMARY KEY ( proc_code );
ALTER TABLE procedure ADD CONSTRAINT proc_name_unq UNIQUE ( proc_name );
Admission
CREATE TABLE admission (
adm_no NUMBER(6) NOT NULL,
adm_date_time DATE NOT NULL,
adm_discharge DATE,
patient_id NUMBER(6) NOT NULL,
doctor_id NUMBER(4) NOT NULL
);
ALTER TABLE admission ADD CONSTRAINT admission_pk PRIMARY KEY ( adm_no ); --surrogate key
ALTER TABLE admission ADD CONSTRAINT admission_nk UNIQUE ( patient_id,
adm_date_time );
Admission Procedure
CREATE TABLE adm_prc (
adprc_no NUMBER(7) NOT NULL,
adprc_date_time DATE NOT NULL,
adprc_pat_cost NUMBER(7, 2) NOT NULL,
adprc_items_cost NUMBER(6, 2) NOT NULL,
adm_no NUMBER(6) NOT NULL,
proc_code NUMBER(5) NOT NULL,
request_dr_id NUMBER(4) NOT NULL,
perform_dr_id NUMBER(4)
);
ALTER TABLE adm_prc ADD CONSTRAINT adm_prc_pk PRIMARY KEY ( adprc_no ); --surrogate key
ALTER TABLE adm_prc ADD CONSTRAINT adm_prc_nk UNIQUE ( adprc_date_time,
adm_no );
ALTER TABLE adm_prc
ADD CONSTRAINT admission_admprc FOREIGN KEY ( adm_no )
REFERENCES admission ( adm_no );
ALTER TABLE adm_prc
ADD CONSTRAINT doctor_performadmprc FOREIGN KEY ( perform_dr_id )
REFERENCES doctor ( doctor_id );
ALTER TABLE adm_prc
ADD CONSTRAINT doctor_requestadmprc FOREIGN KEY ( request_dr_id )
REFERENCES doctor ( doctor_id );
Item Treatment
CREATE TABLE item_treatment (
adprc_no NUMBER(7) NOT NULL,
item_code CHAR(5) NOT NULL,
it_qty_used NUMBER(2) NOT NULL,
it_item_total_cost NUMBER(8, 2) NOT NULL
);
ALTER TABLE item_treatment
ADD CONSTRAINT item_treatment_pk PRIMARY KEY ( adprc_no,item_code);
ALTER TABLE item_treatment
ADD CONSTRAINT admprc_itemtreatment FOREIGN KEY ( adprc_no )
REFERENCES adm_prc ( adprc_no );
ALTER TABLE item_treatment
ADD CONSTRAINT admprc_itemtreatment FOREIGN KEY ( adprc_no )
REFERENCES adm_prc ( adprc_no );
ALTER TABLE item_treatment
ADD CONSTRAINT item_itemtreatment FOREIGN KEY ( item_code )
REFERENCES item ( item_code );
In a fictitious hospital, every time an admission procedure is completed for a patient admission, the lead doctor who performed the procedure is recorded under the perform_dr_id in adm_prc table. Even if a team of doctors perform the procedure, only the lead doctor is recorded.
The hospital now wishes to record all the doctors who performed in the admission procedure including an Ancillary doctor (assisting doctor).
I have altered the adm_prc table to include ancillary doctors
ALTER TABLE ADM_PRC ADD ANCILLARY_DR_ID NUMBER(4);
ALTER TABLE ADM_PRC
ADD CONSTRAINT DOCTOR_PERFORM_ANCILLARY FOREIGN KEY ( ANCILLARY_DR_ID )
REFERENCES DOCTOR ( DOCTOR_ID );
I need to change the structure of the database is such a way that, there should be multiple records for the same admission procedure as there can be many doctors assisting in a single admission procedure. For example, I should be able to insert the following records in adm_prc table
adprc_no adprc_date_time adprc_pat_cost adprc_items_cost adm_no proc_code
request_dr_id perform_dr_id ancillary_dr_id
-----------------------------------------------------------------------------------------------------------------------------
1 14/03/2019 100 100 1234 1234
10 10 12
1 14/03/2019 100 100 1234 1234
10 10 13
However, this violates primary key constraint adm_prc_pk
I'm stuck at this point and unable to proceed further. Would appreciate if someone could point me in the right direction.
You should create a new satellite table (say ADM_PRC_DR_DTLS) to hold adprc_no and corresponding dr_id details.
adprc_no of ADM_PRC_DR_DTLS would refer to the ADM_PRC's adprc_no as a Foreign Key.
I am getting ORA-00955 error which is name is already used by an existing object. I know it is in the foreign key constraint im trying to use. It happens in both foreign key constraints. I cant figure out why its happening. I tried renaming them to anything possible and I cant understand why it wont work.
DROP TABLE STUDENT;
CREATE TABLE STUDENT
(stuID VARCHAR (7) PRIMARY KEY NOT NULL,
major VARCHAR (15) NOT NULL,
firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
yr VARCHAR(8) NOT NULL
);
DROP TABLE ENROLL;
CREATE TABLE ENROLL
(stuID VARCHAR (7) NOT NULL,
grade VARCHAR (1) NOT NULL,
courseID VARCHAR(7) NOT NULL,
CONSTRAINT fk_STUDENT
FOREIGN KEY (stuID)
REFERENCES STUDENT(stuid)
);
DROP TABLE CLASS;
CREATE TABLE CLASS
(instructorID VARCHAR(7) NOT NULL,
classRoomNumber VARCHAR(7) NOT NULL,
dateStarts DATE NOT NULL,
courseID VARCHAR(7) PRIMARY KEY NOT NULL,
description CHAR(100) NOT NULL,
CONSTRAINT fk_INSTRUCTOR
FOREIGN KEY (instructorID)
REFERENCES INSTRUCTOR(instructorid)
);
DROP TABLE INSTRUCTOR;
CREATE TABLE INSTRUCTOR
(firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
departmentName VARCHAR (15) NOT NULL,
instructorID VARCHAR(7) PRIMARY KEY NOT NULL
);
In a comment, you said that there's still an error.
If you create (and/or drop) tables in a correct order, everything is OK. I prefer dropping them separately.
At first, tables don't exist so I'll just create them:
SQL> CREATE TABLE student (
2 stuid VARCHAR(7) PRIMARY KEY NOT NULL,
3 major VARCHAR(15) NOT NULL,
4 firstname VARCHAR(15) NOT NULL,
5 lastname VARCHAR(15) NOT NULL,
6 yr VARCHAR(8) NOT NULL
7 );
Table created.
SQL> CREATE TABLE enroll (
2 stuid VARCHAR(7) NOT NULL,
3 grade VARCHAR(1) NOT NULL,
4 courseid VARCHAR(7) NOT NULL,
5 CONSTRAINT fk_student FOREIGN KEY ( stuid )
6 REFERENCES student ( stuid )
7 );
Table created.
SQL> CREATE TABLE instructor (
2 firstname VARCHAR(15) NOT NULL,
3 lastname VARCHAR(15) NOT NULL,
4 departmentname VARCHAR(15) NOT NULL,
5 instructorid VARCHAR(7) PRIMARY KEY NOT NULL
6 );
Table created.
SQL> CREATE TABLE class (
2 instructorid VARCHAR(7) NOT NULL,
3 classroomnumber VARCHAR(7) NOT NULL,
4 datestarts DATE NOT NULL,
5 courseid VARCHAR(7) PRIMARY KEY NOT NULL,
6 description CHAR(100) NOT NULL,
7 CONSTRAINT fk_instructor FOREIGN KEY ( instructorid )
8 REFERENCES instructor ( instructorid )
9 );
Table created.
Drop tables in reverse order, so that detail is dropped before its master
SQL> DROP TABLE enroll;
Table dropped.
SQL> DROP TABLE student;
Table dropped.
SQL> DROP TABLE class;
Table dropped.
SQL> DROP TABLE instructor;
Table dropped.
SQL>
Table INSTRUCTOR is referenced by table CLASS, hence you heed to create table INSTRUCTOR before you create table CLASS.
Also, you should use DROP TABLE ... CASCADE CONSTRAINTS instead of just DROP TABLE .... This allows foreign and primary keys to be properly dropped at the same time as the table, and might avoid error name already used by existing object that you are currently getting.
I have Oracle database. I want to create INDEX:
CREATE INDEX indexID ON Employee(id_employee);
But it writes -> SQL Error: ORA-01408: such column list already indexed
So before create index I put:
DROP INDEX indexID;
But it writes -> SQL Error: ORA-01418: specified index does not exist
my Employee table:
CREATE TABLE Employee (
id_employee NUMBER(5) NOT NULL,
name VARCHAR(25) NOT NULL,
surname VARCHAR(25) NOT NULL,
day_of_birth DATE NOT NULL,
salary NUMBER(6) NOT NULL,
PRIMARY KEY(id_employee)
);
Have you some idea? it looks like index does not create.
You have a different index on that column.
Say you create the table like this:
SQL> CREATE TABLE Employee (
2 id_employee NUMBER(5) NOT NULL ,
3 name VARCHAR(25) NOT NULL,
4 surname VARCHAR(25) NOT NULL,
5 day_of_birth DATE NOT NULL,
6 salary NUMBER(6) NOT NULL
7 );
Table created.
Then you add the PK constraint:
SQL> alter table employee add primary key(id_employee);
Table altered.
Now Oracle already created a unique index on the PK field, so you already have it, with no need for manual creation.
SQL> select index_name, column_name
2 from user_ind_columns c
3 inner join user_indexes i
4 using (index_name)
5 where i.table_name = 'EMPLOYEE';
INDEX_NAME COLUMN_NAME
-------------------- --------------------
SYS_C007892 ID_EMPLOYEE
In your example:
SQL> CREATE TABLE Zamestnanec (
2 id_zamestnance NUMBER(5) PRIMARY KEY ,
3 jmeno VARCHAR(25) NOT NULL,
4 prijmeni VARCHAR(25) NOT NULL,
5 datum_narozeni DATE NOT NULL,
6 prava CHAR(3) CHECK(prava IN ('ano', 'ne')) NOT NULL,
7 plat NUMBER(6) NOT NULL
8 );
Table created.
SQL> select index_name, column_name
2 from user_ind_columns c
3 inner join user_indexes i
4 using (index_name)
5 where i.table_name = 'ZAMESTNANEC';
INDEX_NAME COLUMN_NAME
-------------------- --------------------
SYS_C007899 ID_ZAMESTNANCE
I slightly modified your syntax; besides, you can avoid the NOT NULL constraint on a PK field: the PK will force the field to be NOT NULL.
See here for a similar problem.
The CREATE TABLE for ZAMESTNANEC is
CREATE TABLE Zamestnanec (
id_zamestnance NUMBER(5) NOT NULL,
jmeno VARCHAR(25) NOT NULL,
prijmeni VARCHAR(25) NOT NULL,
datum_narozeni DATE NOT NULL,
prava CHAR(3) CHECK(prava IN ('ano', 'ne')) NOT NULL,
plat NUMBER(6) NOT NULL,
PRIMARY KEY(id_zamestnance)
);
You've created a primary key constraint for ID_ZAMESTNANCE, which by default creates an index on ID_ZAMESTNANCE; thus you don't need to create another index on ID_ZAMESTNANCE.
I would suggest a freakish solution, but I guarantee you that it will work.
Drop the primary key constraint from the table.
ALTER TABLE Employee
DROP CONSTRAINT pk_id_employee
Drop the index, then…
Create the primary key constraint
I'm fairly new to SQL and I am trying to create a trigger to calculate overtime worked by subtracting hours contracted (GRADE_HOURS) in the COMPANY_GRADE table from hours worked (TIMESHEET_HOURS) in the COMPANY_TIMESHEET table. This will then go into the TIMESHEET_OT column of the COMPANY_TIMESHEET table whenever a row is inserted into the COMPANY_TIMESHEET table.
The tables concerned are as follows:
CREATE TABLE COMPANY_TIMESHEET
(
timesheet_ID number(3) constraint timesheet_pk primary key,
Timesheet_emp number(3) constraint timesheet_empnotnull not null references company_employee,
Timesheet_wc date constraint timesheet_wcnotnull not null,
Timesheet_hours number(2),
Timesheet_OT number(2) default 0,
Timesheet_approved number(3) references company_employee
);
CREATE TABLE COMPANY_GRADE
(
grade_ID number(3) constraint grade_pk primary key,
Grade_rate number(5,2) constraint grade_ratenotnull not null,
Grade_hours number(2)
) ;
CREATE TABLE COMPANY_EMPLOYEE
(
emp_ID number(3) constraint emp_pk primary key,
Emp_firstname varchar2(50) constraint emp_firstnamenotnull not null,
Emp_surname varchar2(50),
Emp_department number(2) constraint employeeFKdepartment references company_department,
emp_street varchar2(50),
emp_town varchar2(50),
emp_district varchar2(50),
Emp_grade number(3) default 4 constraint checkempgrade check (Emp_grade between 1 and 9) references company_grade,
Emp_site varchar2(30) default 'LONDON'
);
I would appreciate any help as I have been trying for many hours now only to be met by error after error.
I have tried numerous variations of this as a starting block to try and pass the hours contracted into a variable to then subtract from another variable:
CREATE OR REPLACE TRIGGER trg_ot
BEFORE INSERT ON company_timesheet
FOR EACH ROW
DECLARE t_contracted NUMBER;
BEGIN
SELECT grade_hours INTO t_contracted
FROM company_grade
WHERE company_employee.emp_id = :new.timesheet_emp;
END;
/
CREATE OR REPLACE TRIGGER trg_ot
BEFORE INSERT ON company_timesheet
FOR EACH ROW
DECLARE t_contracted NUMBER;
BEGIN
SELECT g.grade_hours
INTO t_contracted
FROM company_grade g
INNER JOIN
company_employee e
ON ( e.emp_grade = g.grade_id )
WHERE e.emp_id = :new.timesheet_emp;
:new.timesheet_ot := :new.timesheet_hours - t_contracted;
END;
/