Re: all foreign key should have index?



In message <m3irs1hfsw.fsf@xxxxxxxxxxxxxxxxxxxxxxx>, Christopher Browne <cbbrowne@xxxxxxx> writes
Is it necessary that all foreign key should have index?

It is not necessary from a theoretical standpoint.

It is necessary, from a practical standpoint, if the table pointed to
is fairly large, and you want to be able to validate the foreign key
references in a reasonable period of time.

SELECT * FROM user_constraints WHERE CONSTRAINT_TYPE = 'R'
AND INDEX_NAME IS NOT  NULL

Above query does not return any row in my database. Is it bad?

I don't know. Did you create a table in your database called "user_constraints?" That's not one of the standard view names in the SQL-92 INFORMATION_SCHEMA schema, so it's certainly not something that could be considered "standard."

Talking about Oracle by the look of it - in which case that query always returns nothing, the INDEX_NAME column in USER_CONSTRAINTS is used only to identify the index used to enforce a primary or unique key.


If there is no index on the foreign key column(s) in the child table, then Oracle will lock the entire child table for a delete or primary key update on the parent table - so you often, but not always, want the index, apart from the performance issue referred to above.

To check if a suitable index exists you will have to look in USER_IND_COLUMNS.
--
Eric Junkermann
.




Relevant Pages

  • Re: Foreign key question
    ... then the children have two foreign key fields relating ... But each parent will also have only one first child, ... Eg you have a table of planes and a table of propeller types. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: SQLXML sp2 Identity Propagation
    ... > Parent, Child, and GrandChild. ... there is a column ExportSeq. ... > On Parent, this column is an Identity and Primary Key. ... > ExportSeq is a Foreign Key to Parent on ExportSeq, ...
    (microsoft.public.sqlserver.xml)
  • Re: Cascading updates and NULL
    ... foreign key references between the parent and the child and use cascading ... a parent and a child. ... >> DataCol into the child table, cascading updates work fine. ...
    (microsoft.public.sqlserver.programming)
  • Re: deleting a row potentially referenced by many rows in another table
    ... you would use ON DELETE CASCADE on a foreign key in a child table to ... FOREIGN KEY REFERENCES Parent ... row in the child table referencing that (parent) row is deleted (N:1 ...
    (comp.databases.theory)
  • Re: Cant enter records through subform
    ... Right-click the edge of the subform control, ... Link Child Fields ... Access should assign the foreign key value for you. ... The child table is void of records at the moment but the parent ...
    (microsoft.public.access.formscoding)