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

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.

Related

No matching unique or primary key for column-list in oracle

CREATE TABLE student_detailS01
(USN VARCHAR(20) NOT NULL,
STUDENT_NAME VARCHAR2(20) NOT NULL,
BRANCH VARCHAR2(20) NOT NULL,
CONTACT_NO NUMBER(10) NOT NULL,
FATHERS_NAME VARCHAR2(20) NOT NULL,
HOME_ADDRESS VARCHAR(50) NOT NULL,
DOB DATE NOT NULL,
LOCAL_ADDRESS VARCHAR2(50),
EMAIL_ID VARCHAR2(20),
DOJ DATE,
HID NUMBER NOT NULL,
PRIMARY KEY (USN,HID)
);
CREATE TABLE MESS(
S_NO NUMBER NOT NULL,
HID NUMBER REFERENCES STUDENT_DETAILS01(HID),
NO_OF_BREAKFAST INT,
NO_OF_MEALS INT,
AMT_OF_BREAKFAST INT,
AMT_OF_MEALS INT,
TOTAL INT NOT NULL,
PRIMARY KEY(S_NO)
);
student_details01 table is executed, but mess table gives the following error:
no matching unique or primary key for this column-list.
The ORA-2270 : no matching unique or primary key for this column-list error is quite simple: it happens when the columns we reference in the foreign key do not match a primary key or unique constraint on the parent table. Common reasons for this are
the parent lacks a constraint altogether
the parent table's constraint is a compound key and we haven't
referenced all the columns in the foreign key statement.
To fix your issue try as below:
CREATE TABLE MESS
(
S_NO NUMBER,
S_HID NUMBER,
S_UNO VARCHAR (20), --Added Column
NO_OF_BREAKFAST INT,
NO_OF_MEALS INT,
AMT_OF_BREAKFAST INT,
AMT_OF_MEALS INT,
TOTAL INT NOT NULL,
CONSTRAINT MESS_pk PRIMARY KEY (S_NO),
CONSTRAINT fk_id_dtls FOREIGN KEY
(S_HID, S_UNO)
REFERENCES student_detailS01 (HID, USN)
);

data base oracle foreign key error ORA-02270: no matching unique or primary key for this column-list

while making foreign key in player table it shows following error
ORA-02270: no matching unique or primary key for this column-list
create table person
(
per_ssn number(10) not null,
per_name varchar2(30) not null,
CONSTRAINT pk_PersonID PRIMARY KEY (per_ssn,per_name)
);
create table Player
(
player_ssn number(10) not null,
player_name varchar2(30) not null,
football_club_name varchar2(30) not null,
p_age number(2) not null,
p_weight number(3) not null,
p_height number(10) not null,
country varchar2(20) not null,
p_starting_date date not null,
p_ending_date date not null
);
alter table Player
add constraint player_ssn
FOREIGN KEY (player_ssn)
REFERENCING person (per_ssn)on delete cascade
I want to make two primary keys in person table and then want to refer these
primary keys in player table.
If I make one primary key and then refer it in player table, then it does not show error but I want to make two primary keys.
You should be referencing per_ssn,per_name because that is your PK on person.
Anyway, think about making per_ssn your PK in person table
alter table Player
add constraint player_ssn
FOREIGN KEY (player_ssn,player_name)
REFERENCING person (per_ssn,per_name)on delete cascade

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
)

Missing left parenthesis

Missing left parenthesis on line 9..can't see the error!
create table EDITIONS(
ID number NOT NULL,
ID_VOLUME number,
Publishing varchar2(20),
Year number(4),
Language varchar2(10),
Pages number(4),
CONSTRAINT pk_editions PRIMARY KEY,
constraint fk_editions_volume foreign key (id_volume)
references volume(id),
constraint editions_publishing,
constraint editions_year,
constraint editions_language,
constraint editions_pages,
);
Try something like this:
create table EDITIONS (
ID number NOT NULL primary key
ID_VOLUME number,
Publishing varchar2(20),
Year number(4),
Language varchar2(10),
Pages number(4),
constraint fk_editions_volume foreign key (id_volume) references volume(id)
);
I assume you are using Oracle because of the varchar2(). You should tag questions with the correct database.

Oracle foreign key relation

I have a composite primary key in my Candidate table
CREATE TABLE CANDIDATE(
CANDIDATE_ID VARCHAR(5),
NAME VARCHAR(30),
TELEPHONE NUMBER,
PRIMARY KEY(CANDIDATE_ID, NAME));
When I create a child table, I get an error saying the number of referencing columns must match referenced columns when I create a foreign key for the CANDIDATE_ID
CREATE TABLE JOB(
POSITION_ID VARCHAR(5) PRIMARY KEY,
CANDIDATE_ID VARCHAR(5),
DATE2 DATE,
FOREIGN KEY(CANDIDATE_ID) REFERENCES CANDIDATE);
A table can only have one primary key-- you have a composite primary key. If you have a composite primary key, you have to reference the entire key in your child table. That would mean that the child table would need to have a CANDIDATE_ID column and a NAME column.
CREATE TABLE job (
position_id VARCHAR2(5) PRIMARY KEY,
candidate_id VARCHAR2(5),
name VARCHAR2(30),
date2 DATE,
FOREIGN KEY( candidate_id, name ) REFERENCES candidate( candidate_id, name )
);
Of course, you probably don't want to store the name in both tables. You probably want the candidate_id to be the prmiary key of candidate and you may want to create a separate unique constraint on name.
CREATE TABLE CANDIDATE(
CANDIDATE_ID VARCHAR(5) primary key,
NAME VARCHAR(30) unique,
TELEPHONE NUMBER);
CREATE TABLE JOB(
POSITION_ID VARCHAR(5) PRIMARY KEY,
CANDIDATE_ID VARCHAR(5),
DATE2 DATE,
FOREIGN KEY(CANDIDATE_ID) REFERENCES CANDIDATE(candidate_id));
Assuming that the combination of CANDIDATE_ID and NAME is required for the key to be unique, then you will need to add a reference to the NAME column in your referencing table.
I suspect that CANDIDATE_ID is enough to uniquely identify the candidates in your primary table. If that is the case then it should be your primary key and your relationship will work. If you want to index the NAME separately then do so, but leave it out of the primary key.
Last line should be like this;
CONSTRAINT FK_CANDIDATE_ID FOREIGN KEY (CANDIDATE_ID)REFERENCES CANDIDATE(CANDIDATE_ID);
CREATE TABLE dept
( did char(3) not null,
dname varchar2(20) not null,
CONSTRAINT dept_pk PRIMARY KEY (did)
);
strong text
create table emp
(
eid char(3) unique,
ename varchar2(10) not null,
sal number check (sal between 20000 AND 50000),
city varchar2(10) default 'texus',
did char(3) not null,
constraint fk_did_dept
FOREIGN KEY (did) references
dept(did)
);

Resources