Escolar Documentos
Profissional Documentos
Cultura Documentos
CREATE TABLE t2 (
key_1 INTEGER NOT NULL DEFAULT AUTOINCREMENT,
non_key_1 VARCHAR ( 100 ) NOT NULL DEFAULT 'xxx',
last_updated TIMESTAMP NOT NULL DEFAULT TIMESTAMP,
PRIMARY KEY ( key_1 ) );
INSERT t2
SELECT key_1, non_key_1, last_updated
FROM t1;
Since the two tables above have exactly the same number of columns in the
same order, the INSERT could be even simpler:
INSERT t2 SELECT * FROM t1;
Tip: This form of INSERT is very popular for loading data from external
sources via proxy tables (e.g., INSERT local_table SELECT * FROM proxy_table).
For more information about proxy tables, see Section 1.14, Remote Data
Access.
Heres the rule you must follow: The select must return the same number of col-
umns as exist in the target table, with the same or compatible data types in the
same order as they exist in the CREATE TABLE for the target table. In other
words, if the result set fits, it will be inserted.
As long as you follow that rule you can use all of the sophisticated features
described in Chapter 3, Selecting, when coding an INSERT. Heres an exam-
ple that uses a UNION to add two more rows to the ones selected from t1:
INSERT t2
SELECT 0, 'first', '2001-01-01'
UNION
SELECT * FROM t1
WHERE key_1 BETWEEN 1 AND 9998
UNION
SELECT 9999, 'last', CURRENT TIMESTAMP;