Wednesday, March 30, 2011

DB2 Relational Tables

Is it possible to create a database such that there are 2 tables across 2 different schemas within the database who reference each other?

I would like to clarify my question with an example.

Consider the tables, EMPLOYEE (empID, empName, deptId) and DEPARTMENT (deptId, deptName). We can impose a foreign key constraint on the EMPLOYEE.deptId. In this case can I have these two tables across two different schemas and still impose the constraint?

From stackoverflow
  • You mean via foreign keys or in joins?

    Sure: Just always prefix the table name with the schema name and a "." (dot). Like so:

    select t1.id, t2.id from schema1.table1 t1 join schema2.table2 t2 on t1.fid = t2.id
    
    Shyam : I meant referential integrity by means of a constraint.
    Aaron Digulla : In that case, you will have to elaborate in your question. What are you trying to achieve? How do the tables look? What's the constraint you want to be asserted?
  • The answer is yes. DB2 does not require both tables in a foreign key relationship to reside in the same schema.

0 comments:

Post a Comment