Database Reference
In-Depth Information
Figure 5.3
Error caused by duplicate primary key.
When you tried to add another row to the webpage table that had an ID of 6, MySQL real-
ized that the ID column is the primary key of the database and so wouldn't allow you to
insert a key with that value a second time.
INSERT - Columns from a Query
You can also insert rows that are output from another table. The command takes the fol-
lowing format:
INSERT INTO tablename
SELECT rows
FROM anothertablename
WHERE condition;
To demonstrate the above command, we will copy our table to another one. First we need
to create a new table with the same columns as the webpage table:
CREATE TABLE webpage2 (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Content text, Title text)
Now we will copy the data from webpage to webpage2 by executing the following:
INSERT INTO webpage2
SELECT * from webpage
Search WWH ::




Custom Search