Can FOREIGN KEY column contain null value or duplicate values

[5895 views]




What is Foreign Key?

FOREIGN KEY is a key that is used to link two tables together. A FOREIGN KEY is a column or collection of columns in one table that refers to the PRIMARY KEY in another table. The table containing the PRIMARY KEY is called the child table and the table that contains the PRIMARY KEY is called the Parent Table.

Can FOREIGN KEY column contain null value

YES, FOREIGN KEY column can contain null values. Null by definition means not a value. Null means that we do not yet know what the value of the column is.
For Example, we have three tables Pictures, Videos and Comments in an application which allows comments on pictures and also on videos. In comments table we have two Foreign Keys p_id and v_id along with the primary Key comment_id. So when you comment on a video only v_id would be required and p_id is not required so it would be null. Also,if you comment on a picture only p_id would be required and v_id would be null.

Can FOREIGN KEY column contain Duplicate values

YES, FOREIGN KEY column can contain duplicate values. FOREIGN KEY can be used for One to Many Relationship.
For Example, we have an order table and the order details table. If any customer orders five items, he has one order and five order detail records that contain the same orderID as the FK.

                 






Comments










Search Anything:

Sponsored Deals ends in





Technical Quizzes Specially For You:

Search Tags