Your IP : 216.73.217.64


Current Path : /home3/gp97/resultats2020.mlb.97gp.net/r2020/data/pgsql/
Upload File :
Current File : //home3/gp97/resultats2020.mlb.97gp.net/r2020/data/pgsql/init_metier.sql

--------------------------------------------------------------------------------
-- Mise à jour des séquences avec le max + 1
--



CREATE TABLE bureau (
  bureau integer NOT NULL,
  libelle varchar(30) NOT NULL ,
  type_bureau integer not null,
  ordre integer NOT NULL,
  canton_prefecture varchar(3) ,
  circonscription_prefecture varchar(3),
  adresse1 character varying(100),
  adresse2 character varying (100),
  cp character(5),
  ville character varying(40),
  geom public.geometry(Point,2154),
  coordinates varchar(100),
  PRIMARY KEY (bureau)
) ;


CREATE TABLE type_bureau (
  type_bureau integer NOT NULL,
  libelle varchar(30) NOT NULL ,
  PRIMARY KEY (type_bureau)
) ;



CREATE TABLE candidat (
  candidat integer NOT NULL,
  libelle varchar(40) NOT NULL ,
  libelle_long varchar(80),
  PRIMARY KEY (candidat)
);


CREATE TABLE perimetre (
  perimetre integer NOT NULL ,
  libelle varchar(40) NOT NULL ,
  prefecture varchar(8) NOT NULL ,
  geom public.geometry(Polygon,2154),
  PRIMARY KEY (perimetre)
);

CREATE TABLE perimetre_bureau (
  perimetre_bureau integer not null,
  bureau integer not null, 
  perimetre integer NOT NULL ,
  PRIMARY KEY (perimetre_bureau)
);

CREATE TABLE election (
  election integer NOT NULL ,
  libelle varchar(40) NOT NULL ,
  code varchar(10),
  tour integer, 
  principal boolean,
  type_election integer NOT NULL,
  date date NOT NULL, 
  perimetre integer not null ,
  votant_defaut integer, 
  creation_bureau boolean,
  heure_ouverture integer not null ,
  heure_fermeture integer not null ,
  creation_participation boolean,
  cloture boolean ,
  verrou boolean,
  envoi_initial boolean,
  affichage_1 varchar(20) not null,
  refresh_1 integer not null,
  affichage_2 varchar(20) not null,
  refresh_2 integer not null,  
  PRIMARY KEY (election)
) ;


CREATE TABLE election_bureau (
  election_bureau integer NOT NULL ,	
  election integer NOT NULL ,
  bureau integer NOT NULL ,
  inscrit integer,
  votant integer,
  blanc integer,
  nul integer,
  exprime integer,
  envoi_aff boolean,
  envoi_web boolean,
  PRIMARY KEY (election_bureau)
) ;


CREATE TABLE election_candidat (
  election_candidat integer NOT NULL ,
  election integer NOT NULL ,
  candidat integer NOT NULL,
  ordre integer NOT NULL ,
  prefecture varchar(10),
  creation_resultat boolean,
  age_moyen numeric,
  siege integer,
  age_moyen_com numeric,
  siege_com integer,
  PRIMARY KEY (election_candidat)
) ;


CREATE TABLE election_resultat (
  election_resultat integer not null,	
  election_bureau integer NOT NULL ,
  election_candidat integer NOT NULL ,
  resultat integer,
  PRIMARY KEY (election_resultat)
) ;


CREATE TABLE participation_bureau (
  participation_bureau integer not null,
  election_bureau integer NOT NULL ,
  participation_election integer NOT NULL ,
  votant integer,	
  PRIMARY KEY (participation_bureau)
) ;


CREATE TABLE participation_election (
  participation_election integer not null, 	
  election integer NOT NULL ,
  tranche integer NOT NULL ,
  envoi_aff boolean,
  envoi_web boolean, 
  PRIMARY KEY (participation_election)
) ;



CREATE TABLE tranche (
  tranche integer NOT NULL ,
  libelle time NOT NULL ,
  ordre integer not null,
  PRIMARY KEY (tranche)
) ;


CREATE TABLE type_election (
  type_election integer NOT NULL ,
  libelle varchar(30) NOT NULL ,
  code varchar(8) NOT NULL,
  prefecture varchar(3) NOT NULL,
  PRIMARY KEY (type_election)
) ;





-- cle secondaire

ALTER TABLE ONLY election
    ADD CONSTRAINT election_type_election_fkey FOREIGN KEY (type_election) REFERENCES type_election(type_election);
ALTER TABLE ONLY election
    ADD CONSTRAINT election_perimetre_fkey FOREIGN KEY (perimetre) REFERENCES perimetre(perimetre);
ALTER TABLE ONLY election
    ADD CONSTRAINT election_heure_ouverture_fkey FOREIGN KEY (heure_ouverture) REFERENCES tranche(tranche);
ALTER TABLE ONLY election
    ADD CONSTRAINT election_heure_fermeture_fkey FOREIGN KEY (heure_fermeture) REFERENCES tranche(tranche);

ALTER TABLE ONLY bureau
    ADD CONSTRAINT bureau_type_bureau_fkey FOREIGN KEY (type_bureau) REFERENCES type_bureau(type_bureau);


ALTER TABLE ONLY perimetre_bureau
    ADD CONSTRAINT perimetre_bureau_bureau_fkey FOREIGN KEY (bureau) REFERENCES bureau(bureau);
ALTER TABLE ONLY perimetre_bureau
    ADD CONSTRAINT perimetre_bureau_perimetre_fkey FOREIGN KEY (perimetre) REFERENCES perimetre(perimetre);

ALTER TABLE ONLY election_bureau
    ADD CONSTRAINT election_bureau_election_fkey FOREIGN KEY (election) REFERENCES election(election);
ALTER TABLE ONLY election_bureau
    ADD CONSTRAINT election_bureau_bureau_fkey FOREIGN KEY (bureau) REFERENCES bureau(bureau);
ALTER TABLE ONLY election_candidat
    ADD CONSTRAINT election_candidat_election_fkey FOREIGN KEY (election) REFERENCES election(election);
ALTER TABLE ONLY election_candidat
    ADD CONSTRAINT election_candidat_candidat_fkey FOREIGN KEY (candidat) REFERENCES candidat(candidat);
    
ALTER TABLE ONLY election_resultat
    ADD CONSTRAINT election_resultat_election_candidat_fkey FOREIGN KEY (election_candidat) REFERENCES election_candidat(election_candidat);
ALTER TABLE ONLY election_resultat
    ADD CONSTRAINT election_resultat_election_bureau_fkey FOREIGN KEY (election_bureau) REFERENCES election_bureau(election_bureau);

ALTER TABLE ONLY participation_bureau
    ADD CONSTRAINT participation_bureau_election_bureau_fkey FOREIGN KEY (election_bureau) REFERENCES election_bureau(election_bureau);
ALTER TABLE ONLY participation_bureau
    ADD CONSTRAINT participation_bureau_participation_election_fkey FOREIGN KEY (participation_election) REFERENCES participation_election(participation_election);

ALTER TABLE ONLY participation_election
    ADD CONSTRAINT participation_election_election_fkey FOREIGN KEY (election) REFERENCES election(election);
ALTER TABLE ONLY participation_election
    ADD CONSTRAINT participation_election_tranche_fkey FOREIGN KEY (tranche) REFERENCES tranche(tranche);



-- cle unique
ALTER TABLE election_candidat ADD CONSTRAINT election_candidat_unique  UNIQUE (election,candidat);
ALTER TABLE election_bureau ADD CONSTRAINT election_bureau_unique  UNIQUE (election,bureau);
ALTER TABLE election_resultat ADD CONSTRAINT election_bureau_election_candidat_unique  UNIQUE (election_bureau,election_candidat);
ALTER TABLE participation_election ADD CONSTRAINT election_tranche_unique  UNIQUE (election,tranche);
ALTER TABLE participation_bureau ADD CONSTRAINT participation_election_election_bureau_unique  UNIQUE (participation_election,election_bureau);



CREATE SEQUENCE bureau_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE SEQUENCE election_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE SEQUENCE candidat_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
    
CREATE SEQUENCE canton_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
CREATE SEQUENCE election_bureau_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE SEQUENCE election_candidat_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE SEQUENCE election_resultat_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;   
     
CREATE SEQUENCE participation_bureau_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;   

CREATE SEQUENCE participation_election_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1; 
    
CREATE SEQUENCE tranche_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;      
    
CREATE SEQUENCE type_election_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;   



CREATE SEQUENCE perimetre_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE SEQUENCE perimetre_bureau_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

CREATE SEQUENCE type_bureau_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;




INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'election', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'election_bureau', 3);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'election_candidat', 3);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'election_resultat', 3);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'type_election', 3);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'tranche', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'participation_bureau', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'participation_election', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'menu_administration', 1);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'menu_parametrage', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'bureau', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'canton', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'candidat', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'perimetre', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'perimetre_bureau', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'type_bureau', 2);
INSERT INTO om_droit (om_droit, libelle, om_profil) VALUES (nextval('om_droit_seq'), 'liste', 3);