25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

146 ” Database Programming<br />

These two forms are equivalent to each other— is the name of the new<br />

database you want to create. As you can see, there is no mechanism for providing<br />

security or access control—this is, indeed, a shortcoming of SQL that is solved by<br />

each database system in its own way.<br />

The creation of a table is somewhat more complex, given that you need to declare<br />

its structure as well:<br />

CREATE TABLE (<br />

[],<br />

[...<br />

[]]<br />

);<br />

As you can see, it is necessary to declare a column’s data type—as you probably have<br />

guessed, most database systems are very strict about data typing, unlike <strong>PHP</strong>. Here’s<br />

the declaration for a simple table that we’ll use throughout the remainder of this<br />

chapter:<br />

CREATE TABLE book (<br />

id INT NOT NULL PRIMARY KEY,<br />

isbn VARCHAR(13),<br />

title VARCHAR(255),<br />

author VARCHAR(255),<br />

publisher VARCHAR(255)<br />

);<br />

Here, we start by declaring a column of type INT that cannot contain NULL values and<br />

is the primary key of our table. The other columns are all VARCHARs of varying length<br />

(note how we are using 255 as the maximum allowable length; this is a “safe bet” and<br />

it is true in many, but not all, database systems).<br />

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

Creating Indices and Relationships<br />

Indices can be created (as was the example with the primary key above) while you<br />

are creating a table; alternatively, you can create them separately at a later point in<br />

time:

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

Saved successfully!

Ooh no, something went wrong!