Question

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.

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top