I'm new to SQL, but I've always heard that it's a very efficient way of searching databases. For the same task, in VBA I get a result in a few seconds for 1000 queries. In SQL it takes several minutes.
I've tried going back and forth between my databases and my code to try and improve the execution speed of my triggers, without success.
Here's how my project looks (I work on QGIS, with SpatiaLite (SQLite) layers):
I have SpatiaLite layers :
ESPECES_PT (point geometry layer) [Important fields : ESPECE ; COMPART ; REGION]
ESPECES_ATTRIBUTS (no geometry) [Important fields : ESPECE ; COMPART ; REGION]
When a name is entered in "ESPECE", the table ESPECES_ATTRIBUTS is updated with the same "ESPECE", "REGION" and "COMPART" (if it does not already exist) and triggers are executed to search in several databases, a list of information. *I use 2 layers because the 1rst one can contain multiple identical names (not the second one)
There are 2 kinds of databases
TAXREF_
BDC_
All these databases are fragmented into 7 "compartments" [TAXREF_FLORE ; TAXREF_ENTOMO ; TAXREF_MAMMIFERES ; etc.] [BDC_FLORE ; BDC_ENTOMO ; BDC_MAMMIFERES ; etc.]
According to the contents of REGION and COMPART, the value of ESPECE will be searched in different databases. [For example: If COMPART = 'Flore' and REGION = 'Occitanie' then ESPECE will be searched into TAXREF_FLORE and in BDC_FLORE (in BDC_, the query will then look for "REGION" to get the correspondances)
The picture after shows (with the utmost clarity) how it work:
[On table ESPECES_PT : ESPECE = 'Ophrys provincialis' ; REGION = 'Occitanie' ; COMPART = 'Flore'] [On table ESPECES_ATTRIBUTS : ESPECE = 'Ophrys provincialis' ; REGION = 'Occitanie' ; COMPART = 'Flore' ; NOM_SCIENT_VALIDE is updated to the current valid name]
[On ESPECES_ATTRIBUTS : the query is performed from TAXREF_FLORE AND BDC_FLORE where several data are retrieved (in yellow) according to the value in NOM_VALIDE (table TAXREF) and REGION (table BDC).
This process is quite long and I'm wondering if you would have some tips and improvement to propose in order to improve it.
Here are the different important triggers :
When I add features on table ESPECES_PT (more or less the same for updates)
CREATE TRIGGER A_insert_esp
AFTER INSERT ON ESPECES_PT
BEGIN
UPDATE ESPECES_PT
SET "COMPART" = COALESCE(ESPECES_PT.COMPART, (SELECT "COMPART" FROM ZONE_ETUDE_POLY), '[Voir Table ESPECES_ATTRIBUTS]'),
"REGION" = COALESCE(ESPECES_PT.REGION, (SELECT "REGION" FROM ZONE_ETUDE_POLY), '[Voir Table ESPECES_ATTRIBUTS]'),
"DATA_PROP" = (SELECT "Company" FROM C_copyright)
WHERE pkuid = NEW.pkuid;
INSERT INTO ESPECES_ATTRIBUTS (ESPECE, Visibilite, ENJEU_SPE, IMPORT_ZE, ENJEU_ZE, COMPART, REGION)
SELECT DISTINCT
NEW.ESPECE,
NEW.Visibilite,
CASE
WHEN NEW.ENJEU_SPE LIKE '%1%' THEN 'Très fort'
WHEN NEW.ENJEU_SPE LIKE '%2%' THEN 'Fort'
WHEN NEW.ENJEU_SPE LIKE '%3%' THEN 'Modéré'
WHEN NEW.ENJEU_SPE LIKE '%4%' THEN 'Faible'
WHEN NEW.ENJEU_SPE LIKE '%5%' THEN 'Très faible'
WHEN NEW.ENJEU_SPE LIKE '%6%' THEN NULL
ELSE coalesce(NEW.ENJEU_SPE <> '[Voir Table ESPECES_ATTRIBUTS]',NULL)
END,
CASE
WHEN NEW.IMPORT_ZE LIKE '%1%' THEN 'Très fort'
WHEN NEW.IMPORT_ZE LIKE '%2%' THEN 'Fort'
WHEN NEW.IMPORT_ZE LIKE '%3%' THEN 'Modéré'
WHEN NEW.IMPORT_ZE LIKE '%4%' THEN 'Faible'
WHEN NEW.IMPORT_ZE LIKE '%5%' THEN 'Très faible'
WHEN NEW.IMPORT_ZE LIKE '%6%' THEN NULL
ELSE coalesce(NEW.IMPORT_ZE <> '[Voir Table ESPECES_ATTRIBUTS]',NULL)
END,
CASE
WHEN NEW.ENJEU_ZE LIKE '%1%' THEN 'Très fort'
WHEN NEW.ENJEU_ZE LIKE '%2%' THEN 'Fort'
WHEN NEW.ENJEU_ZE LIKE '%3%' THEN 'Modéré'
WHEN NEW.ENJEU_ZE LIKE '%4%' THEN 'Faible'
WHEN NEW.ENJEU_ZE LIKE '%5%' THEN 'Très faible'
WHEN NEW.ENJEU_ZE LIKE '%6%' THEN NULL
ELSE coalesce(NEW.ENJEU_ZE <> '[Voir Table ESPECES_ATTRIBUTS]',NULL)
END,
CASE
WHEN ESPECES_PT.COMPART <> '[Voir Table ESPECES_ATTRIBUTS]' AND ESPECES_PT.COMPART IS NOT NULL THEN ESPECES_PT.COMPART
END,
CASE
WHEN ESPECES_PT.REGION <> '[Voir Table ESPECES_ATTRIBUTS]' AND ESPECES_PT.REGION IS NOT NULL THEN ESPECES_PT.REGION
END
FROM ESPECES_PT
WHERE ESPECES_PT.pkuid = NEW.pkuid
AND NEW.ESPECE IS NOT NULL
AND NEW.ESPECE NOT IN (SELECT ESPECE FROM ESPECES_ATTRIBUTS);
UPDATE ESPECES_ATTRIBUTS
SET NOM_SIMPLE = (
WITH RECURSIVE split(value, str) AS (
SELECT null, NEW.ESPECE || ' ' FROM ESPECES_ATTRIBUTS WHERE ESPECES_ATTRIBUTS.rowid = last_insert_rowid()
UNION ALL
SELECT substr(str, 0, instr(str, ' ')), substr(str, instr(str, ' ')+1) FROM split WHERE str != ''
)
SELECT
CASE
WHEN NEW.ESPECE LIKE '%var.%' OR NEW.ESPECE LIKE '%subsp.%' THEN
COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1), '')
|| COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 1), '' )
|| COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 2), '')
|| TRIM(COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 3), ''))
ELSE
COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1), '')
|| TRIM(COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 1), '' ))
END
)
WHERE ESPECES_ATTRIBUTS.rowid = last_insert_rowid();
UPDATE ESPECES_ATTRIBUTS
SET NOM_SCIENT_VALIDE =
CASE
WHEN ESPECES_ATTRIBUTS.COMPART = 'Flore' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_FLORE WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_FLORE WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_FLORE WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_FLORE WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Amphibiens' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_AMPHIBIENS WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_AMPHIBIENS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_AMPHIBIENS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_AMPHIBIENS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Arthropodes' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_ENTOMO WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_ENTOMO WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_ENTOMO WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_ENTOMO WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Avifaune' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_OISEAUX WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_OISEAUX WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_OISEAUX WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_OISEAUX WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Mammifères' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_MAMMIFERES WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_MAMMIFERES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_MAMMIFERES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_MAMMIFERES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Poissons' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_POISSONS WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_POISSONS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_POISSONS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_POISSONS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Reptiles' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_REPTILES WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_REPTILES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_REPTILES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_REPTILES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
ELSE 'Nouveau compartiment'
END
WHERE ESPECES_ATTRIBUTS.rowid = last_insert_rowid();
UPDATE ESPECES_ATTRIBUTS
SET "CHOIX_ESPECE" = 'Voir liste'
WHERE ESPECES_ATTRIBUTS.rowid = last_insert_rowid() AND NOM_SCIENT_VALIDE = '(Synonymes)';
INSERT INTO SYNONYMES (ID, ESPECE, NOM_SCIENT_VALIDE)
SELECT DISTINCT
EA.ID,
EA.ESPECE,
T.NOM_VALIDE
FROM ESPECES_ATTRIBUTS EA
JOIN TAXREF_FLORE T ON T.LB_NOM = EA.NOM_SIMPLE
WHERE EA.ID = last_insert_rowid()
AND EA.NOM_SCIENT_VALIDE = '(Synonymes)'
AND EA.COMPART IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM SYNONYMES S
WHERE S.ESPECE = EA.ESPECE
AND S.NOM_SCIENT_VALIDE = T.NOM_VALIDE
)
AND (
(EA.COMPART = 'Flore' AND EXISTS (SELECT 1 FROM TAXREF_FLORE WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Amphibiens' AND EXISTS (SELECT 1 FROM TAXREF_AMPHIBIENS WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Arthropodes' AND EXISTS (SELECT 1 FROM TAXREF_ENTOMO WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Avifaune' AND EXISTS (SELECT 1 FROM TAXREF_OISEAUX WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Mammifères' AND EXISTS (SELECT 1 FROM TAXREF_MAMMIFERES WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Poissons' AND EXISTS (SELECT 1 FROM TAXREF_POISSONS WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Reptiles' AND EXISTS (SELECT 1 FROM TAXREF_REPTILES WHERE LB_NOM = EA.NOM_SIMPLE))
);
END
The code to update "ESPECE" into "NOM_SCIENT_VALIDE" (in table ESPECES_ATTRIBUTS)
CREATE TRIGGER B_update_nom_scient
AFTER UPDATE OF COMPART, REGION, ESPECE ON ESPECES_ATTRIBUTS
FOR EACH ROW
BEGIN
UPDATE ESPECES_ATTRIBUTS
SET NOM_SIMPLE = (
WITH RECURSIVE split(value, str) AS (
SELECT null, NEW.ESPECE || ' '
FROM ESPECES_ATTRIBUTS
WHERE ID = NEW.ID
UNION ALL
SELECT substr(str, 0, instr(str, ' ')), substr(str, instr(str, ' ')+1)
FROM split
WHERE str != ''
)
SELECT
CASE
WHEN NEW.ESPECE LIKE '%var.%' OR NEW.ESPECE LIKE '%subsp.%' THEN
COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1), '') ||
COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 1), '' ) ||
COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 2), '') ||
TRIM(COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 3), ''))
ELSE
COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1), '') ||
TRIM(COALESCE((SELECT value || ' ' FROM split WHERE value IS NOT NULL LIMIT 1 OFFSET 1), '' ))
END
)
WHERE ID = NEW.ID;
UPDATE ESPECES_ATTRIBUTS
SET NOM_SCIENT_VALIDE =
CASE
WHEN ESPECES_ATTRIBUTS.COMPART = 'Flore' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_FLORE WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_FLORE WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_FLORE WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_FLORE WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Amphibiens' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_AMPHIBIENS WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_AMPHIBIENS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_AMPHIBIENS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_AMPHIBIENS WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Entomofaune' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_ENTOMO WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_ENTOMO WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_ENTOMO WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_ENTOMO WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Oiseaux' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_OISEAUX WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_OISEAUX WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_OISEAUX WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_OISEAUX WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Mammifères' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_MAMMIFERES WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_MAMMIFERES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_MAMMIFERES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_MAMMIFERES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
WHEN ESPECES_ATTRIBUTS.COMPART = 'Reptiles' THEN
COALESCE(
(SELECT "NOM_VALIDE" FROM TAXREF_REPTILES WHERE "NOM_VALIDE_SIMPLE" = ESPECES_ATTRIBUTS.NOM_SIMPLE),
CASE
WHEN (SELECT COUNT(*) FROM TAXREF_REPTILES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 1 THEN (SELECT "NOM_VALIDE" FROM TAXREF_REPTILES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE)
WHEN (SELECT COUNT(*) FROM TAXREF_REPTILES WHERE "LB_NOM" = ESPECES_ATTRIBUTS.NOM_SIMPLE) = 0 THEN 'Absent'
ELSE
'(Synonymes)'
END)
ELSE NULL
END
WHERE ID = NEW.ID AND ESPECES_ATTRIBUTS.COMPART is not NULL;
END;
The code for searching the databases
CREATE TRIGGER B_update_spe_stat
AFTER UPDATE OF NOM_SCIENT_VALIDE ON ESPECES_ATTRIBUTS
FOR EACH ROW
BEGIN
UPDATE ESPECES_ATTRIBUTS
SET "CHOIX_ESPECE" = 'Voir liste'
WHERE ID = NEW.ID AND NOM_SCIENT_VALIDE = '(Synonymes)';
UPDATE ESPECES_ATTRIBUTS
SET
NOM_VERN =
CASE
WHEN "COMPART" = 'Flore' THEN (SELECT "NOM_VERN" FROM TAXREF_FLORE WHERE "NOM_VALIDE" = ESPECES_ATTRIBUTS.NOM_SCIENT_VALIDE)
WHEN "COMPART" = 'Amphibiens' THEN (SELECT "NOM_VERN" FROM TAXREF_AMPHIBIENS WHERE "NOM_VALIDE" = ESPECES_ATTRIBUTS.NOM_SCIENT_VALIDE)
WHEN "COMPART" = 'Entomofaune' THEN (SELECT "NOM_VERN" FROM TAXREF_ENTOMO WHERE "NOM_VALIDE" = ESPECES_ATTRIBUTS.NOM_SCIENT_VALIDE)
WHEN "COMPART" = 'Oiseaux' THEN (SELECT "NOM_VERN" FROM TAXREF_OISEAUX WHERE "NOM_VALIDE" = ESPECES_ATTRIBUTS.NOM_SCIENT_VALIDE)
WHEN "COMPART" = 'Mammifères' THEN (SELECT "NOM_VERN" FROM TAXREF_MAMMIFERES WHERE "NOM_VALIDE" = ESPECES_ATTRIBUTS.NOM_SCIENT_VALIDE)
WHEN "COMPART" = 'Reptiles' THEN (SELECT "NOM_VERN" FROM TAXREF_REPTILES WHERE "NOM_VALIDE" = ESPECES_ATTRIBUTS.NOM_SCIENT_VALIDE)
END,
(CHOROLOGIE, FLORAISON, HABIT_S, CARAC_ECO, INDIC_PHYTO, TYPE_BIOLOGIQUE, FORMATION_VEGETALE, ENJEU_paca, ZH, MESSICOLE) =
(SELECT
"CHOROLOGIE",
"floraison",
"HABITAT SIMPLE",
"CARACTERISATION_ECOLOGIQUE_(HABITAT_OPTIMAL)",
"INDICATION_PHYTOSOCIOLOGIQUE_CARACTERISTIQUE",
"TYPE_BIOLOGIQUE",
"FORMATION_VEGETALE",
"ENJEU PACA",
"Statut ZH",
"Statut messicole"
FROM TAXREF_FLORE
WHERE "COMPART" = 'Flore' AND "NOM_VALIDE" = ESPECES_ATTRIBUTS.NOM_SCIENT_VALIDE)
WHERE ID = NEW.ID;
INSERT INTO SYNONYMES (ID, ESPECE, NOM_SCIENT_VALIDE)
SELECT DISTINCT
EA.ID,
EA.ESPECE,
T.NOM_VALIDE
FROM
ESPECES_ATTRIBUTS EA
JOIN
TAXREF_FLORE T ON T.LB_NOM = EA.NOM_SIMPLE
WHERE
EA.ID = NEW.ID
AND EA.NOM_SCIENT_VALIDE = '(Synonymes)'
AND NOT EXISTS (
SELECT 1
FROM SYNONYMES S
WHERE S.ESPECE = EA.ESPECE AND S.NOM_SCIENT_VALIDE = T.NOM_VALIDE
)
AND (
(EA.COMPART = 'Flore' AND EXISTS (SELECT 1 FROM TAXREF_FLORE WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Amphibiens' AND EXISTS (SELECT 1 FROM TAXREF_AMPHIBIENS WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Arthropodes' AND EXISTS (SELECT 1 FROM TAXREF_ENTOMO WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Avifaune' AND EXISTS (SELECT 1 FROM TAXREF_OISEAUX WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Mammifères' AND EXISTS (SELECT 1 FROM TAXREF_MAMMIFERES WHERE LB_NOM = EA.NOM_SIMPLE))
OR (EA.COMPART = 'Reptiles' AND EXISTS (SELECT 1 FROM TAXREF_REPTILES WHERE LB_NOM = EA.NOM_SIMPLE))
);
UPDATE ESPECES_ATTRIBUTS
SET PN =
CASE
WHEN COMPART = 'Flore' THEN (SELECT CODE_STATUT FROM BDC_FLORE WHERE NOM_VALIDE_COMPLET = NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'PN')
WHEN COMPART = 'Amphibiens' THEN (SELECT CODE_STATUT FROM BDC_AMPHIBIENS WHERE NOM_VALIDE_COMPLET = NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'PN')
WHEN COMPART = 'Entomofaune' THEN (SELECT CODE_STATUT FROM BDC_ENTOMO WHERE NOM_VALIDE_COMPLET = NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'PN')
WHEN COMPART = 'Oiseaux' THEN (SELECT CODE_STATUT FROM BDC_OISEAUX WHERE NOM_VALIDE_COMPLET = NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'PN')
WHEN COMPART = 'Mammifères' THEN (SELECT CODE_STATUT FROM BDC_MAMMIFERES WHERE NOM_VALIDE_COMPLET = NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'PN')
WHEN COMPART = 'Reptiles' THEN (SELECT CODE_STATUT FROM BDC_REPTILES WHERE NOM_VALIDE_COMPLET = NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'PN')
ELSE NULL
END,
PR =
CASE
WHEN COMPART = 'Flore' THEN (SELECT CODE_STATUT FROM BDC_FLORE WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'PR')
WHEN COMPART = 'Amphibiens' THEN (SELECT CODE_STATUT FROM BDC_AMPHIBIENS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'PR')
WHEN COMPART = 'Entomofaune' THEN (SELECT CODE_STATUT FROM BDC_ENTOMO WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'PR')
WHEN COMPART = 'Oiseaux' THEN (SELECT CODE_STATUT FROM BDC_OISEAUX WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'PR')
WHEN COMPART = 'Mammifères' THEN (SELECT CODE_STATUT FROM BDC_MAMMIFERES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'PR')
WHEN COMPART = 'Poissons' THEN (SELECT CODE_STATUT FROM BDC_POISSONS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'PR')
WHEN COMPART = 'Reptiles' THEN (SELECT CODE_STATUT FROM BDC_REPTILES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'PR')
ELSE NULL
END,
ListeRN =
CASE
WHEN COMPART = 'Flore' THEN (SELECT CODE_STATUT FROM BDC_FLORE WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'LRN')
WHEN COMPART = 'Amphibiens' THEN (SELECT CODE_STATUT FROM BDC_AMPHIBIENS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'LRN')
WHEN COMPART = 'Entomofaune' THEN (SELECT CODE_STATUT FROM BDC_ENTOMO WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'LRN')
WHEN COMPART = 'Oiseaux' THEN (SELECT CODE_STATUT FROM BDC_OISEAUX WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'LRN')
WHEN COMPART = 'Mammifères' THEN (SELECT CODE_STATUT FROM BDC_MAMMIFERES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'LRN')
WHEN COMPART = 'Poissons' THEN (SELECT CODE_STATUT FROM BDC_POISSONS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'LRN')
WHEN COMPART = 'Reptiles' THEN (SELECT CODE_STATUT FROM BDC_REPTILES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CD_TYPE_STATUT = 'LRN')
ELSE NULL
END,
ListeRR =
CASE
WHEN COMPART = 'Flore' THEN (SELECT CODE_STATUT FROM BDC_FLORE WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'LRR')
WHEN COMPART = 'Amphibiens' THEN (SELECT CODE_STATUT FROM BDC_AMPHIBIENS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'LRR')
WHEN COMPART = 'Entomofaune' THEN (SELECT CODE_STATUT FROM BDC_ENTOMO WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'LRR')
WHEN COMPART = 'Oiseaux' THEN (SELECT CODE_STATUT FROM BDC_OISEAUX WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'LRR')
WHEN COMPART = 'Mammifères' THEN (SELECT CODE_STATUT FROM BDC_MAMMIFERES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'LRR')
WHEN COMPART = 'Poissons' THEN (SELECT CODE_STATUT FROM BDC_POISSONS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'LRR')
WHEN COMPART = 'Reptiles' THEN (SELECT CODE_STATUT FROM BDC_REPTILES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'LRR')
ELSE NULL
END,
ZNIEFF =
CASE
WHEN COMPART = 'Flore' THEN 'Déterminante ZNIEFF - ' || (SELECT CORRESPONDANCE_REGION FROM BDC_FLORE WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'ZDET')
WHEN COMPART = 'Amphibiens' THEN 'Déterminante ZNIEFF - ' || (SELECT CORRESPONDANCE_REGION FROM BDC_AMPHIBIENS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'ZDET')
WHEN COMPART = 'Entomofaune' THEN 'Déterminante ZNIEFF - ' || (SELECT CORRESPONDANCE_REGION FROM BDC_ENTOMO WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'ZDET')
WHEN COMPART = 'Oiseaux' THEN 'Déterminante ZNIEFF - ' || (SELECT CORRESPONDANCE_REGION FROM BDC_OISEAUX WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'ZDET')
WHEN COMPART = 'Mammifères' THEN 'Déterminante ZNIEFF - ' || (SELECT CORRESPONDANCE_REGION FROM BDC_MAMMIFERES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'ZDET')
WHEN COMPART = 'Poissons' THEN 'Déterminante ZNIEFF - ' || (SELECT CORRESPONDANCE_REGION FROM BDC_POISSONS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'ZDET')
WHEN COMPART = 'Reptiles' THEN 'Déterminante ZNIEFF - ' || (SELECT CORRESPONDANCE_REGION FROM BDC_REPTILES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND CD_TYPE_STATUT = 'ZDET')
ELSE NULL
END,
EXO_INV =
CASE
WHEN COMPART = 'Flore' THEN (SELECT STATUT_EXOTIQUE FROM BDC_FLORE WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION)
WHEN COMPART = 'Amphibiens' THEN (SELECT CD_TYPE_STATUT FROM BDC_AMPHIBIENS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND (CD_TYPE_STATUT = 'REGLII' OR CD_TYPE_STATUT = 'REGLLUTTE'))
WHEN COMPART = 'Entomofaune' THEN (SELECT CD_TYPE_STATUT FROM BDC_ENTOMO WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND (CD_TYPE_STATUT = 'REGLII' OR CD_TYPE_STATUT = 'REGLLUTTE'))
WHEN COMPART = 'Oiseaux' THEN (SELECT CD_TYPE_STATUT FROM BDC_OISEAUX WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND (CD_TYPE_STATUT = 'REGLII' OR CD_TYPE_STATUT = 'REGLLUTTE'))
WHEN COMPART = 'Mammifères' THEN (SELECT CD_TYPE_STATUT FROM BDC_MAMMIFERES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND (CD_TYPE_STATUT = 'REGLII' OR CD_TYPE_STATUT = 'REGLLUTTE'))
WHEN COMPART = 'Poissons' THEN (SELECT CD_TYPE_STATUT FROM BDC_POISSONS WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND (CD_TYPE_STATUT = 'REGLII' OR CD_TYPE_STATUT = 'REGLLUTTE'))
WHEN COMPART = 'Reptiles' THEN (SELECT CD_TYPE_STATUT FROM BDC_REPTILES WHERE NOM_VALIDE_COMPLET = NEW.NOM_SCIENT_VALIDE AND CORRESPONDANCE_REGION = NEW.REGION AND (CD_TYPE_STATUT = 'REGLII' OR CD_TYPE_STATUT = 'REGLLUTTE'))
ELSE NULL
END
WHERE ESPECES_ATTRIBUTS.ID = NEW.ID;
END;
EDIT1: EXPLAIN QUERY PLAN
To reply to the last message:
EXPLAIN SELECT * FROM ESPECES_PT WHERE ESPECE = 'Ophrys provincialis';
EXPLAIN SELECT * FROM ESPECES_ATTRIBUTS WHERE ESPECE = 'Ophrys provincialis';
Returns
EXPLAIN QUERY PLAN SELECT * FROM ESPECES_PT WHERE ESPECE = 'Ophrys provincialis';