What is Foreign Key!
In Table 2, Membership_ID is the foreign Key
- A foreign key can have a different name from its primary key
- It ensures rows in one table have corresponding rows in another
- Unlike Primary key they do not have to be unique. Most often they aren’t
- Foreign keys can be null even though primary keys can not
Why do you need a foreign key ?
Suppose an idiot inserts a record in Table B such as
You will only be able to insert values into your foreign key that exist in the unique key in the parent table. This helps in referential integrity.
The above problem can be overcome by declaring membership id from Table2 as foreign key of membership id from Table1
Now , if somebody tries to insert a value in the membership id field that does not exist in the parent table , an error will be shown!
EmoticonEmoticon