This can be done using the syntax * without having to know the table schema (except for the primary key name). The trick is to create a temporary table using the syntax "CREATE TABLE AS".
In this example, I assume that there is an existing, populated table called "src", with the key INTEGER PRIMARY KEY called "id", as well as several other columns. To duplicate the src lines, use the following SQL in SQLite3:
CREATE TEMPORARY TABLE tmp AS SELECT * FROM src; UPDATE tmp SET id = NULL; INSERT INTO src SELECT * FROM tmp; DROP TABLE tmp;
In the above example, all rows of the src table are duplicated. To only duplicate the desired line, simply add the WHERE clause to the first line. This example works because the table "tmp" does not have a primary key constraint, but "src" does. Inserting primary NULL keys into src forces them to receive automatically generated values.
From sqlite documentation: http://www.sqlite.org/lang_createtable.html
The CREATE TABLE ... AS SELECT statement creates and populates a database table based on the results of a SELECT statement. A table created using CREATE TABLE AS does not have a PRIMARY KEY and no restrictions of any type.
If you want real fantasy, you can add a trigger that updates the third table, which displays the old primary keys for the newly created primary keys.
Seth green
source share