-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_device_db.sql
More file actions
35 lines (31 loc) · 1.06 KB
/
init_device_db.sql
File metadata and controls
35 lines (31 loc) · 1.06 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
-- Create a database user
CREATE USER IF NOT EXISTS 'app_user'@'%' IDENTIFIED BY '$MARIADB_APP_PASSWORD';
GRANT ALL PRIVILEGES ON device_management.* TO 'app_user'@'%';
--GRANT ALL ON *.* TO 'app_user'@'%';
FLUSH PRIVILEGES;
-- Create the 'devices' table
CREATE TABLE IF NOT EXISTS devices
(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
status VARCHAR(255),
location VARCHAR(255)
);
-- Create the 'user_device' association table
-- Ensure that if a device is deleted, its associations are automatically removed from the table
CREATE TABLE IF NOT EXISTS user_device
(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT NOT NULL,
device_id BIGINT NOT NULL,
FOREIGN KEY (device_id) REFERENCES devices(id) ON DELETE CASCADE
);
-- Insert some initial data
INSERT INTO devices (name, status, location)
VALUES ('Thermostat', 'active', 'Living Room'),
('Light Switch', 'inactive', 'Bedroom'),
('Smart Bulb', 'active', 'Bedroom');
INSERT INTO user_device (user_id, device_id)
VALUES (1, 1),
(2, 1),
(3, 1);