Changes

Inserting Data into a MySQL Database

63 bytes added, 14:29, 19 May 2009
no edit summary
</table>
<hr>
 
 
<google>BUY_MYSQL</google>
 
A database would be of little use if it did not contain any data. It is, therefore, not surprising that the ''INSERT'' statement is one of the most commonly used statements.
Note that the product_id is specified as NULL. This is because the product_id in our imaginary table is set to AUTO_INCREMENT so we do not specifically set this. Instead, the MySQL database engine will automatically create a new value for us.
A less safe way to add records to a table is to provide only the values. Whilst this approach works it is vital that the values be specified in the ''exact order'' in which the columns were specified when the table was created. Getting the wrong order will, at the very least result in a an error message (if the data types do not match the columns) and at the worst result in data going into the wrong columns. The other danger of this approach is that the statement may work based on the current table layout but will cause problems should the layout be altered at a later date. With these warnings in mind, here is an example of specifying just the values:
<pre>
== Inserting Results from a SELECT Statement ==
A particularly useful SQL statement involves combining the INPUT and SELECT statements. In this scenario, the SELECT statement reads the values (typically from another table) to be added to the current table. This approach is known as ''INSERT SELECT''. and can be used to perform tasks such as added adding all the rows from one table to another table with the same column layout. The following example uses a ''SELECT'' statement to retrieve the rows in a table called ''old_products'' and adds them to a table called ''new_products'':
<pre>
== Reducing the INSERT Performance Load ==
The INSERT statement places a relatively high load on the database server. In fact a high volume of insertions can have an adverse effect on other transactions (such as reading data). To reduce the load imposed by a data insertion , consider using the ''LOW_PRIORITY'' keyword. For example:
<pre>
</pre>
<google>BUY_MYSQL_BOTTOM</google>
<hr>