-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCREATING TABLES.sql
More file actions
800 lines (612 loc) · 18 KB
/
CREATING TABLES.sql
File metadata and controls
800 lines (612 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
SET FOREIGN_KEY_CHECKS = 0;
drop database if exists SmartHome;
create database SmartHome;
use SmartHome;
-- ABITANTE --
--------------
drop table if exists Abitante;
create table Abitante
(
CodFiscale VARCHAR(50) NOT NULL,
Nome VARCHAR(50) NOT NULL,
Cognome VARCHAR(50) NOT NULL,
Scadenza DATE NOT NULL,
NumDoc VARCHAR(50) NOT NULL,
EnteRilascio VARCHAR(70) NOT NULL,
Telefono VARCHAR(10) default NULL,
TipoDoc VARCHAR(50) NOT NULL,
primary key (CodFiscale)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- USER --
----------
drop table if exists User;
create table User
(
UserName varchar(10) not null,
primary key (UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- registrazione --
-------------------
drop table if exists Registrazione;
create table Registrazione
(
abitante VARCHAR(50) NOT NULL,
user VARCHAR(50) NOT NULL,
DataRegistrazione date not null,
primary key (abitante, user),
foreign key (abitante) references Abitante(CodFiscale),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- domande --
-------------
drop table if exists Domande;
create table Domande
(
IDdomanda VARCHAR(10) NOT NULL,
DettagliDomanda VARCHAR(50) NOT NULL,
primary key (IDdomanda)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- recupero --
--------------
drop table if exists Recupero;
create table Recupero
(
domanda VARCHAR(10) NOT NULL,
user VARCHAR(10) NOT NULL,
risposta varchar(10) not null
check(risposta in ('si', 'no')),
primary key (domanda, user),
foreign key (domanda) references Domande(IDdomanda),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- stanza --
-- ----------
drop table if exists Stanza;
create table Stanza
(
IDstanza VARCHAR(50) NOT NULL,
piano int(5) not null,
-- check(piano >=0),
altezza double not null,
larghezza double not null,
lunghezza double not null,
EnergiaPerGrado double default null,
SensoreInterno double default null,
SensoreExterno double default null,
primary key (IDstanza)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- puntiAccesso --
-- ----------------
drop table if exists PuntiAccesso;
create table PuntiAccesso
(
NomeElemento VARCHAR(30) NOT NULL
check(NomeElemento in ('finestra', 'porta', 'portafinestra')),
primary key (NomeElemento)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- composizione --
------------------
drop table if exists Composizione;
create table Composizione
(
nome VARCHAR(30) NOT NULL,
stanza VARCHAR(50) NOT NULL,
Orientazione varchar(10) not null
check(Orientazione in ('N', 'NE', 'NW', 'S', 'SE', 'SW', 'E', 'W')),
primary key (nome, stanza, Orientazione),
foreign key (stanza) references Stanza(IDstanza),
foreign key (nome) references PuntiAccesso(NomeElemento)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- ElemLuce --
--------------
drop table if exists ElemLuce;
create table ElemLuce
(
IDelemLuce VARCHAR(10) NOT NULL,
Nome VARCHAR(20) NOT NULL
check(Nome in ('faretti', 'spot', 'lampade', 'neon') or null),
NumVolte int(10) default 0,
TemperaturaMax int(20) not null,
IntensitàMax int(20) not null,
primary key (IDelemLuce)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- presenza1 --
----------------
drop table if exists Presenza1;
create table Presenza1
(
ElemLuci VARCHAR(10) NOT NULL,
stanza VARCHAR(50) NOT NULL,
primary key (ElemLuci, stanza),
foreign key (stanza) references Stanza(IDstanza),
foreign key (ElemLuci) references ElemLuce(IDelemLuce)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- configurazioniLuci --
------------------------
drop table if exists ConfigurazioniLuci;
create table ConfigurazioniLuci
(
ElemLuci VARCHAR(10) NOT NULL,
user VARCHAR(10) NOT NULL,
Intensità int(50) not null,
Temperatura int(20) not null,
Timestamp timestamp not null,
primary key (ElemLuci, user, Timestamp),
foreign key (ElemLuci) references ElemLuce(IDelemLuce),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- StatoElemLuce --
-------------------
drop table if exists StatoElemLuce;
create table StatoElemLuce
(
ElemLuci VARCHAR(10) NOT NULL,
user VARCHAR(10) NOT NULL,
StatoElemento varchar(20) not null
check(StatoElemento in ('on', 'off')),
Timestamp timestamp not null,
primary key (ElemLuci, user, Timestamp),
foreign key (ElemLuci) references ElemLuce(IDelemLuce),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- ElemAria --
--------------
drop table if exists ElemAria;
create table ElemAria
(
IDelemAria VARCHAR(10) NOT NULL,
primary key (IDelemAria)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- presenza2 --
---------------
drop table if exists Presenza2;
create table Presenza2
(
ElementoAria VARCHAR(10) NOT NULL,
stanza VARCHAR(50) NOT NULL,
primary key (ElementoAria, stanza),
foreign key (ElementoAria) references ElemAria(IDelemAria),
foreign key (stanza) references Stanza(IDstanza)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- SettingCond --
------------------
drop table if exists SettingCond;
create table SettingCond
(
ElementoAria VARCHAR(10) NOT NULL,
user VARCHAR(10) NOT NULL,
Timestamp timestamp not null,
Temperatura int(20) default null,
LivUmidità int(20) default null,
OraInizio time default null,
OraFine time default null,
DataDeferred date default null,
EnergiaTemperatura double default null,
primary key (ElementoAria, user, Timestamp),
foreign key (ElementoAria) references ElemAria(IDelemAria),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- RecurrentSettingCond --
--------------------------
drop table if exists RecurrentSettingCond;
create table RecurrentSettingCond
(
ElemAria VARCHAR(10) NOT NULL,
user VARCHAR(10) NOT NULL,
Timestamp timestamp not null,
Temperatura int(20) default null,
LivUmidità int(20) default null,
OraInizio time default null,
OraFine time default null,
dayweek varchar(20) default 'all'
check(dayweek in ('lunedì', 'martedì', 'mercoledì', 'giovedì', 'venerdì')),
mese varchar(10) default 'all',
season varchar(20) default 'all'
check(season in ('inverno', 'estate', 'primavera', 'autunno')),
EnergiaTemperatura double default null,
primary key (ElemAria, user, Timestamp),
foreign key (ElemAria) references ElemAria(IDelemAria),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- SmartPlug --
---------------
drop table if exists SmartPlug;
create table SmartPlug
(
CodiceSmartplug VARCHAR(10) NOT NULL,
Stato varchar(20) default 'inattivo'
check(Stato in ('attivo', 'inattivo')), -- 0 = inattivo
primary key (CodiceSmartplug)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- Dispositivi --
drop table if exists Dispositivi;
create table Dispositivi
(
IDdispositivo VARCHAR(10) NOT NULL,
ConsumoTotale double default 0,
Conto int(20) default 0,
Tipo varchar(20) not null
check(Tipo in ('on-off', 'programmabili', 'regolabili')),
UsoSingolo varchar(10) default 'si'
check(UsoSingolo in ('si', 'no')),
PotenzaMax double default null,
PotenzaMin double default null,
primary key (IDdispositivo)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- Presenza3 --
---------------
drop table if exists Presenza3;
create table Presenza3
(
stanza VARCHAR(50) NOT NULL,
dispositivo VARCHAR(10) NOT NULL,
primary key (dispositivo, stanza),
foreign key (dispositivo) references Dispositivi(IDdispositivo),
foreign key (stanza) references Stanza(IDstanza)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- Collegamento --
------------------
drop table if exists Collegamento;
create table Collegamento
(
smartplug VARCHAR(10) NOT NULL,
dispositivo VARCHAR(10) NOT NULL,
primary key (dispositivo, smartplug),
foreign key (dispositivo) references Dispositivi(IDdispositivo),
foreign key (smartplug) references SmartPlug(CodiceSmartplug)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- Programmi --
---------------
drop table if exists Programmi;
create table Programmi
(
NomeProg VARCHAR(20) NOT NULL,
ConsumoMedio double default 0,
PotenzaProg double not null,
DurataProg double not null, -- durata in ora
primary key (NomeProg)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- Applicabilità --
-------------------
drop table if exists Applicabilità;
create table Applicabilità
(
programma VARCHAR(20) NOT NULL,
dispositivo VARCHAR(10) NOT NULL,
primary key (dispositivo, programma),
foreign key (dispositivo) references Dispositivi(IDdispositivo),
foreign key (programma) references Programmi(NomeProg)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- SetProgramDispositivi --
---------------------------
drop table if exists SetProgramDispositivi;
create table SetProgramDispositivi
(
programma VARCHAR(20) NOT NULL,
dispositivo VARCHAR(10) NOT NULL,
user varchar(10) not null,
Timestamp timestamp not null,
DataDeferred date default null,
OraDeferred time default null,
primary key (dispositivo, programma, Timestamp, user),
foreign key (dispositivo) references Dispositivi(IDdispositivo),
foreign key (programma) references Programmi(NomeProg),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
-- SettingDispositivi --
------------------------
drop table if exists SettingDispositivi;
create table SettingDispositivi
(
dispositivo VARCHAR(10) NOT NULL,
user varchar(10) not null,
Timestamp timestamp not null,
PotenzaSet double default null,
DurataSet double default null,
OraDeferred time default null,
DataDeferred date default null,
primary key (dispositivo, Timestamp, user),
foreign key (dispositivo) references Dispositivi(IDdispositivo),
foreign key (user) references User(UserName)
)ENGINE = InnoDB DEFAULT CHARSET = latin1;
SET FOREIGN_KEY_CHECKS = 1;
---------------------------------------------------------------------------
-- -- VINCOLI DI INTEGRITA GENERICI ------
---------------------------------------------------------------------------
-- Prima di creare un account per un abitante, controllo che il suo documento non sia scaduto
DROP TRIGGER IF EXISTS CheckDocument;
DELIMITER $$
CREATE TRIGGER CheckDocument
BEFORE INSERT ON Registrazione
FOR EACH ROW
BEGIN
-- prima vado a controllare la scadenza nella tabella Abitante
set @a =
(
select Scadenza
from Abitante
where CodFiscale = new.abitante
);
IF
(@a < current_date())
THEN
-- qui cancello l'account creato nella tabella User
delete
from User
where new.user = UserName;
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ATTENTION: DOCUMENTO SCADUTO';
END IF;
END $$
DELIMITER ;
-- verificare che un utente sia registrato prima di interagire con i elementi e dipositivi
DROP TRIGGER IF EXISTS CheckUser;
DELIMITER $$
CREATE TRIGGER CheckUser
BEFORE INSERT ON ConfigurazioniLuci
FOR EACH ROW
BEGIN
IF not exists
(
select *
from User U
where new.user = U.UserName
)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ATTENTION: UTENTE NON REGISTRATO';
END IF;
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS CheckUser;
DELIMITER $$
CREATE TRIGGER CheckUser
BEFORE INSERT ON StatoElemLuce
FOR EACH ROW
BEGIN
IF not exists
(
select *
from User U
where new.user = U.UserName
)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ATTENTION: UTENTE NON REGISTRATO';
END IF;
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS CheckUser;
DELIMITER $$
CREATE TRIGGER CheckUser
BEFORE INSERT ON SettingCond
FOR EACH ROW
BEGIN
IF not exists
(
select *
from User U
where new.user = U.UserName
)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ATTENTION: UTENTE NON REGISTRATO';
END IF;
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS CheckUser;
DELIMITER $$
CREATE TRIGGER CheckUser
BEFORE INSERT ON RecurrentSettingCond
FOR EACH ROW
BEGIN
IF not exists
(
select *
from User U
where new.user = U.UserName
)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ATTENTION: UTENTE NON REGISTRATO';
END IF;
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS CheckUser;
DELIMITER $$
CREATE TRIGGER CheckUser
BEFORE INSERT ON SetProgramDispositivi
FOR EACH ROW
BEGIN
IF not exists
(
select *
from User U
where new.user = U.UserName
)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ATTENTION: UTENTE NON REGISTRATO';
END IF;
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS CheckUser;
DELIMITER $$
CREATE TRIGGER CheckUser
BEFORE INSERT ON SettingDispositivi
FOR EACH ROW
BEGIN
IF not exists
(
select *
from User U
where new.user = U.UserName
)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ATTENTION: UTENTE NON REGISTRATO';
END IF;
END $$
DELIMITER ;
-- imposta stato smart plug quando viene collegato con un dispositivo --
drop trigger if exists OnSmartplug;
delimiter $$
create trigger OnSmartplug
after insert on collegamento
for each row
begin
update SmartPlug
set Stato = 'attivo'
where CodiceSmartplug = new.smartplug;
end $$
delimiter ;
-- non si può effettuare un'impostazione su un dispositivo con un programma che non è suo
DROP TRIGGER IF EXISTS checkProgam;
DELIMITER $$
CREATE TRIGGER checkProgam
before INSERT ON SetProgramDispositivi
FOR EACH ROW
BEGIN
if not exists
(
select 1
from Applicabilità A
where new.programma = A.programma and new.dispositivo = A.dispositivo
)
then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Programma non è di quel dispositivo';
end if;
END $$
DELIMITER ;
-- imposta la variabile EnergiaPerGrado di una Stanza --------
drop trigger if exists setEnergia;
delimiter $$
create trigger setEnergia
before insert on Stanza
for each row
begin
-- ipotesi coeffisciente termico pari a 35 kcal/mc
set @a = new.altezza * new.lunghezza * new.larghezza * 35; -- risultato in kcal devo convertere in KW con la formula 1 Kcal = 1,163 watt
set new.EnergiaPerGrado =( (@a * 1.163) / 1000 );
end $$
delimiter ;
-- calcolo dell'energia necessaria per mantenere una temperatura impostata in una stanza ----
drop trigger if exists ConsumoImpostazione;
delimiter $$
create trigger ConsumoImpostazione
before insert on SettingCond
for each row
begin
-- EnergiaPerGrado * |TemperaturaImpostata - TemperaturaInterna| + EnergiaPerGrado * (percentuale del livello di dispersione della temperatura interna)
-- ipotesi : 1 punto di accesso diminuisce di 5% la temperatura interna.
-- vado a ricavare il numero di Punti di accesso della stanza su cui si effettua l'impostazione
set @a =
(
select count(*)
from composizione C inner join presenza2 P on P.stanza = C.stanza
where P.ElementoAria = new.ElementoAria
);
-- leggo EnergiaPerGrado e la temperatura interna della stanza per fare variare di un grado la temperatura interna.
set @b =
(
select EnergiaPerGrado
from stanza S inner join presenza2 P on P.stanza = S.IDstanza
where new.ElementoAria = P.ElementoAria
);
set @TempInterna =
(
select SensoreInterno
from stanza S inner join presenza2 P on P.stanza = S.IDstanza
where new.ElementoAria = P.ElementoAria
);
-- ricavo la differanza di temperatura
if(@TempInterna >= new.Temperatura) then set@TempDiff = @TempInterna - new.Temperatura;
else set@TempDiff = new.Temperatura - @TempInterna;
end if;
set new.EnergiaTemperatura = (@TempDiff * @b) + (@a * 0.05) * @TempInterna;
end $$
delimiter ;
-- aggiorno la Temperatura interna della stanza ----
drop trigger if exists aggiornaTempStanza;
delimiter $$
create trigger aggiornaTempStanza
after insert on SettingCond
for each row
begin
update Stanza
set SensoreInterno = new.Temperatura
where IDstanza = (
select P.stanza
from presenza2 P
where new.ElementoAria = P.ElementoAria
);
end $$
delimiter ;
-- -------------------------------------------
-- AGGIORNAMENTO DELLE RIDONDANZE ------------
-- -------------------------------------------
-- aggiorna la ridondanza NumVolte -------
DROP TRIGGER IF EXISTS aggiorna1;
DELIMITER $$
CREATE TRIGGER aggiorna1
after INSERT ON ConfigurazioniLuci
FOR EACH ROW
BEGIN
update ElemLuce
set NumVolte = NumVolte + 1
where new.ElemLuci = IDelemLuci;
END $$
DELIMITER ;
-- aggiorna Conto --------------
DROP TRIGGER IF EXISTS aggiorna2;
DELIMITER $$
CREATE TRIGGER aggiorna2
after INSERT ON SettingDispositivi
FOR EACH ROW
BEGIN
update Dispositivi
set Conto = Conto + 1
where new.dispositivo = IDdispositivo;
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS aggiorna3;
DELIMITER $$
CREATE TRIGGER aggiorna3
after INSERT ON SetProgramDispositivi
FOR EACH ROW
BEGIN
update Dispositivi
set Conto = Conto + 1
where new.dispositivo = IDdispositivo;
END $$
DELIMITER ;
-- aggiorna ConsumoTotale
DROP TRIGGER IF EXISTS aggiorna4;
DELIMITER $$
CREATE TRIGGER aggiorna4
after INSERT ON SetProgramDispositivi
FOR EACH ROW
BEGIN
set @a =
(
select DurataProg * PotenzaProg
from Programmi
where new.Programma = NomeProg
);
update Dispositivi
set ConsumoTotale = ConsumoTotale + @a
where new.dispositivo = IDdispositivo;
END $$
DELIMITER ;
DROP TRIGGER IF EXISTS aggiorna5;
DELIMITER $$
CREATE TRIGGER aggiorna5
after INSERT ON SettingDispositivi
FOR EACH ROW
BEGIN
update Dispositivi
set ConsumoTotale = ConsumoTotale + (new.DurataSet * new.PotenzaSet)
where new.dispositivo = IDdispositivo;
END $$
DELIMITER ;