Recette #7:
Insertion, Modification et Suppression

F�vrier 2011


Pr�c�dent

Table des mati�res

Suivant


Jusqu'� maintenant nous avons vu uniquement comment interroger une table.
SQL n'est pas un langage d�di� uniquement � l'extraction de donn�es:  il est possible d'ins�rer, modifier, supprimer des donn�es relativement facilement.

Il est maintenant temps d'examiner ces points en d�tail


CREATE TABLE test_geom (
  id INTEGER NOT NULL
    PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  measured_value DOUBLE NOT NULL);


SELECT AddGeometryColumn('test_geom', 'the_geom',
  4326, 'POINT', 'XY');

Rien de bien nouveau: il s'agit de la m�me table trait�e dans l'exemple pr�c�dent.

INSERT INTO test_geom
    (id, name, measured_value, the_geom)
  VALUES (NULL, 'first point', 1.23456,
    GeomFromText('POINT(1.01 2.02)', 4326));


INSERT INTO test_geom
  VALUES (NULL, 'second point', 2.34567,
    GeomFromText('POINT(2.02 3.03)', 4326));


INSERT INTO test_geom
    (id, name, measured_value, the_geom)
  VALUES (10, 'tenth point', 10.123456789,
    GeomFromText ('POINT(10.01 10.02)', 4326));


INSERT INTO test_geom
    (the_geom, measured_value, name, id)
  VALUES (GeomFromText('POINT(11.01 11.02)', 4326),
    11.123456789, 'eleventh point', NULL);


INSERT INTO test_geom
    (id, measured_value, the_geom, name)
  VALUES (NULL, 12.123456789, NULL, 'twelfth point');

La syntaxe INSERT INTO (...) VALUES (...) permet d'ins�rer des valeurs dans une table:

SELECT *
FROM test_geom;


id

name

measured_value

the_geom

1

first point

1.234560

BLOB sz=60 GEOMETRY

2

second point

2.345670

BLOB sz=60 GEOMETRY

10

tenth point

10.123457

BLOB sz=60 GEOMETRY

11

eleventh point

11.123457

BLOB sz=60 GEOMETRY

12

twelfth point

12.123457

NULL

Juste un petit coup d'oeil avant d'aller plus loin ...

INSERT INTO test_geom
  VALUES (2, 'POINT #2', 2.2,
    GeomFromText('POINT(2.22 3.33)', 4326));

cette requ�te INSERT va �chouer, aboutissant � une erreur: constraint failed.
Pourquoi? une PRIMARY KEY (cl� primaire) est obligatoirement unique.
or id = 2 existe d�j� dans cette table.

INSERT OR IGNORE INTO test_geom
  VALUES (2, 'POINT #2', 2.2,
    GeomFromText('POINT(2.22 3.33)', 4326));

En sp�cifiant la clause OR IGNORE la requ�te va maintenant �chouer en silence (pour les m�mes raisons).

INSERT OR REPLACE INTO test_geom
  VALUES (2, 'POINT #2', 2.2,
    GeomFromText('POINT(2.22 3.33)', 4326));

Il existe une variante i.e. sp�cifier la clause OR REPLACE va permettre de mettre � jour la colonne (un peu comme UPDATE).

REPLACE INTO test_geom
    (id, name, measured_value, the_geom)
  VALUES (3, 'POINT #3', 3.3,
    GeomFromText('POINT(3.33 4.44)', 4326));


REPLACE INTO test_geom
    (id, name, measured_value, the_geom)
  VALUES (11, 'POINT #11', 11.11,
    GeomFromText('POINT(11.33 11.44)', 4326));

Une autre alternative est possible, i.e. en utilisant REPLACE INTO:
mais il s'agit simplement d'un alias pour INSERT OR REPLACE.

SELECT *
FROM test_geom;


id

name

measured_value

the_geom

1

first point

1.234560

BLOB sz=60 GEOMETRY

2

POINT #2

2.200000

BLOB sz=60 GEOMETRY

3

POINT #3

3.300000

BLOB sz=60 GEOMETRY

10

tenth point

10.123457

BLOB sz=60 GEOMETRY

11

POINT #11

11.110000

BLOB sz=60 GEOMETRY

12

twelfth point

12.123457

NULL

Un autre petit coup d'oeil ...



UPDATE test_geom SET
  name = 'point-3',
  measured_value = 0.003
WHERE id = 3;


UPDATE test_geom SET
  measured_value = measured_value + 1000000.0
WHERE id > 10;

Mettre � jour des valeurs n'est pas plus complexe ...

DELETE FROM test_geom
WHERE (id % 2) = 0;

Il en va de m�me pour la suppression de lignes.
i.e. la clause DELETE va affecter toutes les lignes ayant un id pair.

SELECT *
FROM test_geom;


id

name

measured_value

the_geom

1

first point

1.234560

BLOB sz=60 GEOMETRY

3

point-3

0.003000

BLOB sz=60 GEOMETRY

11

POINT #11

1000011.110000

BLOB sz=60 GEOMETRY

Un dernier coup d'oeil ...



important

Attention: appeler les clauses UPDATE or DELETE sans sp�cifier de condition apr�s WHERE est tout � fait autoris� en SQL.
Dans ce cas, la requ�te va affecter toutes les lignes, sans distinction.
D�butants, attention � ne pas mettre le bazar par m�garde dans votre BDD!


Pr�c�dent

Table des mati�res

Suiavnt


Author: Alessandro Furieri a.furieri@lqt.it
Traduced from English by RIVIERE Romain

This work is licensed under the Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.


Permission is granted to copy, distribute and/or modify this document under the terms of the
GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.