miércoles, 26 de septiembre de 2012

Ejemplo votacion


Suponga que el gobierno mexicano ha autorizado que con fines históricos se revise el material electoral (actas y boletas) del proceso electoral federal de 2006, y a usted le ha sido asignado el diseño de la base de datos en que se almacene toda la información. Diseñe la base de datos y presente.
Diagrama Entidad Relación

Código sql para ejemplito

CREATE DATABASE Votacion;
USE Votacion;
CREATE TABLE Partido (
idPartido int NOT NULL,
nombre varchar(30) NOT NULL,
PRIMARY KEY (idPartido)
);
CREATE TABLE Candidato (
idCandidato int NOT NULL,
nombre varchar(35) NOT NULL,
idPartido int NOT NULL,
PRIMARY KEY(idCandidato),
FOREIGN KEY (idPartido) REFERENCES Partido(idPartido)
);
CREATE TABLE EntidadFederativa (
idEF int NOT NULL,
nombre varchar(25) NOT NULL,
PRIMARY KEY (idEF)
);
CREATE TABLE Boleta (
idB int NOT NULL,
idEF int NOT NULL,
idCandidato int NOT NULL,
tipoBoleta char(1) NOT NULL,
PRIMARY KEY (idB),
FOREIGN KEY (idEF) REFERENCES EntidadFederativa(idEF),
FOREIGN KEY (idCandidato) REFERENCES Candidato(idCandidato)
);

PARA EL LLENADO DE LOS DATOS PARA BOLETAS SE RECURRIO A UN ALGORITMO EMPLEADO EN UN PROGRAMA HECHO EN JAVA, LAS INSERSIONES SON DE FORMA ALEATORIA.


INSERT INTO Partido VALUES(0,"PAN");
INSERT INTO Partido VALUES(1,"Convengercia");
INSERT INTO Partido VALUES(2,"Alianza por Mexico");
INSERT INTO Partido VALUES(3,"PSD");
INSERT INTO Partido VALUES(4,"Partido Nueva Alianza");
INSERT INTO Partido VALUES(5,"NULO");

INSERT INTO Candidato VALUES(0,"Felipe Calderón Hinojosa",0);
INSERT INTO Candidato VALUES(1,"Andrés Manuel López Obrador",1);
INSERT INTO Candidato VALUES(2,"Roberto Madrazo Pintado",2);
INSERT INTO Candidato VALUES(3,"Patricia Mercado",3);
INSERT INTO Candidato VALUES(4,"Roberto Campa Cifrián",4);

INSERT INTO Candidato VALUES(5,"Omar Galindo Ayala",0);
INSERT INTO Candidato VALUES(6,"Gaspar Hernandez Victor",1);
INSERT INTO Candidato VALUES(7,"Gustavo Moises Romero Gonzalez",2);
INSERT INTO Candidato VALUES(8,"Ivan Samora Hernandez",3);
INSERT INTO Candidato VALUES(9,"Victor Hugo De la O Martinez",4);
INSERT INTO Candidato VALUES(10,"Jose Pablo Ibarra Camacho",0);
INSERT INTO Candidato VALUES(11,"Francisco Luna Hernandez",1);
INSERT INTO Candidato VALUES(12,"Martin Lopez Loera",2);
INSERT INTO Candidato VALUES(13,"Roxana Ramirez Rios",3);
INSERT INTO Candidato VALUES(14,"Gabriel Garcia Marquez",4);
INSERT INTO Candidato VALUES(15,"NULO",5);

INSERT INTO EntidadFederativa VALUES(0,"Aguascalientes");
INSERT INTO EntidadFederativa VALUES(1,"Baja California");
INSERT INTO EntidadFederativa VALUES(2,"Baja California Sur");
INSERT INTO EntidadFederativa VALUES(3,"Campeche");
INSERT INTO EntidadFederativa VALUES(4,"Chiapas");
INSERT INTO EntidadFederativa VALUES(5,"Chihuahua");
INSERT INTO EntidadFederativa VALUES(6,"Coahuila");
INSERT INTO EntidadFederativa VALUES(7,"Colima");
INSERT INTO EntidadFederativa VALUES(8,"Distrito Federal");
INSERT INTO EntidadFederativa VALUES(9,"Durango");
INSERT INTO EntidadFederativa VALUES(10,"Estado de México");
INSERT INTO EntidadFederativa VALUES(11,"Guanajuato");
INSERT INTO EntidadFederativa VALUES(12,"Guerrero");
INSERT INTO EntidadFederativa VALUES(13,"Hidalgo");
INSERT INTO EntidadFederativa VALUES(14,"Jalisco");
INSERT INTO EntidadFederativa VALUES(15,"Michoacán");
INSERT INTO EntidadFederativa VALUES(16,"Morelos");
INSERT INTO EntidadFederativa VALUES(17,"Nayarit");
INSERT INTO EntidadFederativa VALUES(18,"Nuevo León");
INSERT INTO EntidadFederativa VALUES(19,"Oaxaca");
INSERT INTO EntidadFederativa VALUES(20,"Puebla");
INSERT INTO EntidadFederativa VALUES(21,"Querétaro");
INSERT INTO EntidadFederativa VALUES(22,"Quintana Roo");
INSERT INTO EntidadFederativa VALUES(23,"San Luis Potosí");
INSERT INTO EntidadFederativa VALUES(24,"Sinaloa");
INSERT INTO EntidadFederativa VALUES(25,"Sonora");
INSERT INTO EntidadFederativa VALUES(26,"Tabasco");
INSERT INTO EntidadFederativa VALUES(27,"Tamaulipas");
INSERT INTO EntidadFederativa VALUES(28,"Tlaxcala");
INSERT INTO EntidadFederativa VALUES(29,"Veracruz");
INSERT INTO EntidadFederativa VALUES(30,"Yucatán");
INSERT INTO EntidadFederativa VALUES(31,"Zacatecas");

