0% found this document useful (0 votes)
20 views

SQL

The document contains SQL statements to create multiple tables for a database including tables for devices, device groups, users, locations, and more. It also includes statements to create foreign key constraints to link the tables together and sample insert statements to add initial data to the tables.

Uploaded by

J4gu4r Rojo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

SQL

The document contains SQL statements to create multiple tables for a database including tables for devices, device groups, users, locations, and more. It also includes statements to create foreign key constraints to link the tables together and sample insert statements to add initial data to the tables.

Uploaded by

J4gu4r Rojo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//apiKey=AIzaSyA8wBWFPVN1kdOaJgXeHNXCknxSb0hoLKs

CREATE TABLE IF NOT EXISTS `device_group` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE IF NOT EXISTS `user_group_assign` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`device_group_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `device_group_id` (`device_group_id`),
CONSTRAINT `user_group_assign_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users`
(`id`),
CONSTRAINT `user_group_assign_ibfk_2` FOREIGN KEY (`device_group_id`) REFERENCES
`device_group` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `devices` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`external_supply` int(1) NOT NULL,
`last_id` int(11) NOT NULL,
`state` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `location_report` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`device_id` int(11) NOT NULL,
`speed` float NOT NULL,
`latitude` decimal(10,6) NOT NULL,
`longitude` decimal(10,6) NOT NULL,
`utc` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `device_id` (`device_id`),
CONSTRAINT `location_report_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES
`devices` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `geofences` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`vertices` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `geofence_user` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`geofence_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `geofence_id` (`geofence_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `geofence_user_ibfk_1` FOREIGN KEY (`geofence_id`) REFERENCES
`geofences` (`id`),
CONSTRAINT `geofence_user_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users`
(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Creación de la tabla 'conductores'


CREATE TABLE IF NOT EXISTS `conductores` (
`id` int(11) NOT NULL,
`paterno` varchar(255) DEFAULT NULL,
`materno` varchar(255) DEFAULT NULL,
`nombres` varchar(255) DEFAULT NULL,
`rut` varchar(255) DEFAULT NULL,
`dv` char(1) DEFAULT NULL,
`estado` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'coordinate_address'


CREATE TABLE IF NOT EXISTS `coordinate_address` (
`id` int(11) NOT NULL,
`latitude` decimal(10,6) DEFAULT NULL,
`longitude` decimal(10,6) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'devices'


CREATE TABLE IF NOT EXISTS `devices` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`external_supply` int(1) NOT NULL,
`last_id` int(11) NOT NULL,
`state` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'device_group'


CREATE TABLE IF NOT EXISTS `device_group` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`device_group_count` int(11) UNSIGNED NOT NULL DEFAULT 0,
`user_group_count` int(11) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'device_group_assign'


CREATE TABLE IF NOT EXISTS `device_group_assign` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`device_group_id` int(11) UNSIGNED NOT NULL,
`device_id` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`device_group_id`) REFERENCES `device_group` (`id`),
FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'device_info'


CREATE TABLE IF NOT EXISTS `device_info` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`utc` datetime NOT NULL,
`device_id` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'geofences'


CREATE TABLE IF NOT EXISTS `geofences` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`tipo` varchar(255) DEFAULT NULL,
`vertices` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'geofence_device'


CREATE TABLE IF NOT EXISTS `geofence_device` (
`id` int(11) NOT NULL,
`geofence_id` int(11) DEFAULT NULL,
`device_id` int(11) DEFAULT NULL,
`fecha_inicio` date DEFAULT NULL,
`fecha_termino` date DEFAULT NULL,
`conductor_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_geofence` (`geofence_id`),
KEY `fk_device` (`device_id`),
KEY `fk_conductor` (`conductor_id`),
FOREIGN KEY (`geofence_id`) REFERENCES `geofences` (`id`),
FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`),
FOREIGN KEY (`conductor_id`) REFERENCES `conductores` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'geofence_user'


CREATE TABLE IF NOT EXISTS `geofence_user` (
`id` int(11) NOT NULL,
`geofence_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `geofence_id` (`geofence_id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`geofence_id`) REFERENCES `geofences` (`id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'location_report'


CREATE TABLE IF NOT EXISTS `location_report` (
`id` int(11) NOT NULL,
`device_id` int(11) NOT NULL,
`speed` float NOT NULL,
`latitude` decimal(10,6) NOT NULL,
`longitude` decimal(10,6) NOT NULL,
`utc` datetime NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'users'


CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`hashedpassword` varchar(255) NOT NULL,
`admin` int(11) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'user_group'


CREATE TABLE IF NOT EXISTS `user_group` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`user_group_count` int(11) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación de la tabla 'device_group_assign'


CREATE TABLE IF NOT EXISTS `device_group_assign` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`device_group_id` int(11) UNSIGNED NOT NULL,
`device_id` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_device_group` (`device_group_id`),
INDEX `fk_device` (`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

-- Creación del índice y la relación con la tabla 'device_group'


ALTER TABLE `device_group_assign`
ADD CONSTRAINT `fk_device_group`
FOREIGN KEY (`device_group_id`) REFERENCES `device_group` (`id`);

-- Creación del índice y la relación con la tabla 'devices'


ALTER TABLE `device_group_assign`
ADD CONSTRAINT `fk_device`
FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`);

-- Insertar datos en la tabla 'conductores'


INSERT INTO `conductores` (`id`, `paterno`, `materno`, `nombres`, `rut`, `dv`,
`estado`)
VALUES (1, 'Pérez', 'González', 'Juan', '12345678', 'K', 'Activo');

-- Insertar datos en la tabla 'coordinate_address'


INSERT INTO `coordinate_address` (`id`, `latitude`, `longitude`, `address`)
VALUES (1, 40.7128, -74.0060, 'New York City');

-- Insertar datos en la tabla 'devices'


INSERT INTO `devices` (`id`, `name`, `external_supply`, `last_id`, `state`)
VALUES (1, 'Device1', 0, 100, 'Active');

-- Insertar datos en la tabla 'device_group'


INSERT INTO `device_group` (`id`, `name`, `device_group_count`, `user_group_count`)
VALUES (1, 'Group1', 5, 3);

-- Insertar datos en la tabla 'device_group_assign'


INSERT INTO `device_group_assign` (`id`, `device_group_id`, `device_id`)
VALUES (1, 1, 1);

-- Insertar datos en la tabla 'device_info'


INSERT INTO `device_info` (`id`, `utc`, `device_id`)
VALUES (1, '2023-06-29 10:00:00', 1);

-- Insertar datos en la tabla 'geofences'


INSERT INTO `geofences` (`id`, `name`, `tipo`, `vertices`)
VALUES (1, 'Geofence1', 'Circle', '10,20;30,40;50,60');

-- Insertar datos en la tabla 'geofence_device'


INSERT INTO `geofence_device` (`id`, `geofence_id`, `device_id`, `fecha_inicio`,
`fecha_termino`, `conductor_id`)
VALUES (1, 1, 1, '2023-06-29', '2023-06-30', 1);

-- Insertar datos en la tabla 'geofence_user'


INSERT INTO `geofence_user` (`id`, `geofence_id`, `user_id`)
VALUES (1, 1, 1);

-- Insertar datos en la tabla 'location_report'


INSERT INTO `location_report` (`id`, `device_id`, `speed`, `latitude`, `longitude`,
`utc`)
VALUES (1, 1, 60.5, 40.7128, -74.0060, '2023-06-29 10:00:00');

-- Insertar datos en la tabla 'users'


INSERT INTO `users` (`id`, `name`, `hashedpassword`,`admin`)
VALUES (1, 'Hozkar', 'b8fb64057a7ce17f05b0187c0d6fee29,1');

You might also like