0

What exactly is Room @ForeignKey used for?

I know that it is used for linking two tables, so that whenever some update happens to the parent it updates children as well. For example,

onDelete = ForeignKey.CASCADE

I suppose it's nothing but my given definition (second paragraph), right?.

The reason I am asking this question is in OrmLite for example when you define foreign = true then you can have join database and can fill the foreign value with data. This you can not do with @ForeignKey of Room. Here is a detailed explanation of what foreign does in OrmLite.

Am I right?

musooff
  • 6,412
  • 3
  • 36
  • 65
  • 1
    What did your research show? [ask] – philipxy Oct 25 '18 at 04:15
  • 1
    Did you mean, "for linking two" tables (not "databases")? Does "it's nothing but my given definition" mean, it is only for what you said in your 2nd paragraph? Also, it is not clear what you mean by "then you can have join database and can fill the foreign value with data". Maybe you could quote documentation. Please use more words & sentences. – philipxy Oct 27 '18 at 03:29
  • yes, that how I meant and I edited question – musooff Oct 28 '18 at 02:46

1 Answers1

1

FKs (foreign keys) are a relational database concept. A FK says table subrows appear elsewhere uniquely. Equivalently, a FK says entities that participate in a relation(ship)/association participate uniquely in another. Those statement are equivalent because in a relational database a table represents entities/values that participate together per a relation(ship)/association--hence "the Relational Model" & "the Entity-Relationship Model".

The FK graph can be used for convenience/shorthand: default join conditions; preventing updates to invalid states; cascading updates; getting a unique value associated with an entity in the other relation(ship)/association; simultaneously setting values in one relation(ship)/association and the other one. FKs are wrongly called "relationships" and don't have to be known to query. They must be known to ask for a single value associated with an entity, but we can always just ask for a set of values whether or not it might always only ever have one element.

FKs, CKs (candidate keys), PKs (primary keys) & superkeys (unique column/field sets) are special cases of constraints, which are just conditions that are always true in every database state & (equivalently) businesss situation. They are determined by the relation(ship)s/associations & the valid business situations that can arise. When we tell the DBMS about them it can prevent update to a state that must be invalid because it violates them.

What is the difference between an entity relationship model and a relational model?

philipxy
  • 14,867
  • 6
  • 39
  • 83
  • Thank you for the explanation of FKs. But I still didn't get an answer to my question. That's maybe, since I am new to SQL and DB management. I wanted to know if `foreign` of **OrmLite** is not equivalent of `@ForeignKey` of **Room**. Is room FK used only for whenever we update parent child will be also updated? – musooff Oct 25 '18 at 10:01
  • 1
    Please clarify by editing your post to the best it can be, rather than by commenting. Use enough words & sentences to clearly say what you mean. – philipxy Oct 25 '18 at 10:03