-
Notifications
You must be signed in to change notification settings - Fork 4
/
SymIntIoTSystemDBCreator.sql
67 lines (59 loc) · 1.78 KB
/
SymIntIoTSystemDBCreator.sql
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
create database symintiotsystem;
use symintiotsystem;
SET @@session.time_zone='+00:00';
SET SQL_SAFE_UPDATES=0;
create table devices
(Id int not null auto_increment primary key,
Mac varchar(45) not null,
Description varchar(42),
Created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Updated TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP);
create table times
(Id int not null auto_increment primary key,
Time datetime);
create table temperatures
(Id int not null auto_increment primary key,
DeviceId int not null,
Temperature_C float,
TimeId int,
foreign key (DeviceId) references devices(Id),
foreign key (TimeId) references times(Id));
create table humiditys
(Id int not null auto_increment primary key,
DeviceId int not null,
Humidity_pct float,
TimeId int,
foreign key (DeviceId) references devices(Id),
foreign key (TimeId) references times(Id));
create table light
(Id int not null auto_increment primary key,
DeviceId int not null,
Lux int,
TimeId int,
foreign key (DeviceId) references devices(Id),
foreign key (TimeId) references times(Id));
create table radiation
(Id int not null auto_increment primary key,
DeviceId int not null,
Siverts_uSv float,
TimeId int,
foreign key (DeviceId) references devices(Id),
foreign key (TimeId) references times(Id));
delimiter $$
create procedure select_or_insert(IN inMac varchar(45), IN inDescription varchar(42))
begin
IF EXISTS (SELECT * FROM devices WHERE devices.Mac = inMac) THEN
UPDATE devices SET Description = inDescription WHERE devices.Mac = inMac;
ELSE
INSERT INTO devices(Mac, Description) VALUES (inMac, inDescription);
END IF;
end $$
delimiter ;
delimiter $$
create procedure add_time(IN inTime DATETIME)
begin
IF NOT EXISTS (SELECT * FROM times WHERE times.Time = inTime) THEN
INSERT INTO times(Time) VALUES (inTime);
END IF;
end $$
delimiter ;