Você está na página 1de 10

CREATE DATABASE PRACTICA_4;

USE PRACTICA_4;

CREATE DATABASE "Practica_4"

WITH OWNER = postgres

ENCODING = 'UTF8'

TABLESPACE = pg_default

LC_COLLATE = 'Spanish_Mexico.1252'

LC_CTYPE = 'Spanish_Mexico.1252'

CONNECTION LIMIT = -1;

CREATE TABLE municipio (

id_municipio serial primary key,

nombre varchar(50) unique not null

);

CREATE INDEX index_municipio

ON municipio

USING btree

(nombre COLLATE pg_catalog."default" NULLS FIRST);

CREATE TABLE servicio (

id_servicio serial primary key,

nombre varchar(50) unique not null

);

CREATE INDEX index_servicio

ON servicio

USING btree

(nombre COLLATE pg_catalog."default" NULLS FIRST);

CREATE TABLE tipo_empleado(

id_tipo serial primary key,


descripcion varchar(50) unique not null

);

CREATE INDEX index_empleado

ON tipo_empleado

USING btree

(descripcion COLLATE pg_catalog."default" NULLS FIRST);

CREATE TABLE grua(

id_grua serial primary key,

placa varchar(10) unique not null,

base int not null,

altura int not null

);

CREATE INDEX index_grua

ON grua

USING btree

(base NULLS FIRST, altura NULLS FIRST);

CREATE TABLE poblacion(

id_poblacion serial primary key,

nombre varchar(50) not null,

id_municipio integer not null,

FOREIGN KEY (id_municipio) REFERENCES municipio (id_municipio)

);

CREATE INDEX index_problacion

ON poblacion

USING btree

(nombre NULLS FIRST);

CREATE TABLE colonia(


id_colonia serial primary key,

nombre varchar(50) not null,

id_poblacion integer,

FOREIGN KEY (id_poblacion) REFERENCES poblacion (id_poblacion),

c_postal char(5) not null

);

CREATE INDEX index_colonia

ON colonia

USING btree

(nombre NULLS FIRST);

CREATE TABLE empresa(

id_empresa serial primary key,

nombre varchar(50) unique not null,

id_colonia integer not null,

FOREIGN KEY (id_colonia) REFERENCES colonia (id_colonia),

calle varchar(50) not null,

numero integer not null

);

CREATE INDEX index_empresa

ON empresa

USING btree

(nombre NULLS FIRST, calle NULLS FIRST, numero NULLS FIRST);

CREATE TABLE telefono(

id_telefono serial primary key,

num_telefono varchar(15) unique not null,

id_empresa integer not null,

FOREIGN KEY (id_empresa) REFERENCES empresa (id_empresa)

);
CREATE INDEX index_telefono

ON telefono

USING btree

(num_telefono NULLS FIRST);

CREATE TABLE cliente(

id_cliente serial primary key,

nombre varchar(30) not null,

paterno varchar(30) not null,

materno varchar(30) not null,

rfc varchar(15) unique not null,

calle varchar(50) not null,

numero integer not null,

id_colonia integer not null,

FOREIGN KEY (id_colonia) REFERENCES colonia (id_colonia)

);

CREATE INDEX index_cliente

ON cliente

USING btree

(nombre NULLS FIRST, paterno NULLS FIRST, rfc NULLS FIRST);

CREATE TABLE solicitud_mudanza(

id_solicitud serial primary key,

calle_origen varchar(30) not null,

numero_origen varchar(30) not null,

id_colonia_origen integer not null,

FOREIGN KEY (id_colonia_origen) REFERENCES colonia (id_colonia),

fecha_solicitud date not null,

fecha_mudanza date not null,

calle_destino varchar(30) not null,

numero_destino varchar(30) not null,

id_colonia_destino integer not null,

FOREIGN KEY (id_colonia_destino) REFERENCES colonia (id_colonia),


id_cliente integer not null,

FOREIGN KEY (id_cliente) REFERENCES cliente (id_cliente)

);

CREATE INDEX index_solicitud_mudanza

ON solicitud_mudanza

USING btree

(calle_origen NULLS FIRST, fecha_solicitud NULLS FIRST, fecha_mudanza NULLS FIRST);

CREATE TABLE transporte(

id_transporte serial primary key,

placa varchar(10) unique not null,

capacidad integer not null,

id_empresa integer not null,

FOREIGN KEY (id_empresa) REFERENCES empresa (id_empresa)

);

CREATE INDEX index_transporte

ON transporte

USING btree

(placa NULLS FIRST, capacidad NULLS FIRST);

CREATE TABLE excedente_grua(

id_excedente serial primary key,

lim_superior integer not null,

porcentaje integer not null,

id_grua integer not null,

FOREIGN KEY (id_grua) REFERENCES grua (id_grua)

);
CREATE TABLE excedente_transporte(

id_excedente serial primary key,

lim_superior integer not null,

porcentaje integer not null,

id_transporte integer not null,

FOREIGN KEY (id_transporte) REFERENCES transporte (id_transporte)

);

CREATE TABLE empleado(

id_empleado serial primary key,

nombre varchar(30) not null,

paterno varchar(30) not null,

materno varchar(30) not null,

id_tipo integer not null,

FOREIGN KEY (id_tipo) REFERENCES tipo_empleado (id_tipo),

sueldo money not null,

telefono_empresa varchar(15) not null,

telefono_casa varchar(15) not null,

calle varchar(50) not null,

numero integer not null,

id_colonia integer not null,

FOREIGN KEY (id_colonia) REFERENCES colonia (id_colonia)

);

CREATE INDEX index_empleado

ON empleado

USING btree

(sueldo NULLS FIRST, telefono_empresa NULLS FIRST, telefono_casa NULLS FIRST);

CREATE TABLE conductor_transporte(

id_conductor_transporte serial primary key,


id_empleado integer not null,

FOREIGN KEY (id_empleado) REFERENCES empleado (id_empleado)

);

CREATE TABLE conductor_grua(

id_conductor_grua serial primary key,

id_empleado integer not null,

FOREIGN KEY (id_empleado) REFERENCES empleado (id_empleado)

);

CREATE TABLE resolucion(

id_resolucion serial primary key,

id_solicitud integer not null,

FOREIGN KEY (id_solicitud) REFERENCES solicitud_mudanza (id_solicitud),

fecha_resolucion date not null,

respuesta varchar(20) not null

);

CREATE TABLE aceptada(

id_aceptada serial primary key,

id_resolucion integer not null,

FOREIGN KEY (id_resolucion) REFERENCES resolucion (id_resolucion),

fecha_atencion date not null,

respuesta varchar(20) not null

);

CREATE TABLE ofrece(

precio money not null,

id_servicio integer not null,

FOREIGN KEY (id_servicio) REFERENCES servicio (id_servicio),

id_empresa integer not null,

FOREIGN KEY (id_empresa) REFERENCES empresa (id_empresa),

id_solicitud integer not null,

FOREIGN KEY (id_solicitud) REFERENCES solicitud_mudanza (id_solicitud),


id_poblacion integer,

FOREIGN KEY (id_poblacion) REFERENCES poblacion (id_poblacion)

);

CREATE TABLE requiere(

id_solicitud integer not null,

FOREIGN KEY (id_solicitud) REFERENCES solicitud_mudanza (id_solicitud),

id_mudanza integer not null,

FOREIGN KEY (id_solicitud) REFERENCES solicitud_mudanza (id_solicitud)

);

CREATE TABLE requirio(

id_solicitud integer not null,

FOREIGN KEY (id_solicitud) REFERENCES solicitud_mudanza (id_solicitud),

id_aceptada integer not null,

FOREIGN KEY (id_aceptada) REFERENCES aceptada (id_aceptada),

tiempo time not null,

peso integer not null,

precio money not null

);

CREATE TABLE participa(

id_aceptada integer not null,

FOREIGN KEY (id_aceptada) REFERENCES aceptada (id_aceptada),

id_empleado integer not null,

FOREIGN KEY (id_empleado) REFERENCES empleado (id_empleado)

);

CREATE TABLE labora(

id_empresa integer not null,

FOREIGN KEY (id_empresa) REFERENCES empresa (id_empresa),

id_empleado integer not null,

FOREIGN KEY (id_empleado) REFERENCES empleado (id_empleado),

fecha_ingreso date not null


);

CREATE TABLE tiene(

id_transporte integer not null,

FOREIGN KEY (id_transporte) REFERENCES transporte (id_transporte),

id_conductor integer not null,

FOREIGN KEY (id_conductor) REFERENCES conductor_transporte


(id_conductor_transporte),

fecha date not null

);

CREATE INDEX index_tiene

ON tiene

USING btree

(fecha NULLS FIRST);

CREATE TABLE posee(

id_grua integer not null,

FOREIGN KEY (id_grua) REFERENCES grua (id_grua),

id_conductor integer not null,

FOREIGN KEY (id_conductor) REFERENCES conductor_grua (id_conductor_grua),

fecha date not null

);

CREATE INDEX index_posee

ON posee

USING btree

(fecha NULLS FIRST);

Você também pode gostar