on 2017 Jul 25 11:42 AM
Hi
Having a foreign key table with column
role_supplementary_namesometimes NULL, I tried to apply following statement but get an Error: No primary key 'persons_role_precised_by_fk' in table 'persons_roles'.
This seems to point to the fact that one of the foreign key column contains NULL values. I tried with the MATCH SIMPLE clause but with no success. Any hint is welcome.
ALTER TABLE persons_roles ADD CONSTRAINT persons_role_precised_by_fk FOREIGN KEY ( role_supplementary_name, role_id ) REFERENCES role_supplementaries ( name, role_id )
Check your data. It may be the non-null data causing the problem.
The behavior you want (now called MATCH SIMPLE) has been the default since at least Version 6.
CREATE TABLE role_supplementaries ( name INT NOT NULL, role_id INT NOT NULL, str VARCHAR(10), PRIMARY KEY (name,role_id)); INSERT INTO role_supplementaries VALUES(1,10,'one-ten'); INSERT INTO role_supplementaries VALUES(2,20,'two-twenty'); COMMIT; CREATE TABLE persons_roles ( fpk INT PRIMARY KEY, role_supplementary_name INT, role_id INT ); INSERT INTO persons_roles VALUES(100,1,10); INSERT INTO persons_roles VALUES(200,null,null); INSERT INTO persons_roles VALUES(300,2,null); INSERT INTO persons_roles VALUES(400,null,10); INSERT INTO persons_roles VALUES(500,null,11111); INSERT INTO persons_roles VALUES(600,22222,null); ALTER TABLE persons_roles ADD CONSTRAINT persons_role_precised_by_fk FOREIGN KEY ( role_supplementary_name, role_id ) REFERENCES role_supplementaries ( name, role_id ); SELECT *, @@VERSION FROM persons_roles ORDER BY fpk; fpk role_supplementary_name role_id @@VERSION ----------- ----------------------- ----------- ---------- 100 1 10 6.0.4.3594 200 (NULL) (NULL) 6.0.4.3594 300 2 (NULL) 6.0.4.3594 400 (NULL) 10 6.0.4.3594 500 (NULL) 11111 6.0.4.3594 600 22222 (NULL) 6.0.4.3594 fpk role_supplementary_name role_id @@VERSION ----------- ----------------------- ----------- ----------- 100 1 10 17.0.7.3399 200 (NULL) (NULL) 17.0.7.3399 300 2 (NULL) 17.0.7.3399 400 (NULL) 10 17.0.7.3399 500 (NULL) 11111 17.0.7.3399 600 22222 (NULL) 17.0.7.3399
Mismatched non-null values do cause a problem...
INSERT INTO persons_roles VALUES(700,22222,11111); V6... Could not execute statement. No primary key value for foreign key 'persons_role_precised_by_fk' in table 'persons_roles' SQLCODE=-194, ODBC 3 State="23000" Line 1, column 1 INSERT INTO persons_roles VALUES(700,22222,11111) V17... Could not execute statement. No primary key value for foreign key 'persons_role_precised_by_fk' in table 'persons_roles' SQLCODE=-194, ODBC 3 State="23000" Line 1, column 1 INSERT INTO persons_roles VALUES(700,22222,11111)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You are absolutely correct. The problem laid in the data, I had 4 rows having an empty string instead of NULL. That caused the problem.
After changing these to NULL, the constraint is applicable.
I am also tested the constraint with MATCH FULL (at the end of the statement) instead of using MATCH SIMPLE (the default as you mention). What I am surprised that the constraint is also applicable with MATCH FULL.
I understand the documentation the way that MATCH FULL means no one column (of my two columns) must contain a NULL value, but it seems I am reading it not correctly.
What exactly is then the difference between MATCH SIMPLE and MATCH FULL?
Thanks and regards, Robert
See that other FAQ...
User | Count |
---|---|
62 | |
10 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.