Changes

Inserting Data into a MySQL Database

834 bytes added, 13:46, 3 October 2007
Inserting a Complete Row
Note that the product_id is specified as NULL. This is because the product_id in our imaginmary 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 porblems should the layout be altered at a later date. With these warnings in mind, here is an example of specifying just the values: a comma separated list of column names enclosed in parentheses. This is followed by the ''VALUES'' keyword and then a comman separated list of values for each column. Note that the values must be listed in the same order as the column names. For example: <pre>INSERT INTO productsVALUES( NULL, 'CD-RW Model 4543', 'CD Writer', 'Shelf 4B', 10};</pre> == Adding Multiple Rows to a Table == Adding multiple rows to a table can be achieved either using multiple ''INSERT'' statements or by submitting all the rows as part of a single ''INSERT'' statement.