Sign in Go Pro

Storing Data in Android with Room

Foreign Key Relationships

11/13/2018

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

Up next

Previous

About

In this lesson, we'll describe foreign keys and how they can be used to establish relationships between entities in the database.

We'll add a new User entity to the project and show how we can create a foreign key relationship between a Task and a User, so that a task can be assigned to a user.

We'll show how you can have the database perform an action when you delete one part of a foreign key relationship so that your database is always in a valid state.

Summary of Content:

  • How to define a foreign key in the @Entity annotation
  • What problems we might have if we don't use a foreign key
  • What referential integrity is and why it is important.
  • Why we might see a SqliteConstraintException when deleting an entity, and how to fix it
  • Defining an onDelete action to be performed when a User is deleted
  • How we could delete Tasks when we delete their assigned User using CASCADE
  • How we could automatically unassign Tasks from a deleted User using SET_NULL.

Links:

Instructor

Links

Comments

There are no comments on this lesson, start the conversation below ...

>
You need to go PRO to post comments.

Lessons in Storing Data in Android with Room