Remove
associated contacts: Delete Detail Records
When you've created the company delete page
in a previous section of this
tutorial, the only thing implemented was a simple delete from the company
table. Indeed, this removes the selected company from the database , but
what happens to the contacts associated to the company? The answer is
absolutely nothing. This means you will be stuck with some contact data
you actually can't access from the site, and you don't need. These are
called orphan records, as their parent company has been deleted.
One way to solve this problem is to fire up your database server management
utility (,
etc) and manually delete
the remaining records. But this means an extra operation, one which is
unnecessary.
The other (simpler) way is to use 's trigger . This trigger
can only be used on pages that contain a delete operation, and allows
implementing a cascade delete: all records in a detail table, associated
with the current record that is being deleted, will be erased too.
To add this functionality to your site, follow the next steps:
- Open the delete page from
the company folder. Remember that you can only use this server behavior
on a page containing a delete transaction for the master table.
- With the delete page opened
in 's editable area, apply the
server behavior. To
access it, click on the button
of the tab, then open . If the
tab is not visible, open it from .
- Once the server behavior's dialog box opens, configure
it to work on the desired detail table, in the way you want it to. Setting
up the options is quite easy, and you can follow the instructions below:
· Notice
first that the interface is divided into two tabs: ,
which allows you to set the tables, and field keys, and ,
where more in depth options regarding the trigger's properties can be
set. Of interest right now is the
tab, as all trigger properties are automatically configured.
· The first
section of the tab contains fields
regarding the master (transaction) table. These fields are automatically
retrieved from the delete transaction that already exists on page: the
company_com table, with theid_com primary key field.
· The second
section is where you define the detail table.
· In the
Detail table drop-down menu select the table
to act as detail; in this case, it's the contacts table: contact_con.
· In the
Detail foreign key
drop-down menu, select the field that acts as a link between the master
and detail table: idcom_con.
· If you've
used the same names, the dialog box looks like this:

· Now you
can click on the button to add the server
behavior to the page.
- This is all it takes to implement the removal of
associated contacts,along with the company they belong to. The final step
is to edit the page header, so that it will suggest that contacts will
be deleted too. Change the header text to ""
(without the quotes).
- Save your page and upload it to the server. When
you will attempt to delete a company in the future, all its contacts will
be removed too. You can check this by opening your database server management
utility, and browsing through the contacts table, before and after the
delete operation.
Now you can move on to the next
topic, where you will learn how to make your contact insertion page
bullet proof, by adding a mechanism to prevent adding a contact for a
company that doesn't exist.