So essentially I have the following problem to implement as part of an oracle sql database:
table1(attr1, attr2)
table2(attr3, table1_attr1)
Constraint: a table2 tuple can only have some table1_attr1 for which tuple in table1 attr2 is not null.
I have tried the following (along with other commented out possibilities), but it obviously doesn't work because attr2 is not a unique value:
CREATE TABLE table1 (
attr1 VARCHAR(100) NOT NULL,
attr2 VARCHAR(200),
PRIMARY KEY(attr1)
);
CREATE TABLE table2 (
attr3 INTEGER NOT NULL,
table1_attr1 VARCHAR(100) NOT NULL,
table1_attr2 VARCHAR(50), --REFERENCES table1(attr2),
FOREIGN KEY (table1_attr1, table1_attr2) REFERENCES table1(attr1, attr2),
CONSTRAINT const_table1 CHECK(table1_attr2 IS NOT NULL),
PRIMARY KEY(attr3)
);
I need some help, since making attr2 unique is not an option. I have read about making a function somewhere, but I'm not very familiar with them. Will try it if they are the only choice.