Skip to content

Commit b76c461

Browse files
authored
SQL
1 parent c3c8931 commit b76c461

File tree

4 files changed

+378
-0
lines changed

4 files changed

+378
-0
lines changed

Diff for: Insert Statements.sql

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#Candidate_Type
2+
insert into Candidate_Type values(100,"MP");
3+
insert into Candidate_Type values(101,"MLA");
4+
5+
#User_Type
6+
insert into User_Type values(1,"Candidate");
7+
insert into User_Type values(2,"Citizen");
8+
9+
#Party_Table
10+
insert into Party_Table values(11,"BJP","Lotus","Narendra Modi");
11+
insert into Party_Table values(12,"INC","Hand","Rahul Gandhi");
12+
insert into Party_Table values(13,"AAP","Broom","Arvind Kejriwa");
13+
insert into Party_Table values(14,"BSP","Elephant","Mayawati");
14+
15+
#Election_Table
16+
insert into Election_Table values(200,"General Elections");
17+
insert into Election_Table values(201,"State Assembly");
18+
19+
#Address
20+
insert into Address values(234,"Andheri","mumbai","Maharashtra",400059);
21+
insert into Address values(235,"Hadapsar","Pune","Maharashtra",411013);
22+
insert into Address values(236,"Malviya","Lucknow","Uttar Pradesh",226004);
23+
insert into Address values(237,"Depalpur","Indore","Madhya Pradesh",453115);
24+
25+
#Voter_Table
26+
insert into Voter_Table values("3591 4628 3661","Akash","Singh","Aishwarya","Bhavesh","M","1984-02-16",37,234,"9623412913");
27+
insert into Voter_Table values("5773 7940 7366","Dipti","Kumar","Gayatri","Dheeraj","F","1998-01-13",23,235,9222325956);
28+
insert into Voter_Table values("7820 3429 4038","Shlok","Agarwal","Aparna","Girish","M","1988-02-04",33,234,9722768470);
29+
insert into Voter_Table values("6169 5028 5641","Rashid","Khan","Indira","Abhay","M","1976-10-17",44,235,9414321457);
30+
insert into Voter_Table values("7367 4166 6818","Nicole","Dias","Juhi","Deepak","F","1991-12-08",29,234,9913542379);
31+
insert into Voter_Table values("5698 6323 9187","Muskan","Gupta","Latika","Harmeet","F","1990-07-14",30,235,9406269045);
32+
insert into Voter_Table values("3552 8455 9830","Saima","Shaikh","Anushree","Bipin","F","1975-12-02",45,234,9251125952);
33+
insert into Voter_Table values("4616 8141 8774","Mayur","Chauhan","Mallika","Mahendra","M","1998-06-01",22,235,9445560413);
34+
insert into Voter_Table values("5629 4547 8360","Aniket","Mali","Namrata","Aditya","M","1977-08-30",43,234,9353628848);
35+
insert into Voter_Table values("9159 9075 6877","Priti","Krishna","Niharika","Rakesh","F","1984-02-03",37,235,9357732303);
36+
insert into Voter_Table values("9996 7085 3995","Bhavna","Wadhwani","Arpita","Mehul","F","1986-05-18",34,234,9600223943);
37+
insert into Voter_Table values("3904 9051 4118","Shrishti","Shetty","Pallavi","Mukesh","F","1993-04-04",28,235,9661640359);
38+
insert into Voter_Table values("6219 8659 3521","Harsh","Chougle","Rashmi","Akshat","M","1990-04-25",30,234,9978862736);
39+
insert into Voter_Table values("8683 3592 5077","Rehmat","Khan","Saloni","Nitish","M","1981-04-27",39,235,9673536403);
40+
insert into Voter_Table values("6179 3483 8162","Joey","Dsouza","Priyanka","Prakash","M","1980-06-21",40,234,9533089508);
41+
insert into Voter_Table values("8199 8865 9927","Celina","Dias","Eva","Thomas","F","1975-01-27",46,235,9356542209);
42+
insert into Voter_Table values("5307 8510 8738","Binita","Soni","Anjali","Aniket","F","1992-02-04",29,234,9472486996);
43+
insert into Voter_Table values("5039 9340 8931","Aasim","Moulvi","Jasbir","Rahul","M","1971-12-06",49,235,9995661892);
44+
insert into Voter_Table values("4413 5402 5058","Vinit","Rajput","Sneha","Randhir","M","1972-11-23",48,234,9254284523);
45+
insert into Voter_Table values("5308 9629 6615","Neha","Bhatt","Tanushree","Arun","F","1975-08-12",45,235,9358443022);
46+
insert into Voter_Table values("8392 3921 0192","Aditi","Kharat","Simran","Jitendra","F","1948-06-06",72,234,9821009201);
47+
48+
#Candidate_Table
49+
insert into Candidate_Table values(1034,"3904 9051 4118",101,11,201,235);
50+
insert into Candidate_Table values(1035,"6219 8659 3521",101,11,201,234);
51+
insert into Candidate_Table values(1036,"8683 3592 5077",101,13,201,235);
52+
insert into Candidate_Table values(1037,"6179 3483 8162",101,12,201,234);
53+
insert into Candidate_Table values(1038,"8199 8865 9927",101,12,201,235);
54+
insert into Candidate_Table values(1039,"7820 3429 4038",101,13,201,234);
55+
insert into Candidate_Table values(1040,"6169 5028 5641",101,14,201,235);
56+
insert into Candidate_Table values(1041,"7367 4166 6818",101,14,201,234);
57+
58+
#User_Table
59+
insert into User_Table values("ABC659753","ABC@3661",1,"3591 4628 3661",2);
60+
insert into User_Table values("JID563930","JID@7366",1,"5773 7940 7366",2);
61+
insert into User_Table values("KOF752745","KOF@4038",1,"7820 3429 4038",1);
62+
insert into User_Table values("KFL505615","KFL@5641",1,"6169 5028 5641",1);
63+
insert into User_Table values("OKF618375","OKF@6818",1,"7367 4166 6818",1);
64+
insert into User_Table values("LPK191656","LPK@9187",1,"5698 6323 9187",2);
65+
insert into User_Table values("JIJ126650","JIJ@9830",1,"3552 8455 9830",2);
66+
insert into User_Table values("PLD990405","PLD@8774",1,"4616 8141 8774",2);
67+
insert into User_Table values("KPF247360","KPF@8360",1,"5629 4547 8360",2);
68+
insert into User_Table values("KLP662421","KLP@6877",1,"9159 9075 6877",2);
69+
insert into User_Table values("OKF192549","OKF@3995",1,"9996 7085 3995",2);
70+
insert into User_Table values("PXS330426","PXS@4118",1,"3904 9051 4118",1);
71+
insert into User_Table values("DKS350620","DKS@3521",1,"6219 8659 3521",1);
72+
insert into User_Table values("DKM595147","DKM@5077",1,"8683 3592 5077",1);
73+
insert into User_Table values("UHD596266","UHD@8162",1,"6179 3483 8162",1);
74+
insert into User_Table values("DAK821391","DAK@9927",1,"8199 8865 9927",1);
75+
insert into User_Table values("CDK501224","CDK@8738",1,"5307 8510 8738",2);
76+
insert into User_Table values("LFP359677","LFP@8931",1,"5039 9340 8931",2);
77+
insert into User_Table values("LDK886759","LDK@5058",1,"4413 5402 5058",2);
78+
insert into User_Table values("OKD930875","OKD@6615",1,"5308 9629 6615",2);
79+
insert into User_Table values("UDJ092345","UDJ@0192",0,"8392 3921 0192",2);
80+
81+
#Vote_Table
82+
insert into Vote_Table values("V1","ABC659753",11,1035,234,"ABC@3661","ABC@3661");
83+
insert into Vote_Table values("V2","JID563930",11,1034,235,"JID@7366","JID@7366");
84+
insert into Vote_Table values("V3","KOF752745",13,1039,234,"KOF@4038","KOF@4038");
85+
insert into Vote_Table values("V4","KFL505615",14,1040,235,"KFL@5641","KFL@5641");
86+
insert into Vote_Table values("V5","OKF618375",14,1041,234,"OKF@6818","OKF@6818");
87+
insert into Vote_Table values("V6","LPK191656",12,1038,235,"LPK@9187","LPK@9187");
88+
insert into Vote_Table values("V7","JIJ126650",11,1035,234,"JIJ@9830","JIJ@9830");
89+
insert into Vote_Table values("V8","PLD990405",11,1034,235,"PLD@8774","PLD@8774");
90+
insert into Vote_Table values("V9","KPF247360",12,1037,234,"KPF@8360","KPF@8360");
91+
insert into Vote_Table values("V10","KLP662421",13,1036,235,"KLP@6877","KLP@6877");
92+
insert into Vote_Table values("V11","OKF192549",11,1035,234,"OKF@3995","OKF@3995");
93+
insert into Vote_Table values("V12","PXS330426",11,1034,235,"PXS@4118","PXS@4118");
94+
insert into Vote_Table values("V13","DKS350620",11,1035,234,"DKS@3521","DKS@3521");
95+
insert into Vote_Table values("V14","DKM595147",13,1036,235,"DKM@5077","DKM@5077");
96+
insert into Vote_Table values("V15","UHD596266",12,1037,234,"UHD@8162","UHD@8162");
97+
insert into Vote_Table values("V16","DAK821391",12,1038,235,"DAK@9927","DAK@9927");
98+
insert into Vote_Table values("V17","CDK501224",11,1035,234,"CDK@8738","CDK@8738");
99+
insert into Vote_Table values("V18","LFP359677",13,1036,235,"LFP@8931","LFP@8931");
100+
insert into Vote_Table values("V19","LDK886759",12,1037,234,"LDK@5058","LDK@5058");
101+
insert into Vote_Table values("V20","OKD930875",11,1034,235,"OKD@6615","OKD@6615");
102+
103+
select * from vote_table;
104+
select * from result;
105+
select * from voter_table;

