#--------------------------------------------------------------------------------# #-----------------------------Eliminar todas las tablas--------------------------# #--------------------------------------------------------------------------------# start transaction; set foreign_key_checks = 0; set autocommit = 0; drop table if exists editorial; drop table if exists estado; drop table if exists autor; drop table if exists categoria; drop table if exists idioma; drop table if exists libro; drop table if exists ejemplar; drop table if exists libro_autor; drop table if exists libro_categoria; drop table if exists libro_idioma; drop table if exists correo; drop table if exists direccion; drop table if exists telefono; drop table if exists empresa; drop table if exists distribuidor; drop table if exists cliente; drop table if exists trabajador; drop table if exists distribuidor_direccion; drop table if exists distribuidor_telefono; drop table if exists distribuidor_correo; drop table if exists cliente_direccion; drop table if exists cliente_telefono; drop table if exists cliente_correo; drop table if exists trabajador_direccion; drop table if exists trabajador_telefono; drop table if exists trabajador_correo; drop table if exists orden_compra; drop table if exists factura; drop table if exists boleta; drop table if exists compra; drop table if exists venta; drop table if exists arriendo; drop table if exists ejemplar_compra; drop table if exists ejemplar_venta; drop table if exists ejemplar_arriendo; drop table if exists usuario; #--------------------------------------------------------------------------------# #--------------Definicion de las tablas relacionadas a los libros----------------# #--------------------------------------------------------------------------------# create table editorial ( id int unsigned primary key auto_increment, nombre varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table estado ( id int unsigned primary key auto_increment, nombre varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table autor ( id int unsigned primary key auto_increment, nombre varchar(255) not null, apellido_paterno varchar(255) null, apellido_materno varchar(255) null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table categoria ( id int unsigned primary key auto_increment, nombre varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table idioma ( id int unsigned primary key auto_increment, nombre varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table libro ( id int unsigned primary key auto_increment, editorial_id int unsigned not null, isbn varchar(255) not null, ano_publicacion int default null, numero_paginas int not null, titulo varchar(255) default null, precio_referencia int not null, inserted_at timestamp default CURRENT_TIMESTAMP, foreign key (editorial_id) references editorial (id) on delete restrict on update cascade ); create table ejemplar ( id int unsigned primary key auto_increment, libro_id int unsigned not null, estado_id int unsigned default 1, serie varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP, unique key serie_libro (serie, libro_id), foreign key (libro_id) references libro (id) on delete cascade on update cascade, foreign key (estado_id) references estado (id) on delete restrict on update cascade ); create table libro_autor ( libro_id int unsigned not null, autor_id int unsigned not null, foreign key (libro_id) references libro (id) on delete restrict on update cascade, foreign key (autor_id) references autor (id) on delete restrict on update cascade ); create table libro_categoria ( libro_id int unsigned not null, categoria_id int unsigned not null, foreign key (libro_id) references libro (id) on delete restrict on update cascade, foreign key (categoria_id) references categoria (id) on delete restrict on update cascade ); create table libro_idioma ( libro_id int unsigned not null, idioma_id int unsigned not null, foreign key (libro_id) references libro (id) on delete restrict on update cascade, foreign key (idioma_id) references idioma (id) on delete restrict on update cascade ); #--------------------------------------------------------------------------------# #------------Definicion de las tablas relacionadas a las personas----------------# #--------------------------------------------------------------------------------# create table direccion ( id int unsigned primary key auto_increment, calle varchar(255) not null, numero varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table telefono ( id int unsigned primary key auto_increment, numero varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table correo ( id int unsigned primary key auto_increment, correo varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table empresa ( id int unsigned primary key auto_increment, nombre varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table distribuidor ( id int unsigned primary key auto_increment, empresa_id int unsigned not null, rut varchar(255) not null, inserted_at timestamp default CURRENT_TIMESTAMP, foreign key (empresa_id) references empresa (id) on delete restrict on update cascade ); create table cliente ( id int unsigned primary key auto_increment, rut varchar(255) not null, nombre varchar(255) not null, apellido_paterno varchar(255) not null, apellido_materno varchar(255) not null, fecha_nacimiento date not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table trabajador ( id int unsigned primary key auto_increment, rut varchar(255) not null, nombre varchar(255) not null, apellido_paterno varchar(255) not null, apellido_materno varchar(255) not null, fecha_contrato date not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table distribuidor_direccion ( distribuidor_id int unsigned, direccion_id int unsigned, foreign key (distribuidor_id) references cliente (id) on delete restrict on update cascade, foreign key (direccion_id) references direccion (id) on delete cascade on update cascade ); create table distribuidor_telefono ( distribuidor_id int unsigned, telefono_id int unsigned, foreign key (distribuidor_id) references cliente (id) on delete restrict on update cascade, foreign key (telefono_id) references telefono (id) on delete cascade on update cascade ); create table distribuidor_correo ( distribuidor_id int unsigned, correo_id int unsigned, foreign key (distribuidor_id) references cliente (id) on delete restrict on update cascade, foreign key (correo_id) references correo (id) on delete cascade on update cascade ); create table cliente_direccion ( cliente_id int unsigned, direccion_id int unsigned, foreign key (cliente_id) references cliente (id) on delete restrict on update cascade, foreign key (direccion_id) references direccion (id) on delete cascade on update cascade ); create table cliente_telefono ( cliente_id int unsigned, telefono_id int unsigned, foreign key (cliente_id) references cliente (id) on delete restrict on update cascade, foreign key (telefono_id) references telefono (id) on delete cascade on update cascade ); create table cliente_correo ( cliente_id int unsigned, correo_id int unsigned, foreign key (cliente_id) references cliente (id) on delete restrict on update cascade, foreign key (correo_id) references correo (id) on delete cascade on update cascade ); create table trabajador_direccion ( trabajador_id int unsigned, direccion_id int unsigned, foreign key (trabajador_id) references trabajador (id) on delete restrict on update cascade, foreign key (direccion_id) references direccion (id) on delete cascade on update cascade ); create table trabajador_telefono ( trabajador_id int unsigned, telefono_id int unsigned, foreign key (trabajador_id) references trabajador (id) on delete restrict on update cascade, foreign key (telefono_id) references telefono (id) on delete cascade on update cascade ); create table trabajador_correo ( trabajador_id int unsigned, correo_id int unsigned, foreign key (trabajador_id) references trabajador (id) on delete restrict on update cascade, foreign key (correo_id) references correo (id) on delete cascade on update cascade ); #--------------------------------------------------------------------------------# #-------Definicion de las tablas relacionadas a la venta/compra/arriendo---------# #--------------------------------------------------------------------------------# create table factura ( id int unsigned primary key auto_increment, folio varchar(255) not null, precio_neto int not null, precio_iva int not null, fecha_compra datetime not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table boleta ( id int unsigned primary key auto_increment, folio varchar(255) not null, precio_neto int not null, precio_iva int not null, fecha_venta datetime not null, inserted_at timestamp default CURRENT_TIMESTAMP ); create table orden_compra ( id int unsigned primary key auto_increment, estado enum ('En Curso', 'Aceptada', 'Cancelada'), compra_id int unsigned, distribuidor_id int unsigned not null, inserted_at timestamp default CURRENT_TIMESTAMP, foreign key (distribuidor_id) references distribuidor (id) on delete restrict on update cascade, foreign key (compra_id) references compra (id) on delete restrict on update cascade ); create table compra ( id int unsigned primary key auto_increment, factura_id int unsigned not null, distribuidor_id int unsigned not null, inserted_at timestamp default CURRENT_TIMESTAMP, foreign key (factura_id) references factura (id) on delete restrict on update cascade, foreign key (distribuidor_id) references cliente (id) on delete restrict on update cascade ); create table venta ( id int unsigned primary key auto_increment, cliente_id int unsigned not null, trabajador_id int unsigned not null, boleta_id int unsigned not null, inserted_at timestamp default CURRENT_TIMESTAMP, foreign key (cliente_id) references cliente (id) on delete restrict on update cascade, foreign key (trabajador_id) references trabajador (id) on delete restrict on update cascade, foreign key (boleta_id) references boleta (id) on delete restrict on update cascade ); create table arriendo ( id int unsigned primary key auto_increment, trabajador_id int unsigned not null, cliente_id int unsigned not null, costo_arriendo int not null, fecha_arriendo date not null, fecha_devolucion_estimada date not null, fecha_devolucion_real date, multa int, inserted_at timestamp default CURRENT_TIMESTAMP, foreign key (trabajador_id) references trabajador (id) on delete restrict on update cascade, foreign key (cliente_id) references cliente (id) on delete restrict on update cascade ); create table ejemplar_compra ( ejemplar_id int unsigned, compra_id int unsigned, foreign key (ejemplar_id) references ejemplar (id) on delete restrict on update cascade, foreign key (compra_id) references compra (id) on delete restrict on update cascade ); create table ejemplar_venta ( ejemplar_id int unsigned, venta_id int unsigned, foreign key (ejemplar_id) references ejemplar (id) on delete restrict on update cascade, foreign key (venta_id) references venta (id) on delete restrict on update cascade ); create table ejemplar_arriendo ( ejemplar_id int unsigned, arriendo_id int unsigned, foreign key (ejemplar_id) references ejemplar (id) on delete restrict on update cascade, foreign key (arriendo_id) references arriendo (id) on delete restrict on update cascade ); #--------------------------------------------------------------------------------# #---------------------Definicion de las tablas de usuario------------------------# #--------------------------------------------------------------------------------# create table usuario ( id int unsigned primary key auto_increment, nombre varchar(255) not null, password binary(32) not null, salt binary(16) not null, trabajador_id int unsigned not null, inserted_at timestamp default current_timestamp, foreign key (trabajador_id) references trabajador (id) on delete cascade on update cascade ); set autocommit = 1; set foreign_key_checks = 1; commit;start transaction; set foreign_key_checks = 0; set autocommit = 0; INSERT INTO `autor` VALUES (1, 'Howard Philips', 'Lovecraft', NULL, '2019-06-12 16:18:57'), (2, 'Stephen', 'King', NULL, '2019-06-12 16:18:57'), (3, 'Brandon', 'Sanderson', NULL, '2019-06-12 16:18:57'); INSERT INTO `categoria` VALUES (1, 'Terror', '2019-06-12 16:18:57'), (2, 'Aventura', '2019-06-12 16:18:57'), (3, 'Fantasia', '2019-06-12 16:18:57'); -- INSERT INTO `cliente` VALUES (1, '21786653-7', 'Cliente', '1', '1', '2019-06-17', '2019-06-13 16:01:33'), (2, '17181388-3', 'Cliente', '2', '2', '2019-06-13', '2019-06-13 16:04:02'); INSERT INTO `correo` VALUES (1, 'HOOLA', '2019-06-13 17:25:41'), (2, 'skrd159@gmail.com', '2019-06-13 17:32:13'); INSERT INTO `distribuidor` VALUES (2, 1, '14166920-6', '2019-06-13 03:08:17'), (3, 2, '8425080-5', '2019-06-13 03:08:23'), (4, 1, '21629388-6', '2019-06-13 03:08:25'), (5, 1, '13510176-1', '2019-06-13 03:08:27'); INSERT INTO `editorial` VALUES (1, 'Editorial N1', '2019-06-12 16:18:57'), (2, 'Editorial N2', '2019-06-12 16:18:57'), (3, 'Editorial N3', '2019-06-12 16:18:57'); INSERT INTO `empresa` VALUES (1, 'Empresa 1', '2019-06-13 00:39:50'), (2, 'Empresa 2', '2019-06-13 00:39:52'); INSERT INTO `estado` VALUES (1, 'Vendido', '2019-06-12 16:18:57'), (2, 'Arrendado', '2019-06-12 16:18:57'), (3, 'Disponible', '2019-06-12 16:18:57'); INSERT INTO `idioma` VALUES (1, 'EspaƱol', '2019-06-12 16:18:57'), (2, 'Ingles', '2019-06-12 16:18:57'), (3, 'Portuges', '2019-06-12 16:18:57'), (4, 'Aleman', '2019-06-12 16:18:57'), (5, 'Ruso', '2019-06-12 16:18:57'), (6, 'Japones', '2019-06-12 16:18:57'); INSERT INTO `libro` VALUES (1, 1, '0-765-31178-X', 2006, 541, 'Mistborn: The Final Empire', 10000, '2019-06-12 16:18:57'), (2, 1, '0-765-31688-9', 2007, 590, 'Mistborn: The Well of Ascension', 10000, '2019-06-12 16:18:57'), (3, 1, '978-0-7653-1689-9', 2008, 572, 'Mistborn: The Hero of Ages', 10000, '2019-06-12 16:18:57'), (4, 2, '1231231231232', 1931, 100, 'The Shadow over Innsmouth', 10000, '2019-06-12 16:18:57'), (5, 2, '1231231231232', 1933, 100, 'The Dreams in the Witch House', 10000, '2019-06-12 16:18:57'), (6, 2, '1231231231232', 1936, 100, 'At the Mountains of Madness', 10000, '2019-06-12 16:18:57'), (7, 3, '0385086954', 1974, 199, 'Carrie', 10000, '2019-06-12 16:18:57'), (8, 3, '978-0-670-26077-5', 1979, 428, 'The Dead Zone', 10000, '2019-06-12 16:18:57'), (9, 3, '0-670-81302-8', 1986, 1138, 'It', 10000, '2019-06-12 16:18:57'); INSERT INTO `libro_autor` VALUES (1, 3), (2, 3), (3, 3), (4, 1), (5, 1), (6, 1), (7, 2), (8, 2), (9, 2); INSERT INTO `libro_categoria` VALUES (1, 3), (2, 3), (3, 3), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1); INSERT INTO `libro_idioma` VALUES (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2), (9, 2); INSERT INTO `trabajador` VALUES (2, '19763899-0', 'Daniel', 'Cortes', 'Pincheira', '2019-06-13', '2019-06-13 16:04:42'), (3, '10768789-0', 'Trabajador', 'N', '2', '2019-06-13', '2019-06-13 16:05:17'), (4, '9717478-4', 'Trabajador', 'N', '3', '2019-06-13', '2019-06-13 16:05:27'); INSERT INTO `trabajador_correo` VALUES (4, 1), (2, 2); INSERT INTO `usuario` VALUES (2, 'admin', 0x243168097E0BA82B896F348BABCEB600A8DCA30488C6F238F97FD8737BD00B27, 0x3564ECCCD85CF0583F9C090602E998B7, 2, '2019-06-13 16:04:53'); set autocommit = 1; set foreign_key_checks = 1; commit;