DROP TABLE IF EXISTS REPAS;
DROP TABLE IF EXISTS NOURRITURE;
DROP TABLE IF EXISTS AMOUR;
DROP TABLE IF EXISTS DRAGON;
DROP TABLE IF EXISTS TERRITOIRE;

\! clear

CREATE TABLE TERRITOIRE(
    num_terr        char(3) NOT NULL PRIMARY KEY,
    nom             varchar(30) UNIQUE,
    longitude       numeric NOT NULL CHECK(longitude <= 180 AND longitude >= -180),
    latitude1        numeric NOT NULL CHECK (latitude1 <= 90),
    latitude2        char(1) NOT NULL CHECK (latitude2 = 'N' OR latitude2 = 'S')    
);
CREATE TABLE DRAGON(
    num             char(5) NOT NULL PRIMARY KEY,
    nom             varchar(30) UNIQUE,
    longueur        numeric NOT NULL CHECK (longueur > 0),
    sexe            char(1) NOT NULL CHECK (sexe = 'F' OR sexe = 'M'),
    nb_ecailles     numeric NOT NULL CHECK (nb_ecailles >= 0),
    date_naissance  date NOT NULL,
    en_amour        varchar(20) CHECK (en_amour = 'macho' OR en_amour = 'timide' OR en_amour = 'sincere' OR en_amour = 'volage'),
    crache_feu      char(1) CHECK (crache_feu = 'O' OR crache_feu = 'N'),
    num_terr char(4) REFERENCES TERRITOIRE,
    CHECK (sexe = 1 AND en_amour = 'macho' OR sexe = 0 AND en_amour != 'macho')
);
CREATE TABLE NOURRITURE(
    num              char(5) PRIMARY KEY,
    nom              varchar(30) NOT NULL UNIQUE,
    calories         numeric NOT NULL CHECK(calories > 0)
);
CREATE TABLE AMOUR(
    force           varchar(20) CHECK (force = 'un peu' OR force = 'beaucoup' OR force = 'pasionnement' OR force = 'a la folie'),
    num_dragon1     char(5) REFERENCES DRAGON,
    num_dragon2     char(5) REFERENCES DRAGON,
    PRIMARY KEY (num_dragon1, num_dragon2)
);
CREATE TABLE REPAS(
    qte             numeric CHECK(qte > 0),
    date_repas      date,
    num_dragon      char(5) REFERENCES DRAGON,
    num_nourr       char(5) REFERENCES NOURRITURE,
    PRIMARY KEY (num_dragon, num_nourr)
);
/*
INSERT INTO TERRITOIRE VALUES('T01', 'terre brûlées', 92, 40, 'S');


INSERT INTO DRAGON VALUES('d0001', 'Solong', '173', 'M', '1481', '25/02/2012', 'timide', 'O', 't01');

INSERT INTO NOURRITURE VALUES('P0001','pomme',7);

INSERT INTO AMOUR VALUES(NULL, 'D0001', 'D0002');

INSERT INTO REPAS VALUES(1000,'10/09/2021','D0001','P0002');

INSERT INTO 


TRUNCATE TABLE TERRITOIRE, DRAGON, NOURRITURE, AMOUR, REPAS;
*/
INSERT INTO  DRAGON VALUES('d0001', 'Solong', '173', 'M', '1481', '25/02/2012', 'timide', 'O', 't01');
INSERT INTO TERRITOIRE VALUES('T01', 'terre brûlées', 92, 40, 'S');
INSERT INTO AMOUR VALUES


TRUNCATE TABLE TERRITOIRE, DRAGON, NOURRITURE, AMOUR, REPAS;

INSERT INTO TERRITOIRE VALUES ('T01','terres_brulees',92,42,'S');
INSERT INTO TERRITOIRE VALUES ('T02','terre_des_fleurs',98,48,'S');
INSERT INTO TERRITOIRE VALUES ('T03','terre_des_neiges',100,8,'N');


INSERT INTO DRAGON VALUES ('D0001','Smeagol',152,1,1857,'14/06/1985','macho','O','T01');
INSERT INTO DRAGON VALUES ('D0002','Birdurh',258,1,4787,'05/05/1989','timide','N','T01');
INSERT INTO DRAGON VALUES ('D0003','Negueth',128,1,1587,'08/09/2018','sincere','O','T02');
INSERT INTO DRAGON VALUES ('D0004','Miss Toc',183,1,2781,'04/07/2020','volage',NULL,'T01');
INSERT INTO DRAGON VALUES ('D0005','Bolong',213,1,754,'06/05/2010','macho','N','T01');
INSERT INTO DRAGON VALUES ('D0006','Miloch',83,1,718,'29/04/2015','timide','O','T02');
INSERT INTO DRAGON VALUES ('D0007','Nessie',168,1,1721,'12/12/2000','macho','O','T02');
INSERT INTO DRAGON VALUES ('D0008','Tarak',123,0,851,'15/04/2009','timide','N','T01');
INSERT INTO DRAGON VALUES ('D0009','Solong',173,1,1481,'04/08/2021','timide',NULL,'T01');


INSERT INTO NOURRITURE VALUES ('P0001','pomme',7);
INSERT INTO NOURRITURE VALUES ('P0002','cacahuete',10);
INSERT INTO NOURRITURE VALUES ('P0003','orange',25);
INSERT INTO NOURRITURE VALUES ('P0004','oeuf',15);
INSERT INTO NOURRITURE VALUES ('P0005','ver',3);
INSERT INTO NOURRITURE VALUES ('P0006','poisson',35);

INSERT INTO AMOUR VALUES ('passionnement','D0001','D0008');
INSERT INTO AMOUR VALUES ('beaucoup','D0002','D0003');
INSERT INTO AMOUR VALUES ('a la folie','D0003','D0006');
INSERT INTO AMOUR VALUES ('a la folie','D0006','D0003');
INSERT INTO AMOUR VALUES ('un peu','D0008','D0005');
INSERT INTO AMOUR VALUES ('beaucoup','D0005','D0008');
INSERT INTO AMOUR VALUES ('un peu','D0007','D0008');

INSERT INTO REPAS VALUES (1000,'10/09/2021','D0001','P0002');
INSERT INTO REPAS VALUES (16,'10/09/2021','D0001','P0001');
INSERT INTO REPAS VALUES (4,'11/09/2021','D0005','P0004');
INSERT INTO REPAS VALUES (6,'10/03/2021','D0003','P0003');
INSERT INTO REPAS VALUES (1,'11/09/2021','D0003','P0004');
INSERT INTO REPAS VALUES (53,'10/09/2021','D0006','P0005');
INSERT INTO REPAS VALUES (100,'11/09/2021','D0006','P0002');
INSERT INTO REPAS VALUES (20,'10/09/2021','D0007','P0006');
INSERT INTO REPAS VALUES (10,'10/09/2021','D0008','P0001');
INSERT INTO REPAS VALUES (10,'11/09/2021','D0008','P0003');
INSERT INTO REPAS VALUES (6,'09/09/2021','D0009','P0004');
INSERT INTO REPAS VALUES (1,'10/09/2021','D0009','P0006');
INSERT INTO REPAS VALUES (2,'11/09/2021','D0009','P0003');

SELECT * FROM TERRITOIRE;
SELECT * FROM DRAGON;
SELECT * FROM NOURRITURE;
SELECT * FROM AMOUR;
SELECT * FROM REPAS;

\d