25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Database Programming ” 147<br />

CREATE INDEX <br />

ON ([, ..., ]);<br />

For example, suppose we wanted to create a unique index on the isbn column of the<br />

book table we created earlier:<br />

CREATE INDEX book_isbn ON book (isbn);<br />

i<br />

The name of the index is, of course, entirely arbitrary and only has a meaning when<br />

deleting the latter; however, it must still be unique and abide by the naming rules we<br />

described above.<br />

Foreign-key relationships are created either when a table is created, or at a later<br />

date with an altering statement. For example, suppose we wanted to add a table that<br />

contains a list of all of the chapter titles for every book:<br />

CREATE TABLE book_chapter (<br />

isbn VARCHAR(13) REFERENCES book (id),<br />

chapter_number INT NOT NULL,<br />

chapter_title VARCHAR(255)<br />

);<br />

This code creates a one-to-many relationship between the parent table book and the<br />

child table book_chapter based on the isbn field. Once this table is created, you can<br />

only add a row to it if the ISBN you specify exists in book.<br />

To create a one-to-one relationship, you can simply make the connective columns of<br />

a one-to-many relationship the primary key of the child table<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

Dropping Objects<br />

The act of deleting an object from a schema—be it a table, an index, or even the<br />

schema itself—is called dropping. It is performed by a variant of the DROP statement:<br />

DROP TABLE book_chapter;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!