Translating with DrWatson… this can take a few seconds the first time.
This is a genuine, complex translation. DrWatson protects commands, error codes, and log output while naturally translating the surrounding text. It’s translated once and saved.
Victor Dario Martinez got error -356 on a foreign key in Informix 9.20. Art Kagel and Michael Hoffman explained that without named referenced columns, the constraint uses table A's primary key (E_N, N), mismatching (N, R). The fix is REFERENCES A(N,R). Bob Roussey added a remark that 9.40 manuals say the optimizer will not use an index on a varchar column. Solved.
Auto-generated by Claude from the posts below — may be imperfect; read the full thread.
Hi, i am a dude on foreign key creation in informix 9.20 , why this
code fails with -356 error ( SQL Error (-356) : Data type of the
referencing and referenced columns do not match.) ?
--DROP TABLE A;
CREATE TABLE A (
E_N integer NOT NULL,
N varchar(5) NOT NULL,R char(1) NOT NULL
);
ALTER TABLE A
ADD CONSTRAINT PRIMARY KEY (E_N, N)CONSTRAINT pk_A;
ALTER TABLE A
ADD CONSTRAINT UNIQUE (N, R)CONSTRAINT pk_U_A;
--DROP TABLE B;
CREATE TABLE B (
N varchar(5) NOT NULL,R char(1) NOT NULL
);
ALTER TABLE B
ADD CONSTRAINT FOREIGN KEY (N,R)
REFERENCES ACONSTRAINT fk_A_B;
You did not name the corresponding columns from table A in the foreign key
definition. That defaults to using the referenced table's primary key which is
e_n, n not n, r. The match is done by key not by name. Just change the
constraint command as follows:
ALTER TABLE B
ADD CONSTRAINT FOREIGN KEY (N,R)
REFERENCES A(N,R)CONSTRAINT fk_A_B;
Art S. Kagel
----- Original Message -----
From: Victor Dari.... <dmartinez@siu.edu.ar>
At: 5/12 17:05
Hi, i am a dude on foreign key creation in informix 9.20 , why this
code fails with -356 error ( SQL Error (-356) : Data type of the
referencing and referenced columns do not match.) ?
--DROP TABLE A;
CREATE TABLE A (
E_N integer NOT NULL,
N varchar(5) NOT NULL,R char(1) NOT NULL
);
ALTER TABLE A
ADD CONSTRAINT PRIMARY KEY (E_N, N)CONSTRAINT pk_A;
ALTER TABLE A
ADD CONSTRAINT UNIQUE (N, R)CONSTRAINT pk_U_A;
--DROP TABLE B;
CREATE TABLE B (
N varchar(5) NOT NULL,R char(1) NOT NULL
);
ALTER TABLE B
ADD CONSTRAINT FOREIGN KEY (N,R)
REFERENCES ACONSTRAINT fk_A_B;
Victor,
When creating a foreign key, if you do not specifically mention the referenced
columns, the constraint will use the referenced table's Primary Key.
In your case, you are trying to match N, R with E_N, N, which is an obvious
data mismatch.
Try this:
ALTER TABLE B
ADD CONSTRAINT FOREIGN KEY (N,R)
REFERENCES A(N,R)CONSTRAINT fk_A_B;
>
> Hi, i am a dude on foreign key creation in informix 9.20 , why this
> code fails with -356 error ( SQL Error (-356) : Data type of the
> referencing and referenced columns do not match.) ?
>
> --DROP TABLE A;
> CREATE TABLE A (
> E_N integer NOT NULL,
> N varchar(5) NOT NULL,> R char(1) NOT NULL
> );
>
> ALTER TABLE A
> ADD CONSTRAINT PRIMARY KEY (E_N, N)> CONSTRAINT pk_A;
>
> ALTER TABLE A
> ADD CONSTRAINT UNIQUE (N, R)> CONSTRAINT pk_U_A;
>
> --DROP TABLE B;
> CREATE TABLE B (
> N varchar(5) NOT NULL,> R char(1) NOT NULL
> );
>
> ALTER TABLE B
> ADD CONSTRAINT FOREIGN KEY (N,R)
> REFERENCES A> CONSTRAINT fk_A_B;
>
>
>
↪ replying to Victor Dario Martinez
Bob Roussey — — source: IIUG Forums & Mailing Lists
Just a
note:
While investigating another problem about an index, I found in the manuals
(9.40 IDS) that the optimizer will not use an index on a varchar column. That
may only apply to indexes where the varchar is the root of an index; I did not
pursue it any further. (I just cursed the application vendor for using
varchars in many places!)
We use strictly necessary cookies to make this site work. With your
consent we’d also use optional cookies for analytics and marketing. You can accept all,
reject all, or choose. Read our Cookie Policy.