• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

I need an android Example which explains foreign key concept

  • Thread starter Thread starter Android Question
  • Start date Start date
A

Android Question

Guest
Hi I need a complete code which Explains me foreign key concept in detail, why we use foreign keys? how do we use them..please please
 
A foreign key is basically a column on one table that can only contain values from a column on another table.

So, for example behind this forum there may be two tables, USERS and POSTS. The USERS table will have a column (probably the Primary Key) called something like USERID as well as other columns. The POSTS table will also have a USERID column and this will only allow values that exist in USERS.USERID in order to ensure that every post links to a valid user.

Note that a User ID can only exist ONCE in USERS but can exist any number of times in POSTS - because a user can make lots of posts.

In a database, the foreign key column will usually be indexed as it will frequently be used in joins.
 
Back
Top Bottom