0

Is it possible to create an index that has as one of its columns values from another table?

Example:

model Pet
  primary_key id
  foreign_key Species
end
model Species
  primary_key id
  int genus
end

Assume there's a lot of kinds of species, with fewer types of genuses. I'd like to create an index on the Pets table by Genus. Can it be done?

If so, I'd be extra grateful if you could point me in the right direction on how to do it in Rails migration.

1
  • I don't know postgres but I do not believe that is possible. You can definitely make an index by using the Species FK but that won't help you as really the genus column should be a FK to a genus table. Commented Mar 5, 2010 at 22:56

1 Answer 1

2

No. In any relational database technology, "index" means an index of the table. You could instead combine the two like this:

ANIMAL_CLASS
 + name
 + id
 + LEVEL
 + parent_id

Where level is logically an enumeration of {SPECIES, GENUS, .... }

Sign up to request clarification or add additional context in comments.

1 Comment

Not exactly. Oracle supports bitmap join indexes -- where you are indexing an attribute of a parent table on the child table. It's designed to eliminate IO against star/snowflake schemas in star transformation queries. So Oracle could, in this case, index Species.Genus against the Pet table, as each Pet has at most one Genus.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.