11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

CHAPTER 28 • MYSQL STORAGE ENGINES AND DATA TYPESwww.it-ebooks.infoDEFAULTThe DEFAULT attribute ensures that some constant value will be assigned when no other value isavailable. This value must be a constant, because <strong>MySQL</strong> does not allow functional or expressionalvalues to be inserted. Furthermore, this attribute cannot be used in conjunction with BLOB or TEXT fields.If the NULL attribute has been assigned to this field, the default value will be null if no default is specified.Otherwise (specifically, if NOT NULL is an accompanying attribute), the default value will depend on thefield data type.An example of a DEFAULT attribute assignment follows:subscribed ENUM('0','1') NOT NULL DEFAULT '0'INDEXIf all other factors are equal, the use of indexing is often the single most important step you can taketoward speeding up your database queries. Indexing a column creates a sorted array of keys for thatcolumn, each of which points to its corresponding table row. Subsequently searching this ordered keyarray for the input criteria results in vast increases in performance over searching the entire unindexedtable because <strong>MySQL</strong> will already have the sorted array at its disposal. The following exampledemonstrates how a column used to store employees’ last names can be indexed:CREATE TABLE employees (id VARCHAR(9) NOT NULL,firstname VARCHAR(15) NOT NULL,lastname VARCHAR(25) NOT NULL,email VARCHAR(45) NOT NULL,phone VARCHAR(10) NOT NULL,INDEX lastname (lastname),PRIMARY KEY(id));Alternatively, an index could be added after a table has been created by making use of <strong>MySQL</strong>’sCREATE INDEX command:CREATE INDEX lastname ON employees (lastname(7));This section offers a slight variation on the previous one, this time indexing only the first sevencharacters of the first name because more letters probably won’t be necessary to differentiate amongfirst names. Performance is better when smaller indexes are used, so you should strive for smallerindexes whenever practical.NATIONALThe NATIONAL attribute is used only in conjunction with the CHAR and VARCHAR data types. Whenspecified, it ensures that the column uses the default character set, which <strong>MySQL</strong> already does bydefault. In short, this attribute is offered as an aid in database compatibility.544

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

Saved successfully!

Ooh no, something went wrong!