Akshay Rane
Akshay Rane

Reputation: 3

SQL - foreign key error

I get this error when I try to add the foreign key :

"ORA-00904: "BR_ID": invalid identifier"

create table Branch9
(br_id number NOT NULL,br_name varchar2(25) NOT NULL ,br_address varchar2(30),PRIMARY KEY(br_id))

create table Employee9
(emp_id number NOT NULL,emp_name varchar2(25) NOT NULL UNIQUE,emp_address varchar2(30),emp_age number,emp_dob date,emp_salary number,PRIMARY KEY(emp_id))

ALTER TABLE Employee9
ADD FOREIGN KEY (br_id) REFERENCES Branch9 (br_id);

Is it anything related to NOT NULL constraint added to br_id?

Upvotes: 0

Views: 97

Answers (3)

Filipe Silva
Filipe Silva

Reputation: 21657

br_id has to be in Employee9 table:

This is how you add it:

ALTER TABLE Employee9 ADD br_id number NOT NULL;

Then you can do:

ALTER TABLE Employee9 
    ADD CONSTRAINT fk_br_id FOREIGN KEY (br_id) REFERENCES Branch9 (br_id);

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146450

There's no br_id column in Employee9, is there?:

create table Employee9 (
    emp_id number NOT NULL,
    emp_name varchar2(25) NOT NULL UNIQUE,
    emp_address varchar2(30),
    emp_age number,
    emp_dob date,
    emp_salary number,
    PRIMARY KEY(emp_id)
)

Upvotes: 0

wvdz
wvdz

Reputation: 16641

br_id needs to be a column in Employee9.

Upvotes: 1

Related Questions