13.09.2016 Views

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

Create successful ePaper yourself

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

244 Chapter 10 Working with Your <strong>MySQL</strong> Database<br />

There’s an ANSI st<strong>and</strong>ard for SQL, <strong>and</strong> database systems such as <strong>MySQL</strong> generally<br />

strive to implement this st<strong>and</strong>ard.There are some subtle differences between st<strong>and</strong>ard<br />

SQL <strong>and</strong> <strong>MySQL</strong>’s SQL. Some of these differences are planned to become st<strong>and</strong>ard in<br />

future versions of <strong>MySQL</strong>, <strong>and</strong> some are deliberate differences.We point out the more<br />

important ones as we go. A complete list of the differences between <strong>MySQL</strong>’s SQL <strong>and</strong><br />

ANSI SQL in any given version can be found in the <strong>MySQL</strong> online manual.You can<br />

find this page at this URL <strong>and</strong> in many other locations http://dev.mysql.com/doc/<br />

refman/5.1/en/compatibility.html.<br />

You might have heard the terms Data Definition Language (DDL), used for defining<br />

databases, <strong>and</strong> Data Manipulation Language (DML), used for querying databases. SQL covers<br />

both of these bases. In Chapter 9, we looked at data definition (DDL) in SQL, so<br />

we’ve already been using it a little.You use DDL when you’re initially setting up a database.<br />

You will use the DML aspects of SQL far more frequently because these are the parts<br />

that you use to store <strong>and</strong> retrieve real data in a database.<br />

Inserting Data into the Database<br />

Before you can do a lot with a database, you need to store some data in it.The way you<br />

most commonly do this is to use the SQL INSERT statement.<br />

Recall that RDBMSs contain tables, which in turn contain rows of data organized<br />

into columns. Each row in a table normally describes some real-world object or relationship,<br />

<strong>and</strong> the column values for that row store information about the real-world object.<br />

You can use the INSERT statement to put rows of data into the database.<br />

The usual form of an INSERT statement is<br />

INSERT [INTO] table [(column1, column2, column3,...)] VALUES<br />

(value1, value2, value3,...);<br />

For example, to insert a record into Book-O-Rama’s customers table, you could type<br />

insert into customers values<br />

(NULL, 'Julie Smith', '25 Oak Street', 'Airport West');’’’<br />

You can see that we’ve replaced table with the name of the actual table where we want<br />

to put the data <strong>and</strong> the values with specific values.The values in this example are all<br />

enclosed in quotation marks. Strings should always be enclosed in pairs of single or double<br />

quotation marks in <strong>MySQL</strong>. (We use both in this book.) Numbers <strong>and</strong> dates do not<br />

need quotes.<br />

There are a few interesting things to note about the INSERT statement.The values<br />

specified here will be used to fill in the table columns in order. If you want to fill in only<br />

some of the columns, or if you want to specify them in a different order, you can list the<br />

specific columns in the columns part of the statement. For example,<br />

insert into customers (name, city) values<br />

(‘Melissa Jones’, ‘Nar Nar Goon North’);

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

Saved successfully!

Ooh no, something went wrong!