Foreign key errors can also arise when performing update or delete operations. For example, suppose you try to remove the R&D department from the Departments table. The DepartmentID field, being the primary key of the Departments table, constitutes the ONE side of a one-to-many relationship (the DepartmentID field of the Employees table is the corresponding foreign key, and forms the MANY side). A record on the ONE side of a relationship may not be deleted until all corresponding records on the MANY side are deleted.
Suppose you attempt to delete the R&D department (DepartmentID 100) in the Departments table. An error is reported indicating that there are other records in the database that reference the R&D department, and the delete operation is not performed. To remove the R&D department, you need to first get rid of all employees in that department, as follows:
DELETE FROM Employees WHERE DepartmentID = 100; |
Now that you deleted all the employees that belong to the R&D department, you can now delete the R&D department:
DELETE FROM Departments WHERE DepartmentID = 100; |
Cancel these changes to the database by entering a ROLLBACK statement:
ROLLBACK; |
Now, suppose you try to change the DepartmentID field from the Employees table. The DepartmentID field, being the foreign key of the Employees table, constitutes the MANY side of a one-to-many relationship (the DepartmentID field of the Departments table is the corresponding primary key, and forms the ONE side). A record on the MANY side of a relationship may not be changed unless it corresponds to a record on the ONE side. That is, unless it has a primary key to reference.
For example, the following UPDATE statement causes an integrity error:
UPDATE Employees SET DepartmentID = 600 WHERE DepartmentID = 100; |
The error no primary key value for foreign key 'FK_DepartmentID_DepartmentID' in table 'Employees'
is raised because there is no department with a DepartmentID of 600 in the Departments table.
To change the value of the DepartmentID field in the Employees table, it must correspond to an existing value in the Departments table. For example:
UPDATE Employees SET DepartmentID = 300 WHERE DepartmentID = 100; |
This statement can be executed because the DepartmentID of 300 corresponds to the existing Finance department.
Cancel these changes to the database by entering a ROLLBACK statement:
ROLLBACK; |
In the previous examples, the integrity of the database was checked as each command was executed. Any operation that would result in an inconsistent database is not performed.
It is possible to configure the database so that the integrity is not checked until commit time using the wait_for_commit option. This is useful if you need to make changes that may cause temporary inconsistencies in the data while the changes are taking place. For example, suppose you want to delete the R&D department in the Employees and Departments tables. Since these tables reference each other, and since the deletions must be performed on one table at a time, there will be inconsistencies between the table during the deletion. In this case, the database cannot perform a COMMIT until the deletion finishes. Set the wait_for_commit option to On to allow data inconsistencies to exist up until a commit is performed. See wait_for_commit option [database].
You can also define foreign keys in such a way that they are automatically modified to be consistent with changes made to the primary key. In the above example, if the foreign key from Employees to Departments was defined with ON DELETE CASCADE, then deleting the department ID would automatically delete the corresponding entries in the Employees table.
In the above cases, there is no way to have an inconsistent database committed as permanent. SQL Anywhere also supports alternative actions if changes would render the database inconsistent. See Ensuring data integrity.
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |