75 lines
2.3 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ============================================================
-- 03_checks.sql — Contraintes d'intégrité métier
-- ============================================================
-- ===========================================
-- UTILISATEURS
-- ===========================================
-- Email déjà couvert par UNIQUE + regex dans le schéma
-- Vérifier que mobile et téléphone fixe ne sont pas identiques
ALTER TABLE utilisateurs
ADD CONSTRAINT chk_telephone_diff CHECK (
mobile IS NULL OR telephone_fixe IS NULL OR mobile <> telephone_fixe
);
-- ===========================================
-- ENFANTS
-- ===========================================
-- Si lenfant est "a_naitre", la date_prevue_naissance doit être renseignée
ALTER TABLE enfants
ADD CONSTRAINT chk_enfant_date_prevue
CHECK (
(statut = 'a_naitre' AND date_prevue_naissance IS NOT NULL)
OR (statut <> 'a_naitre')
);
-- Si lenfant est déjà né, sa date_naissance doit être renseignée
ALTER TABLE enfants
ADD CONSTRAINT chk_enfant_date_naissance
CHECK (
(statut IN ('actif','scolarise') AND date_naissance IS NOT NULL)
OR (statut = 'a_naitre')
);
-- ===========================================
-- DOSSIERS
-- ===========================================
-- Budget positif si renseigné
ALTER TABLE dossiers
ADD CONSTRAINT chk_dossier_budget_pos CHECK (budget IS NULL OR budget > 0);
-- ===========================================
-- CONTRATS
-- ===========================================
-- Cohérence des dates : date_debut obligatoire pour un contrat signé
ALTER TABLE contrats
ADD CONSTRAINT chk_contrat_date_debut
CHECK (
(statut = 'brouillon' OR date_debut IS NOT NULL)
);
-- ===========================================
-- EVENEMENTS
-- ===========================================
-- Cohérence des dates : début < fin
ALTER TABLE evenements
ADD CONSTRAINT chk_evenement_dates
CHECK (date_fin IS NULL OR date_debut <= date_fin);
-- ===========================================
-- VALIDATIONS
-- ===========================================
-- Si statut = 'valide', alors modifie_le doit être >= cree_le
ALTER TABLE validations
ADD CONSTRAINT chk_validation_modif
CHECK (
(statut <> 'valide')
OR (modifie_le IS NULL OR modifie_le >= cree_le)
);