/*
Laboratorio di 
BASI DI DATI - C.d.S. in Informatica
PROGETTAZIONE DI BASI DI DATI - C.d.S. in Informatica e Tecnologie per la Produzione del Software


Docente: dott.ssa Francesca A. Lisi
Tutor: dott.ssa Annalisa Gentile, dott.ssa Anna Lina Caputo

Bari, 12 novembre 2008
*/



DROP DATABASE IF EXISTS premiere;

CREATE DATABASE premiere;

USE premiere;

create table if not exists Articoli(
	NroArt char (4) primary key,
	descrizione char (20),
	giacenza decimal (4,0),
	categoria char (2),
	PrezzoUnitario decimal (6,2)
) ENGINE=INNODB;

create table if not exists Rappresentanti(
	CodRappr char (2) primary key,
	cognome char (10),
	nome char (8),
	via char (15),
	citta char (15),
	prov char (2),
	cap char (5),
	TotProvv decimal (7,2),
	PerProvv decimal (3,2)
) ENGINE=INNODB;

create table if not exists Clienti(
	CodCliente char (3) primary key,
	cognome char (10) not null,
	nome char (8) not null,
	via char (15),
	citta char (15),
	prov char (2),
	cap char (5),
	saldo decimal (7,2),
	fido decimal (6,2),
	CodRappr char (2) not null,
	index Rappr_ind (CodRappr),
	foreign key (CodRappr) references Rappresentanti(CodRappr) on delete cascade
) ENGINE=INNODB;

create table if not exists Ordini(
	NroOrdine char (5) primary key,
	data date,
	CodCliente char (3) not null,
	index Cli_ind (CodCliente),
	foreign key (CodCliente) references Clienti(CodCliente) on delete cascade
) ENGINE=INNODB;

create table if not exists DettOrdini(
	NroOrdine char (5) not null,
	index Ord_ind (NroOrdine),
	foreign key (NroOrdine) references Ordini (NroOrdine) on delete cascade,
	NroArticolo char (4) not null,
	index Art_ind (NroArticolo),
	foreign key (NroArticolo) references Articoli(NroArt) on delete cascade,
	QtaOrdinata decimal (3,0),
	PrzRichiesto decimal (6,2)
) ENGINE=INNODB;

insert into Articoli values ('AX12','FerroStiro',104,'cs',24.95);
insert into Articoli values ('AZ52','Freccette',20,'sp',12.95);
insert into Articoli values ('BA74','Pallone',40,'sp',29.95);
insert into Articoli values ('BH22','Tritatutto',95,'cs',24.95);
insert into Articoli values ('BT04','Forno',11,'el',149.99);
insert into Articoli values ('BZ66','Lavatrice',52,'el',399.99);
insert into Articoli values ('CA14','Setaccio',78,'cs',39.99);
insert into Articoli values ('CB03','Bicicletta',44,'sp',299.99);
insert into Articoli values ('CX11','Frullino',112,'cs',22.95);
insert into Articoli values ('CZ81','TavolaPesi',68,'sp',349.95);

insert into Rappresentanti values('03','Jones','Mary','123 Main','Grant','MI','49219',2150.00,0.05);
insert into Rappresentanti values('06','Smith','William','102 Raymond','Ada','MI','49441',4912.50,0.07);
insert into Rappresentanti values('12','Diaz','Miguel','419 Harper','Lansing','MI','49224',2150.00,0.05);

insert into Clienti values ('124','Adams','Sally','481 Oak','Lansing','MI','49224',818.75,1000,'03');
insert into Clienti values ('256','Samueles','Ann','215 Pete','Grant','MI','49219',21.50,1500,'06');
insert into Clienti values ('311','Charles','Don','48 College','Ira','MI','49034',825.75,1000,'12');
insert into Clienti values ('315','Daniels','Tom','914 Cherry','Kent','MI','48391',770.75,750,'06');
insert into Clienti values ('405','Williams','Al','519 Watson','Grant','MI','49219',402.75,1500,'12');
insert into Clienti values ('412','Adams','Sally','16 Elm','Lansing','MI','49224',1817.50,2000,'03');
insert into Clienti values ('522','Nelson','Mary','108 Pine','Ada','MI','49441',98.75,1500,'12');
insert into Clienti values ('567','Dinh','Tran','808 Ridge','Harper','MI','48421',402.40,750,'06');
insert into Clienti values ('587','Galvez','Mara','512 Pine','Ada','MI','49441',114.60,1000,'06');
insert into Clienti values ('622','Martin','Dan','419 Chip','Grant','MI','49219',1045.75,1000,'03');

insert into  Ordini values('12489','2002/09/02','124');
insert into  Ordini values('12491','2002/09/02','311');
insert into  Ordini values('12494','2002/09/04','315');
insert into  Ordini values('12495','2002/09/04','256');
insert into  Ordini values('12498','2002/09/05','522');
insert into  Ordini values('12500','2002/09/05','124');
insert into  Ordini values('12504','2002/09/05','522');

insert into DettOrdini values ('12489','AX12',11,21.95);
insert into DettOrdini values ('12491','BT04',1,149.99);
insert into DettOrdini values ('12491','BZ66',1,399.99);
insert into DettOrdini values ('12494','CB03',4,279.99);
insert into DettOrdini values ('12495','CX11',2,22.95);
insert into DettOrdini values ('12498','AZ52',2,12.95);
insert into DettOrdini values ('12498','BA74',4,24.95);
insert into DettOrdini values ('12500','BT04',1,149.99);
insert into DettOrdini values ('12504','CZ81',2,325.99);

