-- ============================================================================
-- Base Name        : FREE-BRICKS
-- SGBD Name        : PostgreSQL
-- CREATION Date    : 24/10/24
-- ============================================================================

-- ============================================================================
-- Tables
-- ============================================================================

CREATE TABLE acheter
(
    id_membre                   INTEGER                      NOT NULL,
    id_piece                    INTEGER                      NOT NULL,
    quantite_acheter            INTEGER                             ,
    CONSTRAINT pk__acheter PRIMARY KEY (id_membre, id_piece)
);

CREATE TABLE avoir_motif
(
    id_piece                    INTEGER                      NOT NULL,
    id_motif                    INTEGER                      NOT NULL,
    CONSTRAINT pk__avoir_motif PRIMARY KEY (id_piece, id_motif)
);

CREATE TABLE boites
(
    id_boite                    SERIAL                      NOT NULL,
    titre_boite                 varchar(255)                NOT NULL,
    date_boite                  DATE                                ,
    CONSTRAINT pk__boites PRIMARY KEY (id_boite)
);

CREATE TABLE colorer
(
    id_piece                    INTEGER                      NOT NULL,
    id_couleur                  INTEGER                      NOT NULL,
    CONSTRAINT pk__colorer PRIMARY KEY (id_piece, id_couleur)
);

CREATE TABLE construire
(
    id_boite                    INTEGER                      NOT NULL,
    id_modele                   INTEGER                      NOT NULL,
    CONSTRAINT pk__construire PRIMARY KEY (id_boite, id_modele)
);

CREATE TABLE contenir
(
    id_boite                    INTEGER                      NOT NULL,
    id_piece                    INTEGER                      NOT NULL,
    quantite_contenir           INTEGER                     NOT NULL,
    CONSTRAINT pk__contenir PRIMARY KEY (id_boite, id_piece)
);

CREATE TABLE couleurs
(
    id_couleur                  SERIAL                      NOT NULL,
    nom_couleur                 VARCHAR(255)                        ,
    CONSTRAINT pk__couleurs PRIMARY KEY (id_couleur)
);

CREATE TABLE enregistrer
(
    id_boite                    INTEGER                      NOT NULL,
    id_membre                   INTEGER                      NOT NULL,
    quantite_enregistrer        INTEGER                     NOT NULL,
    CONSTRAINT pk__enregistrer PRIMARY KEY (id_boite, id_membre)
);

CREATE TABLE etre
(
    id_piece                    INTEGER                      NOT NULL,
    id_marque                   INTEGER                      NOT NULL,
    CONSTRAINT pk__etre PRIMARY KEY (id_piece, id_marque)
);

CREATE TABLE etre_complexe
(
    id_piece                    INTEGER                      NOT NULL,
    id_piece_complexe           INTEGER                      NOT NULL,
    CONSTRAINT pk__etre_complexe PRIMARY KEY (id_piece, id_piece_complexe)
);

CREATE TABLE etre_forme
(
    id_forme                    INTEGER                      NOT NULL,
    id_piece                    INTEGER                      NOT NULL,
    CONSTRAINT pk__etre_forme PRIMARY KEY (id_forme, id_piece)
);

CREATE TABLE fils
(
    id_fil                      INTEGER                      NOT NULL,
    id_modele                   INTEGER                      NOT NULL,
    CONSTRAINT pk__fils PRIMARY KEY (id_fil)
);

CREATE TABLE illustrations
(
    id_illustration             SERIAL                      NOT NULL,
    url_illustration            VARCHAR(2048)                       ,
    id_modele                   INTEGER                     NOT NULL,
    CONSTRAINT pk__illustrations PRIMARY KEY (id_illustration)
); 

CREATE TABLE marques
(
    id_marque                   SERIAL                      NOT NULL,
    nom_marque                  VARCHAR(255)                        ,
    CONSTRAINT pk__marques PRIMARY KEY (id_marque)
);

CREATE TABLE messages
(
    id_message                  SERIAL                      NOT NULL,
    contenu_message             VARCHAR(255)                        ,
    id_membre                   SERIAL                      NOT NULL,
    id_fil                      SERIAL                      NOT NULL,
    id_message_2                SERIAL                      NOT NULL,
    CONSTRAINT pk__messages PRIMARY KEY (id_message)
);

CREATE TABLE modeles
(
    id_modele                   SERIAL                      NOT NULL,
    nom_modele                  VARCHAR(255)                        ,
    url_notice_modele           VARCHAR(2048)                       ,
    id_membre                   INTEGER                     NOT NULL,
    id_modele_2                 INTEGER                     NOT NULL,
    CONSTRAINT pk__modeles PRIMARY KEY (id_modele)
);

CREATE TABLE necessiter
(
    id_modele                   INTEGER                      NOT NULL,
    id_piece                    INTEGER                      NOT NULL,
    quantite_necessiter         INTEGER                     NOT NULL,
    CONSTRAINT pk__necessiter PRIMARY KEY (id_modele, id_piece)
);

CREATE TABLE noter
(
    id_modele                   INTEGER                      NOT NULL,
    id_membre                   INTEGER                      NOT NULL,
    note_noter                  VARCHAR(255)                NOT NULL,
    CONSTRAINT pk__noter PRIMARY KEY (id_modele, id_membre)
);

CREATE TABLE perdre
(
    id_membre                   INTEGER                      NOT NULL,
    id_boite                    INTEGER                      NOT NULL,
    id_piece                    INTEGER                      NOT NULL,
    quantite_perdre             INTEGER                     NOT NULL,
    CONSTRAINT pk__perdre PRIMARY KEY (id_membre, id_boite, id_piece)
);

