ORA-02270 Referencing a foreign key - sql

I have 1st table:
CREATE TABLE HEAD (
Code int,
Name varchar(99),
HType char(1),
HDate date,
OpBal number(10),
CONSTRAINT pk_head PRIMARY KEY (Code, Name));
and 2nd table:
CREATE TABLE SUBHEAD (
HCode int,
SubCode int,
Name varchar(99),
SDate date,
OpBal number(10),
CONSTRAINT fk_subhead FOREIGN KEY (HCode) REFERENCES HEAD(Code),
CONSTRAINT pk_subhead PRIMARY KEY (Hcode, SubCode));
Now when I run the script, I get:
CONSTRAINT fk_subhead FOREIGN KEY (HCode) REFERENCES HEAD(Code),
ERROR at line 7:
ORA-02256: number of referencing columns must match referenced columns
I have been looking around trying to figure out what's wrong and I think it has something to do with parent table constraint but I am totally stumped on how to fix this.

Your problem is that your foreign key is not referencing the entire primary key from HEAD.
So you either need to change the foreign key (to include name in there) or change the primary key of HEAD to exclude name there.
So either change
CONSTRAINT pk_head PRIMARY KEY (Code, Name));
to
CONSTRAINT pk_head PRIMARY KEY (Code));
or change
CONSTRAINT fk_subhead FOREIGN KEY (HCode) REFERENCES HEAD(Code)
to
CONSTRAINT fk_subhead FOREIGN KEY (HCode, Name) REFERENCES HEAD(Code, Name)

Related

Recursive relationship SQL error

I am pretty new to SQL and I have a problem. I want to make a recursive relationship (a table that relates to itself), but I get an error when I try to execute my code. It's working fine without the Coordinator_Office_ID foreign key.
The error is:
The number of columns in the foreign-key referencing list is not equal
to the number of columns in the referenced list.
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade,
Constraint lg_cord_id Foreign key (Coordinator_Office_ID) References Logistican(Office_ID)
);
Yes, that's cause you have defined composite primary key like Primary key (Office_ID, Worker_ID) and thus your FK should include both of them else it will result in PFD (partial functional dependency)
Add the constraint with alter table:
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade
);
alter table Logistican
add Constraint lg_cord_id
Foreign key (Coordinator_Office_ID, Worker_Id) References Logistican(Office_ID, Worker_Id);
The relationship needs all elements of the primary key to be valid. I'm not sure if it needs to be a separate statement in Oracle.

Cannot create table. SQL Error 02270

This is the table I am trying to create. However, I get the error
SQL Error: ORA-02270: no matching unique or primary key for this column-list
SQL:
create table Meets_In
(
cid char(20),
rno integer,
time char(20),
CONSTRAINT PRIM_KEY PRIMARY KEY(time),
constraint meets_fk1 foreign key(cid) references COURSES(CID),
constraint meets_fk2 foreign key(rno) references ROOMS(RNO)
);
These are the parent tables:
create table Courses
(
cid char(20),
cname char(20),
credits integer,
constraint CoursesKey Primary Key (cid, cname)
);
CREATE TABLE ROOMS
(
rno INTEGER,
address CHAR(20),
capacity INTEGER,
CONSTRAINT room_key PRIMARY KEY(rno)
);
I don't understand why I am getting this error.
Cause
The ORA-2270, as the error message suggests, happens when there is no matching unique or primary key for this column-list. This could be because
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.
Now in your COURSES table, CID is not a primary key. It is a combination of cid,cname. So for every cid, there can be multiple rows.
Now when you reference cid as foreign key for meets_in, it will not work as it violates the second point as I mentioned above.
Workaround
Add column cname in your meets_in table as well. Then use it like below.
create table Meets_In
(
cid char(20) not null,
cname char(20),
rno integer not null,
time1 char(20) not null,
CONSTRAINT PRIM_KEY PRIMARY KEY(time1),
constraint meets_fk1 foreign key(cid,cname) references COURSES (cid,cname), /*Added cid,cname */
constraint meets_fk2 foreign key(rno) references ROOMS (RNO)
);
Meets_In is acting as an associative table. Therefore its primary key should include the foreign keys into the tables it is associating.
Try a primary key consisting of: cid, cname, rno and time.
As others have noted, your primary key for courses is (cid, cname), so you also need to include both of these in your foreign key constraint meets_fk1. Or, if possible, ensure that cid only is the primary key on courses.
(I think time may be a reserved word, so perhaps consider renaming it.)

Oracle SQL Database error: "no matching unique or primary key for this column-list"

