create table Buveur (
NumBuveur number primary key,
Nom Varchar2(25),
Prenom Varchar2(25),
Ville Varchar2(30));


Create table Viticulteur ( 
	NumVitic Number Primary key, 
	Nom Varchar2(25) not null, 
	Prenom Varchar2(25), 
	Ville Varchar2 (30) not null ); 
	
Create table Vin ( 
	NumVin Number, 
	Cru Varchar2(15) not null, 
	Millesime Number not null, 
	Region Varchar2(15) not null, 
	NumVitic Number not null references Viticulteur,
    Primary key (NumVin)); 
	
Create table Commande ( 
	NumCom Number Primary key, 
	NumBuveur Number not null references Buveur, 
	NumVin Number not null references Vin, 
	Qtte Number not null); 
	
-- on rajoute une colonne à la table commande (nom de l'attribut datecom, type date)
alter table commande add datecom date;

Create table Livraison ( 
	NumCom Number not null references Commande, 
	Qtte Number not null, 
	DateLiv Date not null, 
	Primary key (NumCom, DateLiv));

Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1400','GAUTHIER','ROBERT','PARIS');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1500','FRADET','PHILIPPE','MACON');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1300','JAMET','FLORENCE','VERSAILLES');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1100','COMBET','ALINE','DIJON');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1600','DUPOND','ALAIN','PARIS');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1700','COMBES','EDOUARD','PARIS');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1350','GRAVINEL','SOPHIE','NICE');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1450','PASCAL','YVES','MACON');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1750','DURIEUX','BLAISE','CHINON');
Insert into BUVEUR (NUMBUVEUR,NOM,PRENOM,VILLE) values ('1200','MAURY','FRANCOIS','PARIS');

Insert into VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('1','MOULIN','JEAN','MACON');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('3','MORIN','JULES','BENAIS');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('5','COULIS DUTHEIL',null,'CHINON');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('4','VALLEE','CLAUDE','SAINT NICOLAS');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('10','PARENT','PIERRE','POMMARD');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('15','BARANCOURT',null,'TOURS SUR MARNE');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('16','JADOT','LOUIS','BEAUNE');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('18','MEDEVILLE','CHARLES','PREIGNAC');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('21','LYNCH-BAGES',null,'PAUILLAC');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('22','CHATEAU MONBRISON',null,'MARGAUX');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('23','CHATEAU MOULIN A VENT',null,'CASTELNAU DE MEDOC');
Insert into  VITICULTEUR (NUMVITIC,NOM,PRENOM,VILLE) values ('20','TISSOT','JACQUES','ARBOIS');

Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('160','BOURGUEIL','1984','LOIRE','3');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('88','CHAMBERTIN','1980','BOURGOGNE','1');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('170','BOUZY','1984','CHAMPAGNE','15');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('175','MEURSAULT','1980','BOURGOGNE','16');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('80','SAUTERNES','1982','BORDEAUX','18');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('84','PAUILLAC','1982','BORDEAUX','21');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('90','MARGAUX','1978','BORDEAUX','22');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('98','MOULIS','1981','BORDEAUX','23');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('86','CHINON','1978','LOIRE','4');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('85','CHINON','1983','LOIRE','5');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('120','CHINON','1985','LOIRE','4');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('190','SAINT EMILION','1981','BORDEAUX','18');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('128','BOURGUEIL','1985','LOIRE','4');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('140','POMMARD','1976','BOURGOGNE','10');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('150','MONTHELIE','1978','BOURGOGNE','10');
Insert into  VIN (NUMVIN,CRU,MILLESIME,REGION,NUMVITIC) values ('100','CHAMPAGNE BRUT','1988','CHAMPAGNE','15');

Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('8','1400','140','12',to_date('10/01/88','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('3','1500','88','24',to_date('10/09/87','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('2','1500','100','18',to_date('20/11/87','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('4','1600','175','6',to_date('12/02/87','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('5','1300','140','3',to_date('18/03/87','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('1','1600','90','6',to_date('10/06/87','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('6','1100','140','8',to_date('24/12/87','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('7','1200','88','3',to_date('24/12/87','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('11','1200','140','12',to_date('12/12/89','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('12','1100','120','6',to_date('13/12/89','DD/MM/RR'));
Insert into  COMMANDE (NUMCOM,NUMBUVEUR,NUMVIN,QTTE,DATECOM) values ('10','1500','100','18',to_date('25/03/20','DD/MM/RR'));


insert into livraison (dateliv, qtte, numcom) values 
(to_date('30/08/87', 'DD/MM/RR'), '12', '2');
insert into livraison (dateliv, qtte, numcom) values 
(to_date('15/07/87', 'DD/MM/RR'), '6', '1');
insert into livraison (dateliv, qtte, numcom) values 
(to_date('27/09/87', 'DD/MM/RR'), '6', '2');
insert into livraison (dateliv, qtte, numcom) values 
(to_date('20/11/87', 'DD/MM/RR'), '12', '3');
insert into livraison (dateliv, qtte, numcom) values 
(to_date('20/12/87', 'DD/MM/RR'), '6', '4');
insert into livraison (dateliv, qtte, numcom) values 
(to_date('28/12/87', 'DD/MM/RR'), '8', '6');
insert into livraison (dateliv, qtte, numcom) values 
(to_date('30/12/87', 'DD/MM/RR'), '3', '7');

-- Q1
-- sql 89 
select distinct Nom, prenom, c.numbuveur 
from Buveur b, commande c
where c.numbuveur = b.numbuveur
order by c.numbuveur;

-- SQL 92
select distinct Nom, prenom, numbuveur 
from Buveur natural join commande 
order by numbuveur;

-- Q2
-- sql89
select v.numvitic, nom, ville
from viticulteur vi, vin v
where vi.numvitic = v.numvitic and
    v.region = 'LOIRE' and v.millesime = '1983';

-- SQL92
select numvitic, nom, ville
from viticulteur natural join vin v
where  v.region = 'LOIRE' and v.millesime = '1983';

-- Q3
-- SQL89
-- SQL 89
select distinct B.numbuveur, nom 
from buveur b, commande c, vin v 
where b.numbuveur = c.numbuveur and c.numvin=v.numvin and cru = 'POMMARD' ;

-- SQL92
select distinct numbuveur, nom 
from buveur natural join commande natural join vin  
where cru = 'POMMARD' ;


-- Q4
-- SQL 92
select distinct nom 
from commande natural join vin natural join viticulteur 
where numbuveur = 1600;

select distinct nom 
from viticulteur 
where numvitic in (select numvitic 
                from vin 
                where numvin in (select numvin 
                                from commande 
                                where numbuveur = 1600));
                 
select distinct t.nom 
from (buveur b natural join commande natural join vin)
         join viticulteur t using (numVitic)
where b.nom = 'DUPOND'; 
               
select distinct numvitic, t.nom , ville 
from (buveur natural join commande natural join vin) 
        join viticulteur t using(numvitic, ville);


select distinct numvitic, nom , ville 
from viticulteur t 
where numvitic in (select numvitic 
                from vin 
                where numvin in (select numvin
                                from commande 
                                where numbuveur in (select numbuveur 
                                                    from buveur 
                                                    where ville = t.ville)));
                                                    
                                                    
select * 
from buveur 
where ville in (select ville 
                from buveur 
                where numbuveur = 1400);

select qtte 
                                    from commande 
                                    where numcom = 11 and numvin = 140;
                
                
select * 
from commande 
where numvin = 140 and qtte < Any (select qtte 
                                    from commande 
                                    where numcom = 11 and numvin = 140);
