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.

www.it-ebooks.infoCHAPTER 28 • MYSQL STORAGE ENGINES AND DATA TYPESNOT NULLDefining a column as NOT NULL will disallow any attempt to insert a NULL value into the column. Usingthe NOT NULL attribute where relevant is always suggested as it results in at least baseline verification thatall necessary values have been passed to the query. An example of a NOT NULL column assignmentfollows:zipcode VARCHAR(10) NOT NULLNULLThe NULL attribute indicates that no value can exist for the given field. Keep in mind that NULL is amathematical term specifying “nothingness” rather than an empty string or zero. When a column isassigned the NULL attribute, it is possible for the field to remain empty regardless of whether the otherrow fields have been populated.The NULL attribute is assigned to a field by default. Typically, you will want to avoid this default,ensuring that empty values will not be accepted into the table. This is accomplished through NULL‘santithesis, NOT NULL, introduced above.PRIMARY KEYThe PRIMARY KEY attribute is used to guarantee uniqueness for a given row. No values residing in acolumn designated as a primary key are repeatable or nullable within that column. It’s quite common toassign the AUTO_INCREMENT attribute to a column designated as a primary key because this columndoesn’t necessarily have to bear any relation to the row data, other than acting as its unique identifier.However, there are two other ways to ensure a record’s uniqueness:• Single-field primary keys: Single-field primary keys are typically used when thereis a preexisting, nonmodifiable unique identifier for each row entered into thedatabase, such as a part number or Social Security number. Note that this keyshould never change once set.• Multiple-field primary keys: Multiple-field primary keys can be useful when it isnot possible to guarantee uniqueness from any single field within a record. Thus,multiple fields are conjoined to ensure uniqueness. When such a situation arises,it is often a good idea to simply designate an AUTO_INCREMENT integer as theprimary key; this alleviates the need to somehow generate unique identifiers withevery insertion.The following three examples demonstrate creation of the auto-increment, single-field, andmultiple-field primary key fields, respectively.Creating an automatically incrementing primary key:CREATE TABLE employees (id SMALLINT NOT NULL AUTO_INCREMENT,firstname VARCHAR(15) NOT NULL,lastname VARCHAR(25) NOT NULL,email VARCHAR(55) NOT NULL,PRIMARY KEY(id));545

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

Saved successfully!

Ooh no, something went wrong!