australian electoral commission database by: jason murphy

22

Upload: latrell-towns

Post on 31-Mar-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Australian Electoral Commission Database By: Jason Murphy
Page 2: Australian Electoral Commission Database By: Jason Murphy

Australian Electoral Australian Electoral Commission DatabaseCommission Database

By: Jason MurphyBy: Jason Murphy

Page 3: Australian Electoral Commission Database By: Jason Murphy

What is the AEC DatabaseWhat is the AEC Database

It records all former and present Federal It records all former and present Federal Members of parliamentMembers of parliament

It shows the results of each electorate and the It shows the results of each electorate and the relevant member who achieved such resultsrelevant member who achieved such results

Lists all political partiesLists all political parties Provides contact information for constituentsProvides contact information for constituents

Page 4: Australian Electoral Commission Database By: Jason Murphy

AEC Database ER DiagramAEC Database ER Diagram

Page 5: Australian Electoral Commission Database By: Jason Murphy

One to Many relationshipOne to Many relationship

One party has many membersOne party has many members

jmurphy=> select memberid, partyid, jmurphy=> select memberid, partyid, firstname, lastname from AEC_Member firstname, lastname from AEC_Member where partyid = 'LP';where partyid = 'LP';

memberid | partyid | firstname | lastnamememberid | partyid | firstname | lastname

----------+---------+---------------------+---------+-----------+----------+----------

258632 | LP | Tony | Abott258632 | LP | Tony | Abott

344523 | LP | Phillip | Ruddock344523 | LP | Phillip | Ruddock

Page 6: Australian Electoral Commission Database By: Jason Murphy

Many to many relationshipMany to many relationship

Many party’s have many election results Many party’s have many election results as they vary from electorate to electorateas they vary from electorate to electorate

TABLE 1 :AEC_ElectorateTABLE 1 :AEC_Electorate electorateid | electionresultsid | state | landsize | age18 | age30 | age50electorateid | electionresultsid | state | landsize | age18 | age30 | age50

--------------+-------------------+-------+----------+-------+-------+---------------------+-------------------+-------+----------+-------+-------+-------

Lowe | 82465 | NSW | 45000 | 10 | 20 | 68Lowe | 82465 | NSW | 45000 | 10 | 20 | 68

Sydney | 92465 | NSW | 65000 | 1 | 67 | 5Sydney | 92465 | NSW | 65000 | 1 | 67 | 5

Berowra | 79462 | NSW | 76000 | 25 | 25 | 30Berowra | 79462 | NSW | 76000 | 25 | 25 | 30

Flinders | 23122 | TAS | 100000 | 25 | 25 | 30Flinders | 23122 | TAS | 100000 | 25 | 25 | 30

(4 rows)(4 rows)

Page 7: Australian Electoral Commission Database By: Jason Murphy

Many to Many Linked ByMany to Many Linked By

TABLE THAT LINKS THE PREVIOUS AND TABLE THAT LINKS THE PREVIOUS AND THE NEXT SLIDE: AEC_ER THE NEXT SLIDE: AEC_ER

erid | electionresultsid| electorateiderid | electionresultsid| electorateid

980012 | 82465 | Lowe980012 | 82465 | Lowe

213652 | 72465 | Lowe213652 | 72465 | Lowe

(2 rows)(2 rows)

Page 8: Australian Electoral Commission Database By: Jason Murphy

Many to Many Cont’dMany to Many Cont’d

AEC_ElectionResultsAEC_ElectionResults electionresultsid | liberal | labor | noinformal | totalvotes | yearelectionresultsid | liberal | labor | noinformal | totalvotes | year

-------------------+---------+-------+------------+------------+-------------------------+---------+-------+------------+------------+------

79462 | 60.2 | 10.8 | 25 | 15000 | 200479462 | 60.2 | 10.8 | 25 | 15000 | 2004

72465 | 30.2 | 30.9 | 5 | 12500 | 200172465 | 30.2 | 30.9 | 5 | 12500 | 2001

92465 | 18.2 | 5 | 1 | 11250 | 200492465 | 18.2 | 5 | 1 | 11250 | 2004

82465 | 20.24 | 30.62 | 30 | 15100 | 200482465 | 20.24 | 30.62 | 30 | 15100 | 2004

23122 | 5.98 | 10 | 60 | 12332 | 200123122 | 5.98 | 10 | 60 | 12332 | 2001

(5 rows)(5 rows)

Page 9: Australian Electoral Commission Database By: Jason Murphy

Simple query of a single tableSimple query of a single table

Show all election results where labor achieved greater than 30% on Show all election results where labor achieved greater than 30% on a two party preferred basisa two party preferred basis

