Why foreign keys are allowed to have NULL values? Explain with an example.

Foreign Key is used to represent the relation between two tables. Because in the field with Foreign key attribute data comes from Primary key field in another related table and so Foreign key are allowed to have Null Values if it not the part of Primary key field of the related table. For example-

Here both the table ‘Customer’ and ‘Order’ are related with each other with a common field ‘CCode’ which is Primary key in Customer table and Foreign key in Order table. So we cannot insert a new CCode in Order table if it does not exist in Customer table but we can have null values instead.

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!