diff --git a/doc/Informe1.pdf b/doc/Informe1.pdf index 0e29606..673e603 100644 Binary files a/doc/Informe1.pdf and b/doc/Informe1.pdf differ diff --git a/doc/Informe1.tex b/doc/Informe1.tex index c988f46..703a800 100644 --- a/doc/Informe1.tex +++ b/doc/Informe1.tex @@ -1,14 +1,29 @@ \documentclass[12pt]{article} -\usepackage[utf8]{inputenc} %\ This allows spanish tildes +\usepackage[utf8]{inputenc} \usepackage[spanish]{babel} \usepackage{array} \usepackage{adjustbox} -\usepackage{titling} \usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm,a4paper]{geometry} \usepackage{amsmath} -\title{\textbf{Proyecto Semestral de Base de Datos}} +\usepackage{verbatim} +\usepackage{listings} +\lstset{ + basicstyle=\footnotesize\ttfamily, + columns=flexible, + breaklines=true +} + +\usepackage{titling} +\renewcommand\maketitlehooka{\null\mbox{}\vfill} +\renewcommand\maketitlehookd{\vfill\null} + +\usepackage{hyperref} +\hypersetup{pdftex,colorlinks=true,allcolors=black,bookmarks} +\usepackage{hypcap} + +\title{\textbf{Proyecto Semestral de Base de Datos\\Entrega 1}} \author{Christopher Cromer\\\\Profesora Valeria Beratto\\Sección 1} \date{\today} @@ -21,7 +36,10 @@ \posttitle{\end{center}} \begin{document} -\clearpage\maketitle +\clearpage +\begin{titlingpage} +\maketitle +\end{titlingpage} \thispagestyle{empty} \newpage @@ -32,15 +50,97 @@ \newpage \pagenumbering{arabic} - \section{Introducción} -El objetivo de este trabajo es modelar un sistema por el Servicio Nacional de Turismo(SERNATUR) con el propósito de almacenar y consultar a un base de datos por información sobre el turismo. +El objetivo de este trabajo es modelar un sistema por el Servicio Nacional de Turismo(SERNATUR) con el propósito de almacenar y consultar a un base de datos de PostgreSQL con información sobre el turismo. -\section{MER} +\newpage +\section{Modelo Entidad Relación} +\begin{center} +\includegraphics[width=0.96\textwidth,height=0.96\textheight,keepaspectratio]{MER.png} +\end{center} -\section{MR} +\newpage +\section{Modelo Relacional} +\begin{center} +En negrita se presenta las claves foráneas y las primarias en subrayado.\\ +\end{center} +tour(\underline{id\_tour}, nombre\_tour, costo\_indiv, costo\_grupal, minima\_personas, \textbf{id\_ciudad})\\ +\newline +lugar(\underline{id\_lugar}, nombre\_lugar, valor\_entrada, nivel, \textbf{id\_ciudad})\\ +\newline +asociado(\textbf{id\_tour}, \textbf{id\_lugar}, fecha\_llegada, hora\_llegada, fecha\_salida, hora\_salida)\\ +\newline +turista(\underline{rut\_turista}, nombre\_turista, fecha\_nacimiento, \textbf{id\_contacto})\\ +\newline +contacto\_emergencia(\underline{id\_contacto}, telefono\_emergencia, nombre\_emergencia)\\ +\newline +enfermedad(\underline{id\_enfermedad}, descripcion\_enfermedad)\\ +\newline +tiene\_enfermedad(\textbf{rut\_turista}, \textbf{id\_enfermedad})\\ +\newline +compra(\textbf{id\_tour}, \textbf{rut\_turista}, valor)\\ +\newline +realiza(\textbf{id\_tour}, \textbf{rut\_turista})\\ +\newline +vehiculo(\underline{patente}, ano\_vehiculo, marca, capacidad, chofer)\\ +\newline +empresa(\underline{rut\_empresa}, nombre\_empresa, contacto, telefono)\\ +\newline +arrienda(\textbf{patente}, \textbf{rut\_empresa}, precio, fecha\_devolucion)\\ +\newline +ciudad(\underline{id\_ciudad}, nombre\_ciudad, \textbf{id\_region})\\ +\newline +region(\underline{id\_region}, nombre\_region)\\ +\newline +guia(\underline{rut\_guia}, nombre\_guia)\\ +\newline +especialidad(\underline{id\_especialidad}, descripcion\_especialidad)\\ +\newline +posee(\textbf{rut\_guia}, \textbf{id\_especialidad}, nivel\_especialidad)\\ +\newline +categoria(\underline{id\_categoria}, descripcion\_categoria)\\ +\newline +direccion(\textbf{rut\_guia}, \textbf{id\_ciudad}, calle, numero)\\ +\newline +participa(\textbf{id\_tour}, \textbf{rut\_guia}, \textbf{id\_categoria}) -\section{SQL} +\newpage +\section{Justificación de Modelo Relacional} +\textbf{\textit{tour}} adquiere la clave foránea \textbf{\textit{id\_ciudad}} debido que la relación entre las entidades \textbf{\textit{tour}} y \textbf{\textit{ciudad}} es de \textbf{\textit{n a 1}}.\\ + +\textbf{\textit{lugar}} recibe como clave foránea \textbf{\textit{id\_ciudad}} dado por la relación \textbf{\textit{n a 1}} de las entidades \textbf{\textit{lugar}} y \textbf{\textit{ciudad}}.\\ + +\textbf{\textit{asociado}} es creado con las claves foráneas \textbf{\textit{id\_tour}} y \textbf{\textit{id\_lugar}} dado que la relación entre \textbf{\textit{tour}} y \textbf{\textit{lugar}} es de \textbf{\textit{n a n}}.\\ + +\textbf{\textit{turista}} adquiere la clave foránea \textbf{\textit{id\_contacto}} debido que la relación entre \textbf{\textit{turista}} y \textbf{\textit{contacto\_emergencia}} es \textbf{\textit{n a 1}}.\\ + +\textbf{\textit{tiene\_enfermedad}} es creado con las claves foráneas \textbf{\textit{rut\_turista}} y \textbf{\textit{id\_enfermedad}} dado que la relación es \textbf{\textit{n a n}} entre \textbf{\textit{turista}} y \textbf{\textit{enfermedad}}.\\ + +\textit{\textbf{compra}} es creado con las claves foráneas \textbf{\textit{id\_tour}} y \textbf{\textit{rut\_turista}} dado que la relación entre \textbf{\textit{tour}} y \textbf{\textit{turista}} es de \textbf{\textit{n a n}}.\\ + +\textbf{\textit{realiza}} es creado con las claves foráneas \textbf{\textit{id\_tour}} y \textbf{\textit{rut\_turista}} dado que la relación entre \textbf{\textit{tour}} y \textbf{\textit{turista}} es de \textbf{\textit{n a n}}.\\ + +\textbf{\textit{arrienda}} es creado con las claves foráneas \textbf{\textit{patente}} y \textbf{\textit{rut\_empresa}} dado que la relación entre \textbf{\textit{vehiculo}} y \textbf{\textit{empresa}} es de \textbf{\textit{n a n}}.\\ + +\textbf{\textit{ciudad}} adquiere la clave foránea \textbf{\textit{id\_region}} debido a la relación \textbf{\textit{n a 1}} entre las entidades \textbf{\textit{ciudad}} y \textbf{\textit{region}}.\\ + +\textbf{\textit{posee}} es creado y recibe las claves foráneas \textbf{\textit{rut\_guia}} y \textbf{\textit{id\_especialidad}} dado que la relación entre \textbf{\textit{guia}} y \textbf{\textit{especialidad}} es de \textbf{\textit{n a n}}.\\ + +\textbf{\textit{direccion}} es creado con las claves foráneas \textbf{\textit{rut\_guia}} y \textbf{\textit{id\_ciudad}} dado por la relación \textbf{\textit{n a n}} entre \textbf{\textit{ciudad}} y \textbf{\textit{guia}}.\\ + +\textbf{\textit{participa}} es creado con 3 claves foráneas \textbf{\textit{id\_tour}}, \textbf{\textit{rut\_guia}} y \textbf{\textit{id\_categoria}} dado que la relación es \textbf{\textit{n-arias}} entre \textbf{\textit{tour}}, \textbf{\textit{guia}} y \textbf{\textit{categoria}}.\\ + +\newpage +\section{Script SQL} + +\subsection{Crear tablas} + +\lstinputlisting{Informecreartablas.sql} + +\newpage +\subsection{Insertar duplas} + +\lstinputlisting{Informeinsertarduplas.sql} \end{document} diff --git a/doc/Informecreartablas.sql b/doc/Informecreartablas.sql new file mode 100644 index 0000000..2a66d97 --- /dev/null +++ b/doc/Informecreartablas.sql @@ -0,0 +1,139 @@ +CREATE TABLE contacto_emergencia(id_contacto SERIAL UNIQUE, + telefono_emergencia BIGINT NOT NULL CHECK (telefono_emergencia > 99999999 AND telefono_emergencia < 100000000000), + nombre_emergencia VARCHAR(50) NOT NULL, + PRIMARY KEY (id_contacto) +); + +CREATE TABLE enfermedad(id_enfermedad SMALLSERIAL UNIQUE, + descripcion_enfermedad TEXT NOT NULL, + PRIMARY KEY (id_enfermedad) +); + +CREATE TABLE empresa(rut_empresa VARCHAR(9) NOT NULL UNIQUE, + nombre_empresa VARCHAR(50) NOT NULL, + contacto VARCHAR(50) NOT NULL, + telefono BIGINT NOT NULL CHECK (telefono > 99999999 AND telefono < 100000000000), + PRIMARY KEY (rut_empresa) +); + +CREATE TABLE guia(rut_guia VARCHAR(9) NOT NULL UNIQUE, + nombre_guia VARCHAR(50) NOT NULL, + PRIMARY KEY (rut_guia) +); + +CREATE TABLE especialidad(id_especialidad SMALLSERIAL UNIQUE, + descripcion_especialidad TEXT NOT NULL, + PRIMARY KEY (id_especialidad) +); + +CREATE TABLE categoria(id_categoria SMALLSERIAL UNIQUE, + descripcion_categoria TEXT NOT NULL, + PRIMARY KEY (id_categoria) +); + +CREATE TABLE vehiculo(patente VARCHAR(6) NOT NULL UNIQUE, + ano_vehiculo SMALLINT NOT NULL, + marca VARCHAR(20) NOT NULL, + capacidad SMALLINT NOT NULL, + chofer VARCHAR(50) NOT NULL, + PRIMARY KEY (patente) +); + +CREATE TABLE region(id_region SMALLSERIAL UNIQUE, + nombre_region VARCHAR(20) NOT NULL, + PRIMARY KEY (id_region) +); + +CREATE TABLE ciudad(id_ciudad SMALLSERIAL UNIQUE, + nombre_ciudad VARCHAR(20) NOT NULL, + id_region SMALLINT, + PRIMARY KEY (id_ciudad), + FOREIGN KEY (id_region) REFERENCES region (id_region) ON DELETE RESTRICT +); + +CREATE TABLE tour(id_tour SERIAL UNIQUE, + nombre_tour VARCHAR(50) NOT NULL, + costo_indiv INT NOT NULL CHECK (costo_indiv > costo_grupal), + costo_grupal INT NOT NULL CHECK (costo_grupal > 0), + minima_personas INT NOT NULL CHECK (minima_personas > 0), + id_ciudad SMALLINT, + PRIMARY KEY (id_tour), + FOREIGN KEY (id_ciudad) REFERENCES ciudad (id_ciudad) ON DELETE RESTRICT +); + +CREATE TABLE lugar(id_lugar SMALLSERIAL UNIQUE, + nombre_lugar VARCHAR(50) NOT NULL, + valor_entrada INT NOT NULL CHECK (valor_entrada >= 0), + nivel SMALLINT NOT NULL CHECK (nivel >= 0), + id_ciudad SMALLINT, + PRIMARY KEY (id_lugar), + FOREIGN KEY (id_ciudad) REFERENCES ciudad (id_ciudad) ON DELETE RESTRICT +); + +CREATE TABLE turista(rut_turista VARCHAR(9) NOT NULL UNIQUE, + nombre_turista VARCHAR(50) NOT NULL, + fecha_nacimento DATE NOT NULL, + id_contacto INT, + PRIMARY KEY (rut_turista), + FOREIGN KEY (id_contacto) REFERENCES contacto_emergencia (id_contacto) ON DELETE RESTRICT +); + +CREATE TABLE tiene_enfermedad(rut_turista VARCHAR(9) NOT NULL, + id_enfermedad SMALLINT NOT NULL, + FOREIGN KEY (rut_turista) REFERENCES turista (rut_turista) ON DELETE RESTRICT, + FOREIGN KEY (id_enfermedad) REFERENCES enfermedad (id_enfermedad) ON DELETE RESTRICT +); + +CREATE TABLE compra(id_tour INT NOT NULL, + rut_turista VARCHAR(9) NOT NULL, + valor INT NOT NULL CHECK (valor > 0), + FOREIGN KEY (id_tour) REFERENCES tour (id_tour) ON DELETE RESTRICT, + FOREIGN KEY (rut_turista) REFERENCES turista (rut_turista) ON DELETE RESTRICT +); + +CREATE TABLE realiza(id_tour INT NOT NULL, + rut_turista VARCHAR(9) NOT NULL, + FOREIGN KEY (id_tour) REFERENCES tour (id_tour) ON DELETE RESTRICT, + FOREIGN KEY (rut_turista) REFERENCES turista (rut_turista) ON DELETE RESTRICT +); + +CREATE TABLE arrienda(patente VARCHAR(6) NOT NULL, + rut_empresa VARCHAR(9) NOT NULL, + precio INT NOT NULL CHECK (precio > 0), + fecha_devolucion DATE NOT NULL, + FOREIGN KEY (patente) REFERENCES vehiculo (patente) ON DELETE RESTRICT, + FOREIGN KEY (rut_empresa) REFERENCES empresa (rut_empresa) ON DELETE RESTRICT +); + +CREATE TABLE posee(rut_guia VARCHAR(9) NOT NULL, + id_especialidad SMALLINT NOT NULL, + nivel_especialidad SMALLINT NOT NULL CHECK (nivel_especialidad >= 0), + FOREIGN KEY (rut_guia) REFERENCES guia (rut_guia) ON DELETE RESTRICT, + FOREIGN KEY (id_especialidad) REFERENCES especialidad (id_especialidad) ON DELETE RESTRICT +); + +CREATE TABLE direccion(rut_guia VARCHAR(9) NOT NULL, + id_ciudad SMALLINT NOT NULL, + calle VARCHAR(20) NOT NULL, + numero SMALLINT NOT NULL, + FOREIGN KEY (rut_guia) REFERENCES guia (rut_guia) ON DELETE RESTRICT, + FOREIGN KEY (id_ciudad) REFERENCES ciudad (id_ciudad) ON DELETE RESTRICT +); + +CREATE TABLE participa(id_tour INT NOT NULL, + rut_guia VARCHAR(9) NOT NULL, + id_categoria SMALLINT NOT NULL, + FOREIGN KEY (id_tour) REFERENCES tour (id_tour) ON DELETE RESTRICT, + FOREIGN KEY (rut_guia) REFERENCES guia (rut_guia) ON DELETE RESTRICT, + FOREIGN KEY (id_categoria) REFERENCES categoria (id_categoria) ON DELETE RESTRICT +); + +CREATE TABLE asociado(id_tour INT NOT NULL, + id_lugar SMALLINT NOT NULL, + fecha_llegada DATE, + hora_llegada TIME, + fecha_salida DATE, + hora_salida TIME, + FOREIGN KEY (id_tour) REFERENCES tour (id_tour) ON DELETE RESTRICT, + FOREIGN KEY (id_lugar) REFERENCES lugar (id_lugar) ON DELETE RESTRICT +); diff --git a/doc/Informeinsertarduplas.sql b/doc/Informeinsertarduplas.sql new file mode 100644 index 0000000..31012df --- /dev/null +++ b/doc/Informeinsertarduplas.sql @@ -0,0 +1,79 @@ +INSERT INTO contacto_emergencia (telefono_emergencia, nombre_emergencia) VALUES ('56912345678', 'Chris Cromer'); +INSERT INTO contacto_emergencia (telefono_emergencia, nombre_emergencia) VALUES ('56913334578', 'John Cromer'); +INSERT INTO contacto_emergencia (telefono_emergencia, nombre_emergencia) VALUES ('56913984347', 'Tammy Cromer'); + +INSERT INTO enfermedad (descripcion_enfermedad) VALUES ('Heart Disease'); +INSERT INTO enfermedad (descripcion_enfermedad) VALUES ('Diabetes'); +INSERT INTO enfermedad (descripcion_enfermedad) VALUES ('Lung Cancer'); + +INSERT INTO empresa (rut_empresa, nombre_empresa, contacto, telefono) VALUES ('566034578', 'Hurtz', 'John Jacob', '56948904446'); +INSERT INTO empresa (rut_empresa, nombre_empresa, contacto, telefono) VALUES ('56404537k', 'Rent-a-Car', 'Jim Smith', '56948904446'); +INSERT INTO empresa (rut_empresa, nombre_empresa, contacto, telefono) VALUES ('566003458', 'Car Rentals Dot Com', 'Tony Stark', '56948904446'); + +INSERT INTO guia (rut_guia, nombre_guia) VALUES ('266304578', 'John Smith'); +INSERT INTO guia (rut_guia, nombre_guia) VALUES ('26640457k', 'Jack Offer'); +INSERT INTO guia (rut_guia, nombre_guia) VALUES ('26644457k', 'Mike Toga'); + +INSERT INTO especialidad (descripcion_especialidad) VALUES ('Climbing'); +INSERT INTO especialidad (descripcion_especialidad) VALUES ('History'); +INSERT INTO especialidad (descripcion_especialidad) VALUES ('Astronomy'); + +INSERT INTO categoria (descripcion_categoria) VALUES ('Coordinador'); +INSERT INTO categoria (descripcion_categoria) VALUES ('Speaker'); +INSERT INTO categoria (descripcion_categoria) VALUES ('Expert'); + +INSERT INTO vehiculo (patente, ano_vehiculo, marca, capacidad, chofer) VALUES ('as45fe', 2017, 'Chevy', 4, 'Tony'); +INSERT INTO vehiculo (patente, ano_vehiculo, marca, capacidad, chofer) VALUES ('lj58bv', 2002, 'Mazda', 20, 'Jimmy'); +INSERT INTO vehiculo (patente, ano_vehiculo, marca, capacidad, chofer) VALUES ('49fd38', 2001, 'Subaru', 10, 'Jake'); + +INSERT INTO region (nombre_region) VALUES ('Florida'); +INSERT INTO region (nombre_region) VALUES ('Alabama'); +INSERT INTO region (nombre_region) VALUES ('Arizona'); + +INSERT INTO ciudad (nombre_ciudad, id_region) VALUES ('Pensacola', 1); +INSERT INTO ciudad (nombre_ciudad, id_region) VALUES ('Montgomery', 2); +INSERT INTO ciudad (nombre_ciudad, id_region) VALUES ('Pheonix', 3); + +INSERT INTO tour (nombre_tour, costo_indiv, costo_grupal, minima_personas, id_ciudad) VALUES ('Los Rios', 200000, 150000, 1, 1); +INSERT INTO tour (nombre_tour, costo_indiv, costo_grupal, minima_personas, id_ciudad) VALUES ('Valle de la Luna', 300000, 120000, 10, 2); +INSERT INTO tour (nombre_tour, costo_indiv, costo_grupal, minima_personas, id_ciudad) VALUES ('Peru', 400000, 110000, 20, 3); + +INSERT INTO lugar (nombre_lugar, valor_entrada, nivel, id_ciudad) VALUES ('El Bio Bio', 0, 0, 1); +INSERT INTO lugar (nombre_lugar, valor_entrada, nivel, id_ciudad) VALUES ('El Bio Bio', 10000, 2, 2); +INSERT INTO lugar (nombre_lugar, valor_entrada, nivel, id_ciudad) VALUES ('El Bio Bio', 30000, 5, 3); + +INSERT INTO turista (rut_turista, nombre_turista, fecha_nacimento, id_contacto) VALUES ('226634572', 'Jack Asserton', '1985-02-23', 1); +INSERT INTO turista (rut_turista, nombre_turista, fecha_nacimento, id_contacto) VALUES ('194333573', 'Tommy Gunner', '1990-06-02', 2); +INSERT INTO turista (rut_turista, nombre_turista, fecha_nacimento, id_contacto) VALUES ('17398380k', 'Jimmy Johnson', '1955-01-01', 3); + +INSERT INTO tiene_enfermedad (rut_turista, id_enfermedad) VALUES ('226634572', 1); +INSERT INTO tiene_enfermedad (rut_turista, id_enfermedad) VALUES ('194333573', 2); +INSERT INTO tiene_enfermedad (rut_turista, id_enfermedad) VALUES ('17398380k', 3); + +INSERT INTO compra (id_tour, rut_turista, valor) VALUES (1, '226634572', 150000); +INSERT INTO compra (id_tour, rut_turista, valor) VALUES (2, '194333573', 120000); +INSERT INTO compra (id_tour, rut_turista, valor) VALUES (3, '17398380k', 110000); + +INSERT INTO realiza (id_tour, rut_turista) VALUES (1, '226634572'); +INSERT INTO realiza (id_tour, rut_turista) VALUES (1, '226634572'); +INSERT INTO realiza (id_tour, rut_turista) VALUES (1, '226634572'); + +INSERT INTO arrienda (patente, rut_empresa, precio, fecha_devolucion) VALUES ('as45fe', '566034578', 120000, '2018-11-29'); +INSERT INTO arrienda (patente, rut_empresa, precio, fecha_devolucion) VALUES ('lj58bv', '56404537k', 100000, '2018-11-29'); +INSERT INTO arrienda (patente, rut_empresa, precio, fecha_devolucion) VALUES ('49fd38', '566003458', 150000, '2018-11-29'); + +INSERT INTO posee (rut_guia, id_especialidad, nivel_especialidad) VALUES ('266304578', 1, 0); +INSERT INTO posee (rut_guia, id_especialidad, nivel_especialidad) VALUES ('26640457k', 2, 3); +INSERT INTO posee (rut_guia, id_especialidad, nivel_especialidad) VALUES ('26644457k', 3, 5); + +INSERT INTO direccion (rut_guia, id_ciudad, calle, numero) VALUES ('266304578', 1, 'Edison Dr.', 214); +INSERT INTO direccion (rut_guia, id_ciudad, calle, numero) VALUES ('26640457k', 1, 'Jackson Ave.', 1023); +INSERT INTO direccion (rut_guia, id_ciudad, calle, numero) VALUES ('26644457k', 1, 'Madison Way', 342); + +INSERT INTO participa (id_tour, rut_guia, id_categoria) VALUES (1, '266304578', 1); +INSERT INTO participa (id_tour, rut_guia, id_categoria) VALUES (2, '26640457k', 1); +INSERT INTO participa (id_tour, rut_guia, id_categoria) VALUES (3, '26644457k', 1); + +INSERT INTO asociado (id_tour, id_lugar, fecha_llegada, hora_llegada, fecha_salida, hora_salida) VALUES (1, 1, '2018-09-29', '04:15', '2018-09-30', '05:15'); +INSERT INTO asociado (id_tour, id_lugar, fecha_llegada, hora_llegada, fecha_salida, hora_salida) VALUES (2, 2, '2018-10-29', '09:35', '2018-10-30', '10:35'); +INSERT INTO asociado (id_tour, id_lugar, fecha_llegada, hora_llegada, fecha_salida, hora_salida) VALUES (3, 3, '2018-11-29', '16:40', '2018-11-30', '19:10'); diff --git a/doc/MER.dia b/doc/MER.dia index 20ac301..9166b8a 100644 Binary files a/doc/MER.dia and b/doc/MER.dia differ diff --git a/doc/MER.png b/doc/MER.png new file mode 100644 index 0000000..fd0382d Binary files /dev/null and b/doc/MER.png differ