jmurphy=> Select * from AEC_ElectionResults where labor > 30;jmurphy=> Select * from AEC_ElectionResults where labor > 30; electionresultsid | liberal | labor | noinformal | totalvotes | yearelectionresultsid | liberal | labor | noinformal | totalvotes | year-------------------+---------+-------+------------+------------+-------------------------+---------+-------+------------+------------+------ 72465 | 30.2 | 30.9 | 5 | 12500 | 200172465 | 30.2 | 30.9 | 5 | 12500 | 2001 82465 | 20.24 | 30.62 | 30 | 15100 | 200482465 | 20.24 | 30.62 | 30 | 15100 | 2004(2 rows)(2 rows)

Page 10: Australian Electoral Commission Database By: Jason Murphy

Natural Join QueryNatural Join Query

When you want to join two tables together the automatic way is a When you want to join two tables together the automatic way is a natural join statement. The below query joins the AEC_Member and natural join statement. The below query joins the AEC_Member and AEC_Electorate table togetherAEC_Electorate table together

jmurphy=> Select electorateid, firstname, lastname, landsizejmurphy=> Select electorateid, firstname, lastname, landsize from AEC_Member natural join AEC_Electorate;from AEC_Member natural join AEC_Electorate; electorateid | firstname | lastname | landsizeelectorateid | firstname | lastname | landsize--------------+-----------+----------+------------------------+-----------+----------+---------- Lowe | John | Murphy | 45000Lowe | John | Murphy | 45000 Sydney | Tony | Abott | 65000Sydney | Tony | Abott | 65000 Flinders | Sarah | Stevens | 100000Flinders | Sarah | Stevens | 100000 Berowra | Phillip | Ruddock | 76000Berowra | Phillip | Ruddock | 76000(4 rows)(4 rows)

Page 11: Australian Electoral Commission Database By: Jason Murphy

Cross Product Natural JoinCross Product Natural Join This is the same query but the extended version where in the This is the same query but the extended version where in the

SQL the links between the tables i.e the primary and foreign SQL the links between the tables i.e the primary and foreign keys need to be manually correlated keys need to be manually correlated

select AEC_Electorate.electorateid, firstname, lastname, landsizeselect AEC_Electorate.electorateid, firstname, lastname, landsizefrom AEC_Member, AEC_Electoratefrom AEC_Member, AEC_Electoratewhere AEC_Member.electorateid = AEC_Electorate.electorateid;where AEC_Member.electorateid = AEC_Electorate.electorateid; electorateid | firstname | lastname | landsizeelectorateid | firstname | lastname | landsize--------------+-----------+----------+------------------------+-----------+----------+---------- Lowe | John | Murphy | 45000Lowe | John | Murphy | 45000 Sydney | Tony | Abott | 65000Sydney | Tony | Abott | 65000 Flinders | Sarah | Stevens | 100000Flinders | Sarah | Stevens | 100000 Berowra | Phillip | Ruddock | 76000Berowra | Phillip | Ruddock | 76000(4 rows)(4 rows)

Page 12: Australian Electoral Commission Database By: Jason Murphy

Group By Group By

When trying to find the number of electorates in each state a When trying to find the number of electorates in each state a group by function is neededgroup by function is needed

jmurphy=> select state, count (*) as Number from jmurphy=> select state, count (*) as Number from AEC_Electorate group by state order by state;AEC_Electorate group by state order by state;

state | numberstate | number

-------+---------------+--------

NSW | 3NSW | 3

TAS | 1TAS | 1

(2 rows)(2 rows)

Page 13: Australian Electoral Commission Database By: Jason Murphy

SubquerySubquery

Show the electorate, the state in which the electorate is Show the electorate, the state in which the electorate is when the land is greater than averagewhen the land is greater than average

jmurphy=> select electorateid, state, landsize from jmurphy=> select electorateid, state, landsize from AEC_Electorate where landsize > (select AEC_Electorate where landsize > (select AVG(landsize) from AEC_Electorate);AVG(landsize) from AEC_Electorate);

electorateid | state | landsizeelectorateid | state | landsize--------------+-------+------------------------+-------+---------- Berowra | NSW | 76000Berowra | NSW | 76000 Flinders | TAS | 100000Flinders | TAS | 100000(2 rows)(2 rows)

Page 14: Australian Electoral Commission Database By: Jason Murphy

Self Join querySelf Join query

When you want to ensure that the electorateid is correct. This is When you want to ensure that the electorateid is correct. This is just in case a member changes electorates and the database just in case a member changes electorates and the database wasn’t changed. wasn’t changed.

jmurphy=> select ass1.electorateid, ass2.electorateid, mstate from jmurphy=> select ass1.electorateid, ass2.electorateid, mstate from AEC_Member ass1, AEC_Electorate ass2 where AEC_Member ass1, AEC_Electorate ass2 where ass1.electorateid = ass2.electorateid;ass1.electorateid = ass2.electorateid;