CREATE TABLE varier
(
    id_modele                   INTEGER                      NOT NULL,
    id_modele_2                 INTEGER                      NOT NULL,
    CONSTRAINT pk__varier PRIMARY KEY (id_modele, id_modele_2)
);

CREATE TABLE formes
(
    id_forme                   SERIAL                      NOT NULL,
    nom_forme                  VARCHAR(255)                NOT NULL,
    CONSTRAINT pk__formes PRIMARY KEY (id_forme)
);

CREATE TABLE membres
(
    id_membre                  SERIAL                      NOT NULL,
    nom_membre                 VARCHAR(255)                NOT NULL,
    mdp_membre                 VARCHAR(255)                NOT NULL,
    CONSTRAINT pk__membres PRIMARY KEY (id_membre)
);

CREATE TABLE motifs
(
    id_motif                   SERIAL                      NOT NULL,
    nom_motif                  VARCHAR(255)                NOT NULL,
    CONSTRAINT pk__motifs PRIMARY KEY (id_motif)
);

CREATE TABLE pieces
(
    id_piece                   SERIAL                      NOT NULL,
    CONSTRAINT pk__pieces PRIMARY KEY (id_piece)
);

CREATE TABLE pieces_complexes
(
    id_piece_complexe          SERIAL                      NOT NULL,
    CONSTRAINT pk__pieces_complexes PRIMARY KEY (id_piece_complexe)
);

-- ============================================================================
-- Foreign Keys
-- ============================================================================

ALTER TABLE acheter
    ADD CONSTRAINT fk1__acheter FOREIGN KEY (id_membre)
        REFERENCES membres (id_membre);

ALTER TABLE acheter
    ADD CONSTRAINT fk2__acheter FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE avoir_motif
    ADD CONSTRAINT fk1__avoir_motif FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE avoir_motif
    ADD CONSTRAINT fk2__avoir_motif FOREIGN KEY (id_motif)
        REFERENCES motifs (id_motif);

ALTER TABLE colorer
    ADD CONSTRAINT fk1__colorer FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE colorer
    ADD CONSTRAINT fk2__colorer FOREIGN KEY (id_couleur)
        REFERENCES couleurs (id_couleur);

ALTER TABLE construire
    ADD CONSTRAINT fk1__construire FOREIGN KEY (id_boite)
        REFERENCES boites (id_boite);

ALTER TABLE construire
    ADD CONSTRAINT fk2__construire FOREIGN KEY (id_modele)
        REFERENCES modeles (id_modele);

ALTER TABLE contenir
    ADD CONSTRAINT fk2__contenir FOREIGN KEY (id_boite)
        REFERENCES pieces (id_piece);

ALTER TABLE enregistrer
    ADD CONSTRAINT fk1__enregistrer FOREIGN KEY (id_boite)
        REFERENCES boites (id_boite);

ALTER TABLE enregistrer
    ADD CONSTRAINT fk2__enregistrer FOREIGN KEY (id_membre)
        REFERENCES membres (id_membre);

ALTER TABLE etre
    ADD CONSTRAINT fk1__etre FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE etre_complexe
    ADD CONSTRAINT fk1__etre_complexe FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE etre_complexe
    ADD CONSTRAINT fk2__etre_complexe FOREIGN KEY (id_piece_complexe)
        REFERENCES pieces_complexes (id_piece_complexe);

ALTER TABLE etre_forme
    ADD CONSTRAINT fk1__etre_forme FOREIGN KEY (id_forme)
        REFERENCES formes (id_forme);

ALTER TABLE etre_forme
    ADD CONSTRAINT fk2__etre_forme FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE fils
    ADD CONSTRAINT fk1__fils FOREIGN KEY (id_modele)
        REFERENCES modeles (id_modele);

ALTER TABLE illustrations
    ADD CONSTRAINT fk1__illustrations FOREIGN KEY (id_modele)
        REFERENCES modeles (id_modele);

ALTER TABLE messages
    ADD CONSTRAINT fk1__messages FOREIGN KEY (id_fil)
        REFERENCES fils (id_fil);

ALTER TABLE messages
    ADD CONSTRAINT fk2__messages FOREIGN KEY (id_message_2)
        REFERENCES messages (id_message);

ALTER TABLE necessiter
    ADD CONSTRAINT fk1__necessiter FOREIGN KEY (id_modele)
        REFERENCES modeles (id_modele);

ALTER TABLE necessiter
    ADD CONSTRAINT fk2__necessiter FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE noter
    ADD CONSTRAINT fk1__noter FOREIGN KEY (id_modele)
        REFERENCES modeles (id_modele);

ALTER TABLE noter
    ADD CONSTRAINT fk2__noter FOREIGN KEY (id_membre)
        REFERENCES membres (id_membre);

ALTER TABLE perdre
    ADD CONSTRAINT fk1__perdre FOREIGN KEY (id_membre)
        REFERENCES membres (id_membre);

ALTER TABLE perdre
    ADD CONSTRAINT fk2__perdre FOREIGN KEY (id_boite)
        REFERENCES boites (id_boite);

ALTER TABLE perdre
    ADD CONSTRAINT fk3__perdre FOREIGN KEY (id_piece)
        REFERENCES pieces (id_piece);

ALTER TABLE varier
    ADD CONSTRAINT fk1__varier FOREIGN KEY (id_modele)
        REFERENCES modeles (id_modele);

ALTER TABLE varier
    ADD CONSTRAINT fk2__varier FOREIGN KEY (id_modele_2)
        REFERENCES modeles (id_modele);

-- ============================================================================
-- Indexes
-- ============================================================================







-- ============================================================================

