IT tutorials
 
Database
 

SQL Server : Enforcing Data Integrity in Constraints (part 2) - Foreign key constraints and NULLs

5/25/2013 9:51:41 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

2. Foreign key constraints and NULLs

It is a common misconception that foreign keys always prevent orphan rows, that is, rows in the child table that do not have corresponding rows in the parent table. In fact, if the columns involved in the foreign key constraint are nullable, then we may have orphan rows.

Let's see for ourselves. Listing 10 creates a parent and child pair of tables.

Listing 10. Create a parent table (ParkShelters) and a child table (ShelterRepairs).

We have a composite FOREIGN KEY constraint on the Latitude and Longitude columns in the child table, but since both of these columns are nullable, we can add a child row without a matching parent row into the child table, even though the parent table is empty at this time, as Listing 11 demonstrates.

Listing 11. Adding repairs even though there is no such shelter.

The database engine will verify whether or not there is a matching row in the parent table if, and only if, neither the Latitude nor Longitude column in the child table contains a NULL. Listing 12, for example, will fail.

Listing 12. We cannot add repairs if both Latitude and Longitude columns in the child table are not null, and there is no matching shelter.

This demonstrates that, if we need to make sure that every row in the child table has a matching row in the parent table, it is not enough to simply create a FOREIGN KEY; in such cases, we also need to ensure that the columns involved in the FOREIGN KEY, in the child table, are not nullable. Use of nullable columns in FOREIGN KEY constraints must be reserved only for the cases when it is acceptable to have rows in the child table without matching rows in the parent one.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us