electorateid | electorateid | mstateelectorateid | electorateid | mstate--------------+--------------+----------------------+--------------+-------- Lowe | Lowe | NSWLowe | Lowe | NSW Sydney | Sydney | NSWSydney | Sydney | NSW Flinders | Flinders | TASFlinders | Flinders | TAS Berowra | Berowra | NSWBerowra | Berowra | NSW(4 rows)(4 rows)

Page 15: Australian Electoral Commission Database By: Jason Murphy

CHECK statementsCHECK statements

It is important to ensure that the data entered is It is important to ensure that the data entered is correct. The following check statement ensures correct. The following check statement ensures that the number of informal votes entered is that the number of informal votes entered is

(a) not negative(a) not negative (b) doesn’t exceed the total number of votes(b) doesn’t exceed the total number of votes CONSTRAINT CONSTRAINT

AEC_ElectionResults_Invalid_Informals AEC_ElectionResults_Invalid_Informals CHECK ((NoInformal >= 0) AND CHECK ((NoInformal >= 0) AND (NoInformal <= TotalVotes))(NoInformal <= TotalVotes))

Page 16: Australian Electoral Commission Database By: Jason Murphy

Another CHECK constraintAnother CHECK constraint

The following CHECK constraint ensures that the The following CHECK constraint ensures that the member has a starting date greater than or in 1901.member has a starting date greater than or in 1901.

This is because Australia was not federated until 1901 This is because Australia was not federated until 1901 and that is the first possible year within the databaseand that is the first possible year within the database

CONSTRAINT AEC_Member_Invalid_StartYear CONSTRAINT AEC_Member_Invalid_StartYear CHECK (StartYear >= 1901)CHECK (StartYear >= 1901)

Page 17: Australian Electoral Commission Database By: Jason Murphy

Delete CascadeDelete Cascade

When PartyID is deleted this will flow throuhgh When PartyID is deleted this will flow throuhgh the AEC_Member table the AEC_Member table

CONSTRAINT AEC_Member1_Party_FK CONSTRAINT AEC_Member1_Party_FK FOREIGN KEY (PartyID) REFERENCES FOREIGN KEY (PartyID) REFERENCES AEC_PartyAEC_Party

ON DELETE CASCADE ON DELETE CASCADE

Page 18: Australian Electoral Commission Database By: Jason Murphy

Delete cascadeDelete cascade

When Electorate ID is deleted this will flow When Electorate ID is deleted this will flow throuhgh the AEC_Electorate tablethrouhgh the AEC_Electorate table

CONSTRAINT AEC_Member2_Electorate_FK CONSTRAINT AEC_Member2_Electorate_FK FOREIGN KEY (ElectorateID) FOREIGN KEY (ElectorateID) REFERENCES AEC_ElectorateREFERENCES AEC_Electorate

ON DELETE CASCADEON DELETE CASCADE

Page 19: Australian Electoral Commission Database By: Jason Murphy

Views in the AEC databaseViews in the AEC database

Views allow us to have the data on hand Views allow us to have the data on hand without having to constantly re type the same without having to constantly re type the same sql.sql.

The view is stored within the database and can The view is stored within the database and can be easily used again with a view name set by be easily used again with a view name set by the userthe user

CREATE VIEW name AS queryCREATE VIEW name AS query

Page 20: Australian Electoral Commission Database By: Jason Murphy

ViewsViews Say we want to have the following data shown:Say we want to have the following data shown:

electorateid, firstname, lastname, contactnumber where electorateid, firstname, lastname, contactnumber where there is no finish datethere is no finish date

This could be used by the AEC to issue a phone number for This could be used by the AEC to issue a phone number for people trying to get in contact with their member of parliamentpeople trying to get in contact with their member of parliament

Output:Output: | firstname | lastname | contactnumber || firstname | lastname | contactnumber | | John | Murphy | 0297474211 || John | Murphy | 0297474211 | | Tony | Abott | 0295611876 | | Tony | Abott | 0295611876 | | Sarah | Stevens | 0487299833 | | Sarah | Stevens | 0487299833 | | Phillip | Ruddock | 0411299833 || Phillip | Ruddock | 0411299833 |

CREATE VIEW contact AS firstname, lastname, contactnumber from CREATE VIEW contact AS firstname, lastname, contactnumber from AEC_Member where finishdate = null;AEC_Member where finishdate = null;

Page 21: Australian Electoral Commission Database By: Jason Murphy

How to use a view once savedHow to use a view once saved

\dv checks to see the views that have been \dv checks to see the views that have been stored, this is a user shortcutstored, this is a user shortcut

List of relations

Name | Type | Owner

-------------------------------+------+----------

contact | view | jmurphy

Page 22: Australian Electoral Commission Database By: Jason Murphy

The EndThe End

Thank you for listening about the Thank you for listening about the AEC databaseAEC database