Diff for: Queries.sql

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#What if a person changes there number permanently?
2+
update voter_table set Phone=9283478293 where AADHAAR="5773 7940 7366";
3+
4+
5+
#What will you do if for some political reason voting needs to occur again and you have to delete all rows from the vote and result table?
6+
truncate result;
7+
truncate vote_table;
8+
select * from result;
9+
select * from vote_table;
10+
11+
12+
#Do you want to know how many people from voted from a particular district?
13+
CREATE VIEW district_vote_count AS
14+
SELECT DistrictID, SUM(Vote_Count)
15+
FROM Result
16+
GROUP BY DistrictID;
17+
18+
drop view district_vote_count;
19+
select* from district_vote_count;
20+
21+
22+
#Do you know how many a votes each party got from all the districts combined?
23+
CREATE VIEW party_vote_count AS
24+
SELECT Result.PartyID, party_table.PartyName,SUM(Vote_Count) AS 'Total_Count'
25+
FROM Result,party_table
26+
WHERE Party_table.Partyid = Result.partyID
27+
GROUP BY result.PartyID
28+
ORDER BY Total_Count DESC;
29+
30+
31+
drop view party_vote_count;
32+
select* from party_vote_count;
33+
34+
35+
#What will we do with a person’s information who dies?
36+
delete voter_table,user_table from Voter_Table,User_Table where Voter_Table.Aadhaar=User_table.Aadhaar and User_Table.isActive=0;
37+
#OR
38+
delete Voter_Table,User_Table From Voter_Table INNER JOIN User_Table on Voter_Table.Aadhaar=User_table.Aadhaar where User_Table.isActive=0;
39+
40+
select count(*) from user_table where User_Table.isActive=0;
41+
insert into User_Table values("UDJ092345","UDJ@0192",0,"8392 3921 0192",2);
42+
insert into Voter_Table values("8392 3921 0192","Aditi","Kharat","Simran","Jitendra","F","1948-06-06",72,234,9821009201);
43+
44+
45+
#Want all the details related to each candidate?
46+
create view candidate_detail as
47+
SELECT CandidateID, party_table.PartyName, voter_table.*
48+
FROM candidate_table LEFT JOIN voter_table
49+
ON candidate_table.AADHAAR = voter_table.AADHAAR
50+
INNER JOIN party_table
51+
ON Party_table.Partyid = candidate_table.partyID;
52+
53+
drop view candidate_detail;
54+
select * from candidate_detail;

