Database Reference
In-Depth Information
Figure 5.2
The webpage table.
When you are inserting multiple rows into a database you place each row inside paren-
theses and separate rows with a comma. Select all of the rows from your webpage table
again. It should look like Figure 5.2.
INSERT - All Columns
An alternative form of the INSERT statement misses out the column name list as follows:
INSERT INTO tablename
VALUES (value, value, …);
In this case you must specify a value for each of the columns in the table in the VALUES
section, in the order which you originally created the table.
You have less control over the inserting of data using this method, and it can have prob-
lems associated with it. For example, if you have a column that is auto-generating
(AUTO_INCREMENT) to provide your row with a unique primary key, inserting a number
over this may well cause the table to have a duplicate primary key, which is not allowed. It
may work, but may lead to errors in the processing of your data later on. If we look at
Figure 5.2 again, we can see that we can safely add a row with the ID number 6 as there is
not another row with this ID. The following will add a row with that ID:
INSERT INTO webpage VALUES (6, “”,”How to Contact Me”)
This should have successfully added another row into your table. As a further demonstra-
tion of how hard-coding an auto-generating field can lead to errors, run the above query a
second time. Figure 5.3 shows the results of this.
Notice how the last line of Figure 5.3 has the error message:
Duplicate entry '6' for key 1.
Search WWH ::




Custom Search