Database Reference
In-Depth Information
When using the above command, the table that you are selecting from must have the
same number of columns as the table that you are inserting into. For instance, if you try the
following:
INSERT INTO webpage2
SELECT title FROM webpage
you will get the error:
Column count does not match value count at row 1.
To ensure that your columns match, you can specify a column list after the first table
name as follows:
INSERT INTO webpage2 (title, content)
SELECT title,content FROM webpage
As long as you have the same number of columns in the SELECT statement as you do in
the table you are inserting into, you can use just about any code for the SELECT statement.
The following will search for rows that have the word Home in the title field and insert them
into the webpage2 table:
INSERT INTO webpage2 (title, content)
SELECT title,content FROM webpage WHERE title = 'Home'
To view the contents of this new table, execute the following:
SELECT * FROM webpage2
Figure 5.4 shows the current content of our webpage2 table.
If you look at Figure 5.4, you will see that we have a unique primary key for each row of
the table. You can also see that the content column sometimes has NULL in it, where no data
was specified in the content string, an empty string as in row 6, and text data as in row 2.All
are equally valid entries. You can also see that in the title column we now have duplicate
page entries. This is acceptable in our demonstration database but would probably have lit-
tle use if we were going to use this table to power a website. We will remove this temporary
table in Chapter 9.
Inserting Data with Scripts
Another way to get data into our database is to run a script. A script is just a collection of
SQL queries separated by colons and stored in a text file.
Search WWH ::




Custom Search