Diff for: Voting tables.sql

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
CREATE TABLE Address(
2+
DistrictID integer NOT NULL,
3+
Locality VARCHAR(30) NOT NULL,
4+
City VARCHAR(30) NOT NULL,
5+
State VARCHAR(30) NOT NULL,
6+
Zip VARCHAR(10) NOT NULL,
7+
CONSTRAINT PK_District PRIMARY KEY (DistrictID));
8+
9+
CREATE TABLE Voter_Table(
10+
AADHAAR char(15) NOT NULL ,
11+
FirstName VARCHAR(30) NOT NULL,
12+
Lastname VARCHAR(50) NOT NULL,
13+
MotherName VARCHAR(30),
14+
FatherName VARCHAR(30),
15+
Sex char(7) not null,
16+
Birthday DATE NOT NULL,
17+
Age int not null,
18+
DistrictID integer NOT NULL,
19+
Phone Numeric NOT NULL,
20+
CONSTRAINT PK_VOTER PRIMARY KEY (AADHAAR),
21+
CONSTRAINT FK_DISTRICT FOREIGN KEY (DistrictID) references Address(DistrictID));
22+
23+
CREATE TABLE Candidate_Type(
24+
CandidateTypeID int not null,
25+
CandidateType varchar(20) not null,
26+
CONSTRAINT PK_CANDIDATETYPE PRIMARY KEY (CandidateTypeID));
27+
28+
CREATE TABLE Election_Table(
29+
ElectionID int not null,
30+
ElectionType varchar(20) not null,
31+
CONSTRAINT PK_ELECTION PRIMARY KEY (ElectionID));
32+
33+
CREATE TABLE Party_Table(
34+
PartyID int not null,
35+
PartyName varchar(20) not null unique,
36+
Symbol Varchar(20) not null unique,
37+
PartyLeader varchar(50) not null,
38+
CONSTRAINT PK_PARTY PRIMARY KEY (PartyID));
39+
40+
CREATE TABLE User_Type(
41+
UserTypeID int not null,
42+
UserType varchar(20) not null,
43+
CONSTRAINT PK_USERTYPE PRIMARY KEY (UserTypeID));
44+
45+
CREATE TABLE Candidate_Table(
46+
CandidateID int not null,
47+
AADHAAR char(15) not null,
48+
CandidateTypeID int not null,
49+
PartyID int not null,
50+
ElectionID int not null,
51+
DistrictID int not null,
52+
CONSTRAINT PK_CANDIDATE PRIMARY KEY (CandidateID),
53+
CONSTRAINT FK_VOTER FOREIGN KEY (AADHAAR) references Voter_Table(AADHAAR),
54+
CONSTRAINT FK_DISTRICT_2 FOREIGN KEY (DistrictID) references Address(DistrictID),
55+
CONSTRAINT FK_ELECTION FOREIGN KEY (ElectionID) references Election_Table(ElectionID),
56+
CONSTRAINT FK_PARTY FOREIGN KEY (PARTYID) references Party_Table(PartyID),
57+
CONSTRAINT FK_CANDIDATETYPE FOREIGN KEY (CandidateTypeID) references Candidate_Type(CandidateTypeID));
58+
59+
CREATE TABLE User_Table(
60+
VoterID varchar(10) not null,
61+
Def_Password varchar(50) not null,
62+
isActive BOOLEAN not null,
63+
AADHAAR char(15) not null,
64+
UserTypeID int not null,
65+
CONSTRAINT PK_USER PRIMARY KEY (VoterID),
66+
CONSTRAINT FK_VOTER_2 FOREIGN KEY (AADHAAR) references Voter_Table(AADHAAR),
67+
CONSTRAINT FK_USERID FOREIGN KEY (UserTypeID) references User_Type(UserTypeID));
68+
69+
70+
CREATE TABLE Vote_Table(
71+
VoteID varchar(7) not null,
72+
VoterID varchar(10) not null unique,
73+
PartyID int not null,
74+
CandidateID int not null,
75+
DistrictID int not null,
76+
Def_Password varchar(50) not null,
77+
password_entered varchar(50) not null,
78+
CONSTRAINT PK_VOTE PRIMARY KEY (VoteID),
79+
CONSTRAINT FK_VOTERID FOREIGN KEY (VoterID) references User_Table(VoterID),
80+
CONSTRAINT FK_CANDIDATEID FOREIGN KEY (CandidateID) references Candidate_Table(CandidateID),
81+
CONSTRAINT FK_DISTRICT_4 FOREIGN KEY (DistrictID) references Address(DistrictID),
82+
CONSTRAINT FK_PARTY_2 FOREIGN KEY (PARTYID) references Party_Table(PartyID),
83+
CHECK (Def_password=password_entered ));
84+
85+
86+
CREATE TABLE Result(
87+
ResultID int not null auto_increment,
88+
CandidateID int not null,
89+
PartyID int not null,
90+
DistrictID int not null,
91+
Vote_Count int not null,
92+
CONSTRAINT PK_RESULT PRIMARY KEY (ResultID),
93+
CONSTRAINT FK_CANDIDATEID_2 FOREIGN KEY (CandidateID) references Candidate_Table(CandidateID),
94+
CONSTRAINT FK_DISTRICT_5 FOREIGN KEY (DistrictID) references Address(DistrictID),
95+
CONSTRAINT FK_PARTY_3 FOREIGN KEY (PARTYID) references Party_Table(PartyID));
96+
97+
DELIMITER //
98+
CREATE TRIGGER Vote_counting
99+
after insert on Vote_Table
100+
FOR EACH ROW
101+
BEGIN
102+
if not exists (select CandidateID from Result where Result.CandidateID=new.CandidateID)
103+
then
104+
insert into Result(CandidateID,PartyID,DistrictID,Vote_Count) values(new.CandidateID,new.PartyID,new.DistrictID,1);
105+
else
106+
update Result set Result.Vote_Count=Result.Vote_Count+1 where Result.CandidateID=new.CandidateID;
107+
end if;
108+
END //
109+
DELIMITER ;
110+
111+
112+
show tables;
113+
drop table result;
114+
drop table Vote_Table;
115+
drop table User_Table;
116+
drop table Candidate_Table;
117+
drop table User_Type;
118+
drop table Party_Table;
119+
drop table Election_Table;
120+
drop table Candidate_Type;
121+
drop table Voter_Table;
122+
drop table Address;
123+
124+
125+
126+
127+
128+
129+
130+

