April 06, 2019
There are a lot of times during development where we make a mistake and all or part of our data are gone. Even worse when we lose them due to a bug in our production version.
In this post, we’ll see how to create a database table as a backup from an existing one, copy the data, and make a massive update joining these two tables. We’ll quote the corresponding SQL statements from different RDBMSs: MariaDB, PostgreSQL, MS SQL Server, IBM DB2, SQLite.
CREATE TABLE tbl2 LIKE tbl1;
tbl2
table will inhert all the attributes from tbl1
.
INSERT INTO tbl2
SELECT * FROM tbl1;
We may use the WHERE
clause to insert only a part of tbl1’s data.
UPDATE tbl1 a
INNER JOIN tbl2 b ON
a.key = b.key
SET a.column1 = b.column1;
CREATE TABLE tbl2 ( LIKE tbl1 INCLUDING ALL );
INSERT INTO tbl2
SELECT * FROM tbl1;
UPDATE tbl1 a
SET a.column1 = b.column1
FROM tbl2 b
WHERE
a.key = b.key;
SELECT * INTO tbl2
FROM tbl1
SELECT * INTO tbl2
FROM tbl1
WHERE 1 = 0;
UPDATE a
SET a.column1 = b.column1
FROM tbl1 a
INNER JOIN tbl2 b ON
a.key = b.key;
CREATE TABLE tbl2 LIKE tbl1;
INSERT INTO tbl2
SELECT * FROM tbl1;
UPDATE tbl1 a
SET a.column1 = b.column1
FROM tbl2 b
WHERE
a.key = b.key;
CREATE TABLE tbl2 AS
SELECT * FROM tbl1
WHERE 1 = 0;
INSERT INTO tbl2
SELECT * FROM tbl1;
CREATE TABLE tbl2 AS
SELECT * FROM tbl1;
UPDATE tbl1
SET
column1 = (SELECT b.column1
FROM tbl2 b
WHERE b.key = tbl1.key)
WHERE tbl1.key IN (SELECT b.key FROM tbl2 b);
In SQLite we may take advantage of the Row Values Method, in the case we want to use more than one key:
UPDATE tbl1
SET
column1 = (SELECT b.column1
FROM tbl2 b
WHERE (b.key1, b.key2) = (tbl1.key1, tbl1.key2) )
WHERE (tbl1.key1, tbl1.key2) IN (
SELECT b.key1, b.key2 FROM tbl2 b);
Personal blog by Theoklitos Bampouris who lives in Athens (Greece), working as a Software Developer. You should follow him on Twitter