13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

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

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

Inserting Data into the Database<br />

245<br />

This approach is useful if you have only partial data about a particular record or if some<br />

fields in the record are optional.You can also achieve the same effect with the following<br />

syntax:<br />

insert into customers<br />

set name = ’Michael Archer’,<br />

address = ’12 Adderley Avenue’,<br />

city = ’Leeton’;<br />

Also notice that we specified a NULL value for the customerid column when adding<br />

Julie Smith <strong>and</strong> ignored that column when adding the other customers.You might recall<br />

that when you set up the database, you created customerid as the primary key for the<br />

customers table, so this might seem strange. However, you specified the field as<br />

AUTO_INCREMENT.This means that, if you insert a row with a NULL value or no value in<br />

this field, <strong>MySQL</strong> will generate the next number in the auto increment sequence <strong>and</strong><br />

insert it for you automatically.This behavior is pretty useful.<br />

You can also insert multiple rows into a table at once. Each row should be in its own<br />

set of parentheses, <strong>and</strong> each set of parentheses should be separated by a comma.<br />

Only a few other variants are possible with INSERT. After the word INSERT, you can<br />

add LOW_PRIORITY or DELAYED.The LOW_PRIORITY keyword means the system may wait<br />

<strong>and</strong> insert later when data is not being read from the table.The DELAYED keyword means<br />

that your inserted data will be buffered. If the server is busy, you can continue running<br />

queries rather than having to wait for this INSERT operation to complete.<br />

Immediately after this, you can optionally specify IGNORE.This means that if you try<br />

to insert any rows that would cause a duplicate unique key, they will be silently ignored.<br />

Another alternative is to specify ON DUPLICATE KEY UPDATE expression at the end of<br />

the INSERT statement.This can be used to change the duplicate value using a normal<br />

UPDATE statement (covered later in this chapter).<br />

We’ve put together some simple sample data to populate the database.This is just a<br />

series of simple INSERT statements that use the multirow insertion approach.You can find<br />

the script that does this in the file \chapter10\book_insert.sql on the CD accompanying<br />

this book. It is also shown in Listing 10.1.<br />

Listing 10.1<br />

use books;<br />

book_insert.sql—SQL to Populate the Tables for Book-O-Rama<br />

insert into customers values<br />

(3, ‘Julie Smith’, ‘25 Oak Street’, ‘Airport West’),<br />

(4, ‘Alan Wong’, ‘1/47 Haines Avenue’, ‘Box Hill’),<br />

(5, ‘Michelle Arthur’, ‘357 North Road’, ‘Yarraville’);<br />

insert into orders values<br />

(NULL, 3, 69.98, ‘2007-04-02’),<br />

(NULL, 1, 49.99, ‘2007-04-15’),

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

Saved successfully!

Ooh no, something went wrong!