DROP TABLE CONTENIR;
DROP TABLE POTION;
DROP TABLE OBTENIR;
DROP TABLE INGREDIENT;
DROP TABLE SOURCESS;

\! clear

/*      fAIRE ATTENTION À L'ORDRE ET PAS TROP FAIRE LES DROP TABLE ENSEMBLE !!! */

CREATE TABLE SOURCES(
    idSources       char(4) PRIMARY KEY,
    nom             varchar(20) NOT NULL UNIQUE,
    lieu            varchar(50),
    dateCollecte    date
);

CREATE TABLE INGREDIENT(
    idIngredient    char(4) PRIMARY KEY,
    designation     varchar(50) NOT NULL UNIQUE,
    idSOURCESs char(4) REFERENCES SOURCES
);

CREATE TABLE POTION(
    idPotion char(4) PRIMARY KEY,
    effet    varchar(50) DEFAULT 'INCONNU'
);

CREATE TABLE CONTENIR(
    idPotion    char(4) REFERENCES POTION,
    idIngredient char(4) REFERENCES INGREDIENT,
    quantite    numeric(3) CHECK(quantite > 0),
    unite       varchar(2)
);

CREATE TABLE OBTENIR(
    transformation      char(10),
    idIngredientBase        char(4) REFERENCES INGREDIENT,
    idIngredientObtenue     char(4) REFERENCES INGREDIENT
);

INSERT INTO SOURCES VALUES ('S001','Abeille sauvage','plaines verdoyantes','22/09/2021');
INSERT INTO SOURCES VALUES ('S002','Branchiflore','marais sombre','08/09/2021');
INSERT INTO SOURCES VALUES ('S003','Licorne','montagne dorée','16/04/2020');
INSERT INTO SOURCES VALUES ('S004','Mandragore','forêt de l Est','2/10/2021');
INSERT INTO SOURCES VALUES ('S005','Pin bleu','forêt de l Est','10/10/2021');
INSERT INTO SOURCES VALUES ('S006','Sirène','mer d opale','03/02/2021');
INSERT INTO SOURCES VALUES ('S007','Tortue géante','mer d opale','03/11/2022');

INSERT INTO INGREDIENT VALUES('I001', 'poudre de Mandragore', 'S004');
INSERT INTO INGREDIENT VALUES('I002', 'larmes de Sirene', 'S006');
INSERT INTO INGREDIENT VALUES('I003', 'Miel', 'S001');
INSERT INTO INGREDIENT VALUES('I004', 'poils de Licorne', 'S003');
INSERT INTO INGREDIENT VALUES('I005', 'seve de Pin', 'S005');
INSERT INTO INGREDIENT VALUES('I006', 'Branchiflore', 'S002');
INSERT INTO INGREDIENT VALUES('I007', 'ecailles de tortues', 'S007');
INSERT INTO INGREDIENT VALUES('I008', 'racine de Mandragore', 'S002');
INSERT INTO INGREDIENT VALUES('I009', 'rayon de miel d Abeille sauvage', 'S001');

INSERT INTO POTION VALUES ('P001','Invisibilité');
INSERT INTO POTION VALUES ('P002','Force');
INSERT INTO POTION VALUES ('P003');

INSERT INTO CONTENIR VALUES ('P001','I001',20,'g');
INSERT INTO CONTENIR VALUES ('P001','I002',2,'u');
INSERT INTO CONTENIR VALUES ('P001','I003',20,'cl');
INSERT INTO CONTENIR VALUES ('P001','I004',5,'g');
INSERT INTO CONTENIR VALUES ('P002','I005',10,'cl');
INSERT INTO CONTENIR VALUES ('P002','I006',3,'u');
INSERT INTO CONTENIR VALUES ('P002','I007',12,'g');
INSERT INTO CONTENIR VALUES ('P002','I008',1,'u');

INSERT INTO OBTENIR VALUES ('I008','I001','Broyage');
INSERT INTO OBTENIR VALUES ('I003','I009','Extraction');


SELECT * FROM CONTENIR;
SELECT * FROM POTION;
SELECT * FROM OBTENIR;
SELECT * FROM INGREDIENT;
SELECT * FROM SOURCESS;

/*  Question 3  */
/*  SELECT * FROM POTION ORDER BY effet;    */

/*  Question 4  */
/*  SELECT DISTINCT lieu FROM SOURCESS;  */

/*  Question 5  */
/*  */

\d