Discussion Locked

MS Access 97 referential integrity

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

This is repeating of my last question.
I need to use complex foreign key, some
fields of this is not required. Simplified
example showed below (in real database values of required fields defines by another foreign key and ckeck)

CREATE TABLE A
(F1 TEXT(3) NOT NULL,
F2 TEXT(3) NOT NULL,
INFO TEXT(32) NOT NULL,
CONSTRAINT PK PRIMARY KEY (F1,F2));

CREATE TABLE B
(F1 TEXT(3) NOT NULL,
F2 TEXT(3) NULL,
INFO TEXT(32) NULL,
CONSTRAINT FK FOREIGN KEY (F1,F2) REFERENCES A(F1,F2));

In MS Access 97 I can't insert into table B
values ('001',NULL,'INFO') - error 3201 generated. In MS Access 2.0 such insert is
allowed. In ORACLE DBMS this code also work correctly. Can I tune MS Access 97 to allow this input (last recommendation to allow NULL in A.F2 is not work) - Thank you.

4 replies

by bob sellman

Because a null value is not allowed in a component of a primary key, you will not be able to have a matching value in B where F2 is null, since the key in table A cannot contain a null value for F2.

There are two solutions I would suggest:

1.Create a different primary key in table A (perhaps use an auto number column that you won't normally use). Then you can create an index of F1 and F2 that will permit F2 to be null and that can be referenced by table B (with F2 null).

2. Create a new column in both tables that is a concatenation of F1 and F2 (with F2 permitted to be null in both tables). The new column (I'll call it F1F2) would be the primary key in table A and the value in table B would reference F1F2 in table A. For this to work correctly your input forms would have to concatenate F1 and F2 into F1F2 as they are entered by the user, although the user would never have to see the F1F2 field.

Good luck.

by donx

What you are trying to do is a violation of referential integrity. Because you are trying to input a data that is not a legal value in Table A. The value you are allowed to input in Table B are only those values present in Table A. This means that the values ('001,NULL) will not be accepted in Table A. Before you can create this values in Table B, it must be present in Table A. This is waht it meant be referential integrity. If this values of yours are allowed to be entered in Table B then the record becomes an orphan, without a related record in Table A. If you want your idea to work then remove the 'constraint' from Table B. Because of this 'constraint', Table B is bound to Table A via foreign key.

by avhohlov

There are many reasons to use foreign keys with null fields. For example table of currency rates showed below (CUR_ID - currency code, RAT_D - date of rate, RAT_P - date of pred. rate; DBMS-ORACLE, not MS Access):
CREATE TABLE RATES
(CUR_ID VARCHAR2(3) NOT NULL,
RAT_D DATE NOT NULL,
RAT_P DATE,
RATE NUMBER(11,4) NOT NULL,
SIGN CHAR(1),
CONSTRAINT PK1 PRIMARY KEY (CUR_ID,RAT_D),
CONSTRAINT UK1 UNIQUE (CUR_ID,RAT_P),
CONSTRAINT FK1 FOREIGN KEY (CUR_ID,RAT_P) REFERENCES RATES (CUR_ID,RAT_D),
CONSTRAINT AK1 UNIQUE (CUR_ID,SIGN),
CONSTRAINT CH1 CHECK (((RAT_P IS NULL) AND (SIGN IS NOT NULL) AND (SIGN='1')) OR ((RAT_P IS NOT NULL) AND (SIGN IS NULL))));

This discussion is closed to new replies.