11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

Create successful ePaper yourself

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

www.it-ebooks.infoCHAPTER 36 • INDEXES AND SEARCHING■ Note You can have only one automatically incrementing column per table, and that column must be designatedas the primary key. Furthermore, any column designated as a primary key cannot hold NULL values; even if notexplicitly declared as NOT NULL, <strong>MySQL</strong> will automatically assign this trait.It is typically ill-advised to create a primary index that allows the developer to divine someinformation about the row it represents. The reason why is demonstrated with an illustration. Ratherthan use an integer value as the bookmarks table’s primary index, suppose you decide to instead use theURL. The repercussions involved in making such a decision should be obvious. First, what happens ifthe URL changes due to a trademark issue or an acquisition, for example? Even Social Security numbers,values once taken for granted as being unique, can be changed due to the repercussions of identity theft.Save yourself the hassle and always use a primary index that offers no insight into the data it represents;it should be an autonomous vehicle with the sole purpose of ensuring the ability to uniquely identify adata record.Unique IndexesLike a primary index, a unique index prevents duplicate values from being created. However, thedifference is that only one primary index is allowed per table, whereas multiple unique indexes aresupported. With this possibility in mind, consider again the bookmarks table from the previous section.Although it’s conceivable that two sites could share the same name—for example, “Great <strong>PHP</strong>resource”—it wouldn’t make sense to repeat URLs. This sounds like an ideal unique index:CREATE TABLE bookmarks (id INT UNSIGNED NOT NULL AUTO_INCREMENT,name VARCHAR(75) NOT NULL,url VARCHAR(200) NOT NULL UNIQUE,description MEDIUMTEXT NOT NULL,PRIMARY KEY(id));As mentioned, it’s possible to designate multiple fields as unique in a given table. For instance,suppose you want to prevent contributors to the link repository from repeatedly designatingnondescriptive names (“cool site,” for example) when inserting a new web site. Again returning to thebookmarks table, define the name column as unique:CREATE TABLE bookmarks (id INT UNSIGNED NOT NULL AUTO_INCREMENT,name VARCHAR(75) NOT NULL UNIQUE,url VARCHAR(200) NOT NULL UNIQUE,description MEDIUMTEXT NOT NULL,PRIMARY KEY(id));You can also specify a multiple-column unique index. For example, suppose you want to allow yourcontributors to insert duplicate URL values, and even duplicate name values, but you do not wantduplicate name and URL combinations to appear. You can enforce such restrictions by creating amultiple-column unique index. Revisiting the original bookmarks table:695

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

Saved successfully!

Ooh no, something went wrong!