Question

On a ibm db2 database, i have two tables, TABLE1 and TABLE2. I also have a logical file LOGICAL1 on TABLE1 LOGICAL1 filters records from TABLE1 depending on the value of some TABLE1 fields. (it's like a sql view)

I'd like to add a foreign constraint between TABLE2 and the logical file LOGICAL1.

When i tried to do it, i had a error message SQ0157 saying that LOGICAL1 is not valid in foreign key clause, that logical files or views cannot be specified in a foreign key clause.

So, is there a way to have a constraint on a filtered table ?

Was it helpful?

Solution 2

Constraints mean that all child rows have a parent row.

Consider writing a trigger over the child table that uses LOGICAL1 to validate whether the insert / write is allowed or not.

OTHER TIPS

Foreign (referential) constraints can only be applied to physical tables.

Here's some documentation links for more information:

IBM i 7.1 Information Center > Database > Reference > SQL reference > Concepts > Tables > Constraints > Referential constraints

As James points out, FK constraints are only applied to the physical table. AFAIK, this rule applies to all RDBMS systems.

But if you put the constraint on the physical, it's effects will be seen in the view.

Charles

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top