musooff
musooff

Reputation: 6852

What is Android Room foreign key used for?

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?

Upvotes: 0

Views: 92

Answers (1)

philipxy
philipxy

Reputation: 15118

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?

Upvotes: 1

Related Questions