313 lines
10 KiB
SQL
313 lines
10 KiB
SQL
#--------------------------------------------------------------------------------#
|
|
#-----------------------------Eliminar todas las tablas--------------------------#
|
|
#--------------------------------------------------------------------------------#
|
|
set foreign_key_checks = 0;
|
|
drop table if exists libro_arriendo;
|
|
drop table if exists libro_venta;
|
|
drop table if exists libro_compra;
|
|
drop table if exists arriendo;
|
|
drop table if exists venta;
|
|
drop table if exists compra;
|
|
drop table if exists boleta;
|
|
drop table if exists factura;
|
|
drop table if exists trabajador_telefono;
|
|
drop table if exists trabajador_direccion;
|
|
drop table if exists cliente_telefono;
|
|
drop table if exists cliente_direccion;
|
|
drop table if exists distribuidor_telefono;
|
|
drop table if exists distribuidor_direccion;
|
|
drop table if exists trabajador;
|
|
drop table if exists cliente;
|
|
drop table if exists distribuidor;
|
|
drop table if exists empresa;
|
|
drop table if exists telefono;
|
|
drop table if exists direccion;
|
|
drop table if exists libro_idioma;
|
|
drop table if exists libro_categoria;
|
|
drop table if exists libro_autor;
|
|
drop table if exists libro;
|
|
drop table if exists editorial;
|
|
drop table if exists idioma;
|
|
drop table if exists categoria;
|
|
drop table if exists autor;
|
|
drop table if exists estado;
|
|
set foreign_key_checks = 1;
|
|
|
|
|
|
#--------------------------------------------------------------------------------#
|
|
#--------------Definicion de las tablas relacionadas a los libros----------------#
|
|
#--------------------------------------------------------------------------------#
|
|
create table editorial
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
nombre varchar(255) not null
|
|
);
|
|
|
|
create table estado
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
nombre varchar(255) not null
|
|
);
|
|
|
|
create table autor
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
nombre varchar(255) not null,
|
|
apellido_paterno varchar(255) not null,
|
|
apellido_materno varchar(255) not null
|
|
);
|
|
|
|
create table categoria
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
nombre varchar(255) not null
|
|
);
|
|
|
|
create table idioma
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
nombre varchar(255) not null
|
|
);
|
|
|
|
create table libro
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
serie varchar(255) unique not null,
|
|
isbn varchar(255) not null,
|
|
titulo varchar(255) default null,
|
|
numero_paginas int not null,
|
|
precio_referencia int not null,
|
|
ano_publicacion int default null,
|
|
editorial_id int unsigned not null,
|
|
estado_id int unsigned default 1,
|
|
foreign key (editorial_id) references editorial (id) on delete restrict 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
|
|
);
|
|
|
|
create table telefono
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
numero varchar(255) not null
|
|
);
|
|
|
|
create table empresa
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
nombre varchar(255) not null
|
|
);
|
|
|
|
create table distribuidor
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
rut varchar(255) not null,
|
|
empresa_id int unsigned not null,
|
|
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
|
|
);
|
|
|
|
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
|
|
);
|
|
|
|
create table distribuidor_direccion
|
|
(
|
|
distribuidor_id int unsigned,
|
|
direccion_id int unsigned,
|
|
foreign key (distribuidor_id) references distribuidor (id) on delete restrict on update cascade,
|
|
foreign key (direccion_id) references direccion (id) on delete restrict on update cascade
|
|
);
|
|
|
|
create table distribuidor_telefono
|
|
(
|
|
distribuidor_id int unsigned,
|
|
telefono_id int unsigned,
|
|
foreign key (distribuidor_id) references distribuidor (id) on delete restrict on update cascade,
|
|
foreign key (telefono_id) references telefono (id) on delete restrict 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 restrict 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 restrict 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 restrict 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 restrict 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,
|
|
costo_iva int not null,
|
|
fecha_compra datetime not null
|
|
);
|
|
|
|
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,
|
|
costo_iva int not null,
|
|
fecha_venta datetime not null
|
|
);
|
|
|
|
create table compra
|
|
(
|
|
id int unsigned primary key auto_increment,
|
|
factura_id int unsigned not null,
|
|
distribuidor_id int unsigned not null,
|
|
foreign key (factura_id) references factura (id) on delete restrict on update cascade,
|
|
foreign key (distribuidor_id) references distribuidor (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,
|
|
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,
|
|
costo_arriendo int not null,
|
|
multa int,
|
|
costo_total int,
|
|
fecha_arriendo date not null,
|
|
fecha_devolucion_estimada date not null,
|
|
fecha_devolucion_real date,
|
|
trabajador_id int unsigned not null,
|
|
cliente_id int unsigned not null
|
|
);
|
|
|
|
create table libro_compra
|
|
(
|
|
libro_id int unsigned,
|
|
compra_id int unsigned,
|
|
foreign key (libro_id) references libro (id) on delete restrict on update cascade,
|
|
foreign key (compra_id) references compra (id) on delete restrict on update cascade
|
|
);
|
|
|
|
create table libro_venta
|
|
(
|
|
libro_id int unsigned,
|
|
venta_id int unsigned,
|
|
foreign key (libro_id) references libro (id) on delete restrict on update cascade,
|
|
foreign key (venta_id) references venta (id) on delete restrict on update cascade
|
|
);
|
|
|
|
create table libro_arriendo
|
|
(
|
|
libro_id int unsigned,
|
|
arriendo_id int unsigned,
|
|
foreign key (libro_id) references libro (id) on delete restrict on update cascade,
|
|
foreign key (arriendo_id) references arriendo (id) on delete restrict on update cascade
|
|
);
|
|
|
|
#--------------------------------------------------------------------------------#
|
|
#------------------------Poblar con datos iniciales------------------------------#
|
|
#--------------------------------------------------------------------------------#
|
|
|
|
insert into estado (nombre)
|
|
values ('Vendido'),
|
|
('Arrendado'),
|
|
('Disponible');
|
|
|
|
insert into idioma (nombre)
|
|
values ('Español'),
|
|
('Ingles'),
|
|
('Portuges'),
|
|
('Aleman'),
|
|
('Ruso'),
|
|
('Japones');
|
|
|
|
insert into editorial (nombre)
|
|
values ('The Pragmatic Bookshelf'),
|
|
('O\'Reilly Media'),
|
|
('Manning Publications');
|
|
|