Update of "CloneTable"
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

Artifact ID: 4a796e8e2b7c8b6893758a8f4af9fd8ec43a0251
Page Name:CloneTable
Date: 2014-08-28 11:05:01
Original User: sandro
Parent: 0d3082e342c8d167c2615c5d03b8ffc6753a187f (diff)
Next b004a56dd683f09a2e949d0e781acc8eafee0fa6
Content

quick how-to guide: using CloneTable()

Starting since version 4.2.1 a new CloneTable() SQL function is now available; this new function is specifically intended to simplify copying data between different Tables and (may be) between different DB-files. Several advanced option intended to support flexible behaviors are supported, so reading this quick tutorial is strongly suggested in order to clarify any possible doubt about CloneTable().

downloading the sample DB

In this tutorial we'll use a sample DB available for download

exploring the sample DB

As you can easily check the sample DB contains the following Tables:

TableCreate StatementGeometry ColumnIndexTrigger
regions
CREATE TABLE regions (
    macro_region TEXT NOT NULL,
    nuts_1 TEXT NOT NULL,
    nuts_2 TEXT NOT NULL PRIMARY KEY,
    region TEXT NOT NULL, 
    pop_2011 INTEGER NOT NULL,
    pop_m_2011 INTEGER NOT NULL,
    pop_f_2011 INTEGER NOT NULL
);
nonenonenone
prov_itc
CREATE TABLE prov_itc (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    nuts_2 TEXT NOT NULL,
    nuts_3 TEXT NOT NULL,
    province TEXT NOT NULL,
    code TEXT NOT NULL,
    pop_2011 INTEGER NOT NULL,
    pop_m_2011 INTEGER NOT NULL,
    pop_f_2011 INTEGER NOT NULL);
geom

srid=32632
MULTIPOLYGON

no Spatial Index
nonenone
prov_itfsame as prov_itc
prov_itg
prov_ith
prov_iti
provinces
CREATE TABLE provinces (
    nuts_2 TEXT NOT NULL,
    nuts_3 TEXT NOT NULL PRIMARY KEY,
    province TEXT NOT NULL,
    code TEXT NOT NULL,
    pop_2011 INTEGER NOT NULL,
    pop_m_2011 INTEGER NOT NULL,
    pop_f_2011 INTEGER NOT NULL,
    CONSTRAINT fk_prov_reg 
        FOREIGN KEY (nuts_2) 
        REFERENCES regions (nuts_2));
geom

srid=32632
MULTIPOLYGON

Spatial Index
CREATE INDEX idx_prov_code 
    ON provinces (code);
CREATE INDEX idx_prov_name 
    ON provinces (name);
CREATE TRIGGER ins_prov 
    AFTER INSERT ON provinces
BEGIN
  UPDATE regions 
  SET pop_2011 = pop_2011 + NEW.pop_2011,
    pop_m_2011 = pop_m_2011 + NEW.pop_m_2011,
    pop_f_2011 = pop_f_2011 + NEW.pop_f_2011
  WHERE nuts_2 = NEW.nuts_2;
END;

The sample DB is fully based on the 2011 Italian Census kindly released by ISTAT under CC-BY license.

Just few keypoints worth to be highlighted:

Tutorial step #1 (basic level)

We'll use the sqlite3 CLI front-end tool in the following examples, but you could eventually use either spatialite or spatialite_gui.
$ sqlite3 test1.sqlite
SQLite version 3.8.6 2014-08-15 11:46:33
Enter ".help" for usage hints.
sqlite> SELECT load_extension('mod_spatialite');
sqlite>
we'll start by creating a new DB named test1.sqlite and by loading the mod_spatialite dynamic extension.
sqlite> SELECT InitSpatialMetadata(1);
1
sqlite>
now we'll properly initialize the new DB by creating any required matatable.
sqlite> ATTACH DATABASE "./clone_origin.sqlite" AS origin;
sqlite>
and finally we'll attach the clone_origin.sqlite sample DB: we are now ready for copying data between the two DB-files.
sqlite> SELECT CloneTable('origin', 'regions', 'regions', 1);
1
sqlite>
sqlite> SELECT * FROM regions;
as you can easily check a new regions table has been created (exactly corresponding to input definitions), and all rows have been copied.

Tutorial step #2 (advanced level)