I'm trying to set up a database in Oracle sql developer, I've got these 3 tables.
I need the table "GuyAddress" to have 3 primary keys, which are all foreign keys as well. This is where I'm running into an error which I can't get my head around.
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
"number" NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
This is the error, hopefully someone can spot the mistake I made because I can't...
Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
)
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
Thanks!
You don't need separate foreign keys for each column in the referenced table's primary key - you can have multiple columns in a foreign key, e.g.:
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
address_number NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
Note that I've updated your address.number column to be address.address_number, as it's not recommended that you use a column based on a keyword. Using doublequotes to get around this (and also enforce case sensitivity) is not recommended either; you'll have to remember to use them every time you reference that column!
As an aside, I assume that your address table has other columns? Because as things stand, the address table is pointless and could be skipped!

SQL. How to reference a composite primary key Oracle?

I have two parent tables: Treatment and Visit.
Treatment table:
create table Treatment (
TreatCode CHAR(6) constraint cTreatCodeNN not null,
Name VARCHAR2(20),
constraint cTreatCodePK primary key (TreatCode),
);
Visit table:
create table Visit (
SlotNum NUMBER(2),
DateVisit DATE,
ActualArrivalTime DATE,
constraint cVisitSlotDatePK primary key (SlotNum, DateVisit)
);
Now I try to create a child table:
create table Visit_Treat (
TreatCode constraint cTreatCodeFK references Treatment(TreatCode),
SlotNum constraint cSlotNumFK references Visit(SlotNum),
DateVisit constraint cDateFK references Visit(DateVisit),
constraint cVisitTreatPK primary key (SlotNum, TreatCode, DateVisit)
);
Everything executes fine till the 3 line. Starting from the 3rd line, i.e. SlotNum constraint ... there is a message: no matching unique or primary key. There was already a similar question, but i did not quite get the logic to apply in my case. I reference each column one by one, and it works for the Treatment table parent. How should i correct reference Visit table parent?
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
in your case
create table Visit_Treat (
TreatCode CHAR(6) constraint cTreatCodeFK references Treatment(TreatCode),
SlotNum NUMBER(2),
DateVisit DATE,
constraint cVisitTreatPK primary key (SlotNum, TreatCode, DateVisit),
constraint fk_slotnumDatevisit FOREIGN KEY(SlotNum,DateVisit)
references Visit(SlotNum,DateVisit)
);
A foreign key must reference the primary key of the parent table - the entire primary key. In your case, the Visit table's primary key is SlotNum, DateVisit but the foreign key from Visit_Treat only references SlotNum.
You have two good options:
Add a DateVisit column to Visit_Treat and have the foreign key be SlotNum, DateVisit, referencing SlotNum, DateVisit in Visit.
Create a non-business primary key on Visit (for example a column named VisitID of type NUMBER, fed by a sequence), add a VisitID column to Visit_Treat, and make that the foreign key.
And two bad options:
Change the Visit primary key to be only SlotNum so your Visit_Treat foreign key will work. This probably isn't what you want.
Don't use a foreign key. I don't recommend this option. If you're having trouble setting up a foreign key that you know should exist, it generally means a design problem.

Invalid FK and PK reference

Unable to create table as oracle shows ' no matching unique or primary key for this column-list' when I did label the primary key reference for the required table.
First table created successfully:
CREATE TABLE TEST
(
TESTno VARCHAR2(6) NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TEST_Date DATE NOT NULL,
ACTUAL DATE,
PREDICTED Date,
CONSTRAINT TESTPKs PRIMARY KEY (TEST_Date, TESTno, ExamNo),
CONSTRAINT TTESTNO_Fk FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
Here's the table i want to create and gives me error:
CREATE TABLE Assignment
(
TEST_Date DATE NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TestNo VARCHAR2(6) NOT NULL,
Type VARCHAR2(20),
Hours_Spent Decimal(4,2),
CONSTRAINT ASSIGNPKS PRIMARY KEY (TEST_Date, TestNo , ExamNo),
CONSTRAINT ASSIGNTESTDATE_FK FOREIGN KEY (TEST_Date) REFERENCES TEST(TEST_Date) ON
DELETE CASCADE,
CONSTRAINT ASSIGNTESTNO_FK FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
May i know where's the issue that it keeps giving me no matching unique primary keys? I already tried to recreate and labelled the 'test_Date' as my primary key. But oracle can't seems to find.
Thanks
The PK you refer to is PRIMARY KEY (TEST_Date, TESTno, ExamNo) — hence the foreign key should be FOREIGN KEY (TEST_Date, TESTno, ExamNo) as well. The error you're getting is due to your attempt to refer to a part of TEST's PK.
See also http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c22integ.htm
Check the tables you are referencing in your foreign keys. Those columns must be the primary key or otherwise unique on the foreign table.