Diff for: vote tables.sql

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
CREATE TABLE if not exists address(
2+
DistrictId integer NOT NULL,
3+
Locality VARCHAR(30) NOT NULL,
4+
City VARCHAR(30) NOT NULL,
5+
State VARCHAR(30) NOT NULL,
6+
Zip VARCHAR(10) NOT NULL,
7+
CONSTRAINT PK_District PRIMARY KEY (DistrictId));
8+
9+
CREATE TABLE if not exists voter_table(
10+
Aadhaar char(15) NOT NULL unique,
11+
FirstName VARCHAR(30) NOT NULL,
12+
MiddleName VARCHAR(30) NOT NULL,
13+
LastName VARCHAR(50) NOT NULL,
14+
Sex char(7) not null,
15+
Birthday DATE NOT NULL,
16+
Age int not null,
17+
Phone Numeric NOT NULL,
18+
Email varchar(50) NOT NULL,
19+
DistrictId integer NOT NULL,
20+
CONSTRAINT PK_VOTER PRIMARY KEY (Aadhaar),
21+
CONSTRAINT FK_DISTRICT FOREIGN KEY (DistrictId) references Address(DistrictId));
22+
23+
24+
CREATE TABLE if not exists party_table(
25+
PartyId int not null auto_increment ,
26+
PartyName varchar(20) not null unique,
27+
Symbol Varchar(20) not null unique,
28+
PartyLeader varchar(50) not null,
29+
LeaderAadhaar char(15) not null unique,
30+
CONSTRAINT PK_PARTY PRIMARY KEY (PartyId),
31+
CONSTRAINT FK_VOTER_3 FOREIGN KEY (LeaderAadhaar) references voter_table(Aadhaar));
32+
33+
34+
CREATE TABLE if not exists candidate_table(
35+
CandidateId int not null auto_increment ,
36+
Aadhaar char(15) not null unique,
37+
CandidateName varchar(100),
38+
PartyId int not null,
39+
DistrictId int not null,
40+
CONSTRAINT PK_CANDIDATE PRIMARY KEY (CandidateId),
41+
CONSTRAINT FK_VOTER FOREIGN KEY (Aadhaar) references voter_table(Aadhaar),
42+
CONSTRAINT FK_DISTRICT_2 FOREIGN KEY (DistrictId) references address(DistrictId),
43+
CONSTRAINT FK_PARTY FOREIGN KEY (PartyId) references party_table(PartyId));
44+
45+
CREATE TABLE if not exists user_table(
46+
VoterId varchar(10) not null,
47+
Aadhaar char(15) not null unique,
48+
_Password varchar(50) not null,
49+
CONSTRAINT PK_USER PRIMARY KEY (VoterId),
50+
CONSTRAINT FK_VOTER_2 FOREIGN KEY (Aadhaar) references voter_table(Aadhaar));
51+
52+
53+
CREATE TABLE if not exists vote_table(
54+
VoteId int not null auto_increment,
55+
Aadhaar char(15) not null unique,
56+
PartyId int not null,
57+
CandidateId int not null,
58+
DistrictId int not null,
59+
CONSTRAINT PK_VOTE PRIMARY KEY (VoteID),
60+
CONSTRAINT FK_VOTERID FOREIGN KEY (Aadhaar) references user_table(Aadhaar),
61+
CONSTRAINT FK_CANDIDATEID FOREIGN KEY (CandidateId) references candidate_table(CandidateId),
62+
CONSTRAINT FK_DISTRICT_4 FOREIGN KEY (DistrictId) references address(DistrictId),
63+
CONSTRAINT FK_PARTY_2 FOREIGN KEY (PartyId) references party_table(PartyId));
64+
65+
66+
CREATE TABLE if not exists result(
67+
ResultId int not null auto_increment,
68+
CandidateId int not null,
69+
PartyId int not null,
70+
DistrictId int not null,
71+
Vote_Count int not null,
72+
CONSTRAINT PK_RESULT PRIMARY KEY (ResultId),
73+
CONSTRAINT FK_CANDIDATEID_2 FOREIGN KEY (CandidateId) references candidate_table(CandidateId),
74+
CONSTRAINT FK_DISTRICT_5 FOREIGN KEY (DistrictId) references address(DistrictId),
75+
CONSTRAINT FK_PARTY_3 FOREIGN KEY (PartyId) references party_table(PartyId));
76+
77+
DELIMITER //
78+
CREATE TRIGGER Vote_counting
79+
after insert on vote_table
80+
FOR EACH ROW
81+
BEGIN
82+
if not exists (select CandidateId from result where result.CandidateId=new.CandidateId)
83+
then
84+
insert into result(CandidateId,PartyId,DistrictId,Vote_Count) values(new.CandidateId,new.PartyId,new.DistrictId,1);
85+
else
86+
update result set result.Vote_Count=result.Vote_Count+1 where result.CandidateId=new.CandidateId;
87+
end if;
88+
END //
89+
DELIMITER ;

0 commit comments

Comments
 (0)