Tech Point Fundamentals

Tuesday, September 7, 2021

SQL Foreign Key Constraint Interview Questions

SQL Foreign Key Constraint Interview Questions and Answers

SQL-Foreign-key-interview-questions

A Foreign Key is a constraint that enforces referential integrity in the SQL Server database. To create a SQL Foreign Key Constraint, the parent table should have a Primary Key column or column with a UNIQUE Constraint or UNIQUE Index. A table can have more than one Foreign Key Constraint, unlike a Primary Key. In this article, we will walk through some interview questions based on the foreign keys.


Please visit our Youtube Channel for more SQL Interview Questions and Answers videos by the below link:







Below are some common interview questions based on the SQL Foreign Key Constraint:


QA. What is the difference between PRIMARY KEY and FOREIGN KEY? 


QB. What is the difference between UNIQUE KEY and FOREIGN KEY?


QC. What is the difference between UNIQUE KEY and UNIQUE INDEX?


QD. What is the difference between FOREIGN KEY Constraint and CHECK Constraint?




Q01. What is the use of FOREIGN KEY Constraints?


Q02. Can a unique constraint be referenced by a FOREIGN KEY Constraint?


Q03. How many Foreign Keys can you create in a table?


Q04. Can a FOREIGN KEY reference multiple columns of the parent table?


Q05. Is NULL value allowed in the FOREIGN KEY column?


Q06. Can a FOREIGN KEY have more than one matching record in the parent table?


Q07. Can a FOREIGN KEY exist without the key of the parent table?


Q08. Can you create a FOREIGN KEY on a Composite Primary Key?


Q09. Can you create a composite FOREIGN KEY?


Q10. Which type of index is created by default for the FOREIGN KEY constraint?



Q11. Can a FOREIGN KEY contain duplicate values?


Q12. Can you create a FOREIGN KEY without referencing any table? or Can a FOREIGN KEY reference a column of the same table?


Q13. Can a FOREIGN KEY reference the FOREIGN KEY column of other tables?


Q14. What is the use of the CASCADE keyword? or What is CASCADE DELETE or CASCADE  UPDATE in SQL?


Q15. How many tables are required for creating a FOREIGN KEY constraint? or How can you create the FOREIGN KEY in a single table?


Q16. Can a FOREIGN KEY reference the Non-KEY column of the parent table?


Q17. What is the use of WITH CHECK and WITH NO CHECK clause in the FOREIGN KEY?


Q18. How can you create a FOREIGN KEY Constraint after creating a table?


Q19. Are duplicate key names allowed in the database?



Q20. What is the difference between a trusted and non-trusted referential integrity constraint?


Q21. Can a FOREIGN KEY column contain NULLvalues?


Q22. How many NULL values are allowed in a FOREIGN KEY column?


Q23. Can a FOREIGN KEY Contain an empty(blank) value or whitespace?


Q24. Can you make the FOREIGN KEY column an identity column?


Q25. Can you create an index on the FOREIGN KEY column?


Q26. How can you delete or update the records referenced by the FOREIGN KEY constraint?


Q27. Can you disable the FOREIGN KEY constraint in any table?


Q28. What is the use of the 'NOT FOR REPELCIATION' option in the FOREIGN KEY constraint?


Q29. Can you add FOREIGN KEY REFERENCES on the temp table?


Q30. Can you add multiple FOREIGN KEY REFERENCES on the same column?



Q31. Can you ALTER or change the existing FOREIGN KEY REFERENCES?


Q32. Can you create a FOREIGN KEY Constraint on the computed column?


Q33. Can you reference a column in the FOREIGN KEY which is neither Primary Key nor Unique Key but has a Unique Index?


Q34. Can you create a FOREIGN KEY Constraint without referencing any column name?


Q35. Can a FOREIGN KEY REFERENCES across the database table? or Can you reference an external database table for creating the FOREIGN KEY REFERENCES?


Q36. What will happen if a foreign key column has multiple foreign key constraints with different CASCADE actions that reference the same column?


Q37. What is the difference between INSERT INTO and BULK INSERT when you have to add the data into a table which has FOREIGN KEY REFERENCES?


Q38. Can you drop a column index that is referenced by the FOREIGN KEY column?


Q39. Can you add a FOREIGN KEY constraint on the varchar(max) column?


Q40. How do the FOREIGN KEY Constraints maintain the referential integrity of the database?



Q41. I have organization data structured in different databases, how will you make sure that the referential integrity is preserved across the databases?


Q42. Can you reference a temp table column for creating a FOREIGN KEY constraint?


Q43. How can you delete the records of the primary (parent) table without deleting the matching foreign key reference in the secondary table?


Q44. I have two tables User and UserRole with records. The UserRole table has a nullable FOREIGN KEY for userId. Now how can you delete the top 10 users from the User table?


Q45. If you have inserted any record in the primary (parent) table, is it mandatory to insert the records into the secondary (child) table which contains the FOREIGN KEY?


Q46. You have added a FOREIGN KEY constraint while creating the table, but you did not provide the key name for the Foreign Key. How can you drop that FOREIGN KEY constraint?


Q47. You have two tables User(Id, Name) and UserRole(UserId, RoleName) which contain data already but there are some records in the UserRole table for which there is no matching User record into the User table. How can you add a FOREIGN KEY constraint on the UserId column of the UserRole table for the Id column of the User table?


Q48. Can you DROP a function that is being referenced by the CHEK Constraint?




Live Demo





Watch our related videos here




Recommended Articles


Most Frequently Asked SQL Interview Questions
Most Frequently Asked C# Interview Questions
Primary Key Interview Questions
Unique Key Interview Questions
Derived Table (DT) Interview Questions
Unicode vs Non-Unicode Data Types Interview Questions
Table Variable (TV) Interview Questions
Table-Valued Type (TVT) Interview Questions
Common Table Expression(CTE) Interview Questions
Temp Table Interview Questions
Static Class Interview Questions and Answers
Sealed Class Interview Questions and Answers
Sealed Method Interview Questions and Answers
Abstract Class vs Interface
Interface Interview Questions and Answers





Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn, Telegram, WhatsApp, Youtube, and Quora for regular updates.

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.