• TOTAL DE VOTOS POR CANDIDATO (PRESIDENCIAL y DIPUTADOS)
Candidatos a la presidencia
(total de votos anulados para candidatos a presidente)
select count(idB) as Total from boleta where tipoboleta='N' and idB between 0 and
999;
(total de votos para candidatos a presidentes)
1. select count(boleta.idCandidato) from boleta where tipoBoleta = 'P';

2. select count(idb) from boleta where idb between 0 and 999 and tipoBoleta='p';

SELECT b.idCandidato, c.nombre, b.tipoBoleta,count(b.idCandidato)NumeroVotos FROM boleta b, candidato c where b.tipoboleta='P' and c.idcandidato = b.idcandidato group by idCandidato;
Candidatos a diputados
(total de votos anulados para candidatos a diputados)

select count(idB) as Total from boleta where tipoboleta='N' and idB between 1000 and 1999;

(total de votos para candidates a diputados)
select count(boleta.idCandidato) from boleta where tipoBoleta = 'D';


SELECT b.idCandidato, c.nombre, b.tipoBoleta,count(b.idCandidato)NumeroVotos FROM boleta b, candidato c where b.tipoboleta='D' and c.idcandidato = b.idcandidato group by idCandidato;

• PORCENTAJE DE VOTOS POR PARTIDO POLITICO EN CADA ENTIDAD FEDERATIVA
Candidatos a la presidencia
SELECT b.idEF "IdEF", e.nombre "Nombre Entidad", b.idCandidato "IdCandidato", c.nombre "Nombre Candidato", p.nombre "Nombre Partido", count(b.idEF) SumaE,((count(b.idCandidato)*100)/(SELECT count(a.idEF) FROM boleta a)) porcentaje FROM boleta b, entidadfederativa e, candidato c, partido p where b.tipoboleta = 'P' and b.idEF = e.idEF and b.idCandidato = c.idCandidato and c.idpartido = p.idpartido group by b.idEF,b.idcandidato;

Candidatos a diputados
SELECT b.idEF "IdEF", e.nombre "Nombre Entidad", b.idCandidato "IdCandidato", c.nombre "Nombre Candidato", p.nombre "Nombre Partido", count(b.idEF) SumaE,((count(b.idCandidato)*100)/(SELECT count(a.idEF) FROM boleta a)) porcentaje FROM boleta b, entidadfederativa e, candidato c, partido p where b.tipoboleta = 'D' and b.idEF = e.idEF and b.idCandidato = c.idCandidato and c.idpartido = p.idpartido group by b.idEF,b.idcandidato;

• TOTAL DE VOTOS EN LAS ACTAS MENOS EL TOTAL DE VOTOS DE ACUERDO A LA BOLETA PARA CADA PARTIDO POLPÍTICO
Candidatos a diputados
(total de votos en las actas PRESIDENCIALES)
select sum(total) from acta where idA between 0 and 31;

(total de votos de acuerdo a la boleta para cada partido)
SELECT p.nombre,tipoBoleta,count(b.idCandidato) FROM boleta b inner join candidato c on b.idCandidato = c.idCandidato inner join partido p on c.idPartido = p.idPartido where b.tipoboleta = 'P' group by c.idpartido;

(total de boletas por todos los candidatos a la presidencia)
SELECT tipoBoleta, count(b.idCandidato) from boleta b, candidato c, partido p where b.idcandidato = c.idcandidato and c.idpartido = p.idpartido and b.tipoboleta = 'P';

(total de boletas en las actas menos el total de boletas para presidencia )
select sum(total)-(SELECT count(b.idCandidato) totalBPresidente from boleta b, candidato c, partido p where b.idcandidato = c.idcandidato and c.idpartido = p.idpartido and b.tipoboleta = 'P') totalBoletas from acta where idA between 0 and 31;

Candidatos a diputados
(total de votos en las actas para DIPUTADO)
select sum(total) from acta where idA between 32 and 64;

(total de votos de acuerdo a la boleta para cada partido)
SELECT p.nombre,tipoBoleta,count(b.idCandidato) FROM boleta b inner join candidato c on b.idCandidato = c.idCandidato inner join partido p on c.idPartido = p.idPartido where b.tipoboleta = 'D' group by c.idpartido;

(total de boletas por todos los candidatos a diputados)
SELECT tipoBoleta, count(b.idCandidato) from boleta b, candidato c, partido p where b.idcandidato = c.idcandidato and c.idpartido = p.idpartido and b.tipoboleta = 'D';

(total de boletas en las actas menos el total de boletas para diputados )
select sum(total)-(SELECT count(b.idCandidato) totalBDiputado from boleta b, candidato c, partido p where b.idcandidato = c.idcandidato and c.idpartido = p.idpartido and b.tipoboleta = 'D') totalBoletas from acta where idA between 32 and 64;  

• PARA UNA ENTIDAD Y UN PARTIDO POLÍTICO DETERMINADOS, EL TOTAL DE VOTOS PARA PRESIDENTE DE LA REPÚBLICA MENOS EL TOTAL DE VOTOS PARA DIPUTADOS FEDERALES
select count(idb) tbPresidencia from boleta where idef = 10 and tipoboleta='p';
select count(idb) tbDiputado from boleta where idef = 10 and tipoboleta='d';
select count(idb)-(select count(idb) from boleta where idef = 10 and tipoboleta='d') as total from boleta where idef = 10 and tipoboleta='p';

No hay comentarios:

Publicar un comentario