Defining the foreign key relationship in InnoDB

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 8 docx (Trang 38 - 43)

The default MyISAM storage engine in MySQL doesn’t support foreign key constraints. If your remote server doesn’t support InnoDB, skip ahead to “Populating the tables.”

The normal way to define a foreign key relationship in MySQL is in the initial table defini- This section applies only if you are using InnoDB tables. If you have converted your tables to InnoDB by mistake, refer to “Converting a table’s storage engine.”

that phpMyAdmin takes. Defining a foreign key relationship in phpMyAdmin involves the following steps:

1.Define both parent and child tables, and set Storage Engine(Table typein older ver- sions of phpMyAdmin) to InnoDB.

2.Confirm that the foreign key column in the child table is indexed.

3.Use Relation viewto add the foreign key constraint to the child table.

Steps 1 and 2 have already been covered in the preceding sections, but you might want to convert MyISAM tables to InnoDB at a later stage, so I’ll briefly describe the process.

Checking the storage engine of a table

To find out whether a table uses the MyISAM or InnoDB storage engine, click the database name at the top of the main frame in phpMyAdmin or in the navigation frame on the left to display the database structure. The value for Typeshows the current storage engine for each table. Figure 16-5 shows that the authorsand quotationstables use InnoDB, while the userstable uses MyISAM.

Figure 16-5.Check the storage engine used by each table by viewing the database structure in phpMyAdmin.

It’s perfectly acceptable to mix different types of storage engines in MySQL. In fact, it’s rec- ommended that you use the most appropriate type for each table. MyISAM has the advan- tage of speed, but it currently lacks support for foreign key constraints and transactions.

Converting a table’s storage engine

You can change a table’s storage engine at any time, even if it already contains data. The following instructions explain how:

1.Select the table name in the list of links in the phpMyAdmin navigation frame (or click the Structureicon alongside the table name under Actionin the main frame).

2.With the table structure displayed in the main frame, click the Operationstab.

In database terminology, atransactionis a linked series of SQL queries, in which every query must succeed. If any part of the series fails, the whole series is abandoned, and the database remains unchanged. Transactions are an advanced subject beyond the scope of this book. For details, see http://dev.mysql.com/doc/refman/5.0/en/transactional-commands.html.

16

3.Select InnoDBorMyISAMfrom the Storage Enginedrop-down menu in the Table options section, as shown in the following screenshot (you might see different options, but Storage Engineis the only one you’re interested in here), and click Go:

Converting a table from MyISAM to InnoDB shouldn’t cause any problems. However, if for- eign key constraints have been defined in an InnoDB table relationship, you must first remove them before converting from InnoDB to MyISAM. Removing a foreign key rela- tionship simply involves reversing the process described in the next section.

Setting foreign key constraints in phpMyAdmin

When a table uses the InnoDB storage engine, phpMyAdmin adds a new option called Relation viewbeneath the table structure (see Figure 16-6). This is where you define for- eign key constraints.

Figure 16-6.The Relation view option lets you define foreign key constraints with InnoDB tables.

The foreign key constraint must always be defined in the child table. In the case of authors and quotations, this is quotations, because it uses the authors primary key (author_id) as a foreign key. The following instructions show you how to establish the relationship:

1.Select the child table (quotations) in phpMyAdmin, and click the Structuretab to display the table grid, as shown in Figure 16-6.

2.Click the Relation viewlink beneath the structure grid (it’s circled in Figure 16-6).

This displays the screen shown in Figure 16-7.

Figure 16-7.Relation view lets you specify what happens when a record in a parent table is deleted or updated.

Foreign key relationships can be established only on indexed columns. The quotations table has two indexed columns: quote_idis the table’s primary key, and author_idis the foreign key. As you can see in Figure 16-7, phpMyAdmin displays three drop-down menus alongside both indexed columns. These are for you to set the foreign key constraint options, so the ones you are interested in are alongside author_id. The first drop-down is where you specify which indexed column you want to reference. (The underlying SQL com- mand uses the keyword REFERENCESto establish the foreign key relationship.)

16

3.Click the down arrow on the right of the first drop-down menu.

This lists all indexed columns in InnoDB tables in the database. As you can see from the screenshot alongside, they are listed in the format databaseName.tableName.columnName (phpMyAdmin 2 omits the name of the database, and uses -> as a separator).

Since there are only two InnoDB tables in the database, the list is very short, but in a larger database, it would be considerably longer, so you need to make sure you select the right one.

4.You need to establish a reference between the author_idcolumns in the child (quotations) and parent (authors) tables. Select dwcs4.authors.author_id in the first drop-down menu alongside author_id.

5.Activate the ON DELETE drop-down menu alongside author_id. It displays the options shown here:

This is what each option means:

CASCADE: If you delete a record in the parent table, MySQL cascades the delete operation to the child table. So if you delete the record for Shakespeare in the authorstable, all records in the quotationstable with an author_idof 32are automatically deleted (see Figure 16-2 earlier in the chapter). This is a silent

operation, and you have no way of restoring the records once they have been deleted.

SET NULL: If you delete a record in the parent table, the foreign key of related records in the child table is set to NULL. For this to work, the foreign key column in the child table must accept NULL values. Taking the Shakespeare example again from Figure 16-2, if Shakespeare is deleted from the authors table, the value of author_idis set to NULLin all records that currently have a value of 32.

This leaves the quotations intact, but they are no longer related to Shakespeare.

If you subscribe to literary conspiracy theories, you could now reassign those quotations to Christopher Marlowe.

NO ACTION: This doesn’t mean what you might expect. Some database systems allow you to delay foreign constraint checks. NO ACTIONmeans a delayed check, but this is not supported in MySQL. If you select this option, MySQL treats it the same as RESTRICT.

RESTRICT: This rejects any attempt to delete records in the parent table if related records still exist in the child table. So, attempting to delete Shakespeare from the authorstable would fail unless all records with an author_idof 32in the quotationstable have already been deleted.

The fifth option is to select nothing. This applies the default action, which is the same as RESTRICT. The same options are available for ON UPDATE, although they are less useful, especially if the foreign key is the primary key in the parent table. In normal circumstances, you should never change the primary key of a record.

However, in the rare cases where this might be appropriate, the most useful options are RESTRICT and CASCADE. The former prevents changes if there are dependent records in the child table; the latter propagates the changes automati- cally to all dependent records.

6.Set both ON DELETEand ON UPDATEto RESTRICT, and click Save.

7.When it confirms the creation of the foreign key constraint, phpMyAdmin displays the SQL query that it used to change the table definition. It looks like this:

Although the field at the top of the page shows that phpMyAdmin used ON DELETE RESTRICT ON UPDATE RESTRICT, the Links to section gives the impression that your instructions were ignored. This isn’t the case, because RESTRICTis the same as the default action. In other words, the only time you need to set values for ON DELETEor ON UPDATE is if you want to set them to either CASCADEor SET NULL.

If you need to remove a foreign key constraint (for example, when converting an InnoDB table to MyISAM), set all drop-down menus to the default value, and click Save.

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 8 docx (Trang 38 - 43)

Tải bản đầy đủ (PDF)

(94 trang)