“national electoral division profiles” case study vincent tang high distinction assignment,...
TRANSCRIPT
““National ElectoralNational ElectoralDivision Profiles”Division Profiles”
Case StudyCase Study
Vincent TangVincent Tang
High Distinction Assignment, High Distinction Assignment,
Autumn 2007Autumn 2007
Introduction to Introduction to National Electoral Division National Electoral Division
ProfilesProfilesThe database refers to the website:The database refers to the website:
http://www.aec.gov.au/_content/Who/profiles/divisional_prhttp://www.aec.gov.au/_content/Who/profiles/divisional_profiles_2004.pdfofiles_2004.pdf► Holds information about 14 National/Federal Electoral Holds information about 14 National/Federal Electoral
Divisions Divisions ► The current/previous members of those divisionsThe current/previous members of those divisions
► First NameFirst Name► SurnameSurname
► Parties the members belong tooParties the members belong too► The length of period they are members’ of the division The length of period they are members’ of the division
► Year StartedYear Started► Year EndedYear Ended
► Contact Details for the State Head OfficeContact Details for the State Head Office
ERDERD
One to Many Relationship – One to Many Relationship – 1:m1:m
divisionname| state| size | firstelection| demographic------------+------+------+--------------+-------------…………………….. | ….. | ….| ……….| …. Prospect| NSW | 159| 1968| Inner Metro Reid | NSW | 62| 1922| Inner Metro Calare | NSW | 21621| 1906| Rural
stateabv|buildingno| street | suburb | postcode| areacode| phoneno | faxno---------+----------+-----------------------+----------+---------+---------+---------+--------- NSW | 24| Campbell Street | Sydney | 2000| 2| 93756333| 92127659 VIC | 2| Lonsdale Street | Melbourne| 3000| 3| 92857100| 92857174 QLD | 488| Queen Street | Brisbane | 4000| 7| 38343440| 38323058 WA | 111| St Georges Terrace | Perth | 6848| 8| 63638080| 63638016 SA | 1| King William Street | Adelaide | 5000| 8| 82376555| 82312664 TAS | 86| Collins Street | Hobart | 7000| 3| 62350501| 62344268 NT | 80| Mitchell Street | Darwin | 800| 8| 89828000| 89817964 ACT | | Queen Victoria Terrace| Parkes | 2600| 2| 62714411| 62714558(8 rows)
Division Table
StateOffice Table
Primary Key
Foreign Key
• State in Division Table is Foreign Key in StateOffice Table
D iv is ion
D ivis io nNa meS ta te *
S izeF irs tE le ctio nD e mo gra ph ic
Sta te O ffice
S ta teB u ild ingNo
S tre e tS uburb
P o s tco deA re a C o deP ho ne No
F a xNoP a rtyNa me
Many to Many Relationship – Many to Many Relationship – m:mm:m
memid | memfirstname | memsurname-------+--------------+------------ 1 | C | Bowen 2 | J | Crosio 3 | R E | Klugman 8 | C | Pearce 9 | PE | Nugent 10 | J | Saunderson 11 | P | Andren
memid*| partyabv*-------+--------- 1 | ALP 2 | ALP 3 | ALP 8 | LP 9 | LP 10 | ALP 11 | IND
partyabv | partyname----------+------------------------------------------- ALP |Australian Labor Party IND |Independent LP |Liberal Party of Australia (formed in 1946)
Member Table Memberships Table
PartName Table
Query: Simple Query: Simple • Display Divisions that belong to NSW
divisionname | state | size | firstelection | demographic--------------+-------+-------+---------------+------------- Prospect | NSW | 159 | 1968 | Inner Metro Reid | NSW | 62 | 1922 | Inner Metro Calare | NSW | 21621 | 1906 | Rural(3 rows)
select * from division where state='NSW';
Query: Natural JoinQuery: Natural Join
memid | memfirstname | memsurname | partyabv-------+--------------+-------------+---------- 1 | C | Bowen | ALP 2 | J | Crosio | ALP 3 | R E | Klugman | ALP 4 | L | Ferguson | ALP 5 | T | Uren | ALP 6 | C A | Morgan | ALP 7 | J T | Lang | LANG LAB 8 | C | Pearce | LP 9 | PE | Nugent | LP 10 | J | Saunderson | ALP 11 | P | Andren | IND 12 | D W | Simmons | ALP 13 | A J | MacKenzie | NCP 13 | A J | MacKenzie | NP
select * from member natural join memberships;
• Display everything in both member table and membership table (natural join):
- shows the members name and the party they belong in
Query: Cross ProductQuery: Cross Product
memid | memfirstname | memsurname | memid | partyabv-------+--------------+-------------+-------+---------- 1 | C | Bowen | 1 | ALP 2 | J | Crosio | 2 | ALP 3 | R E | Klugman | 3 | ALP 4 | L | Ferguson | 4 | ALP 5 | T | Uren | 5 | ALP 6 | C A | Morgan | 6 | ALP 7 | J T | Lang | 7 | LANG LAB 8 | C | Pearce | 8 | LP 9 | PE | Nugent | 9 | LP 10 | J | Saunderson | 10 | ALP 11 | P | Andren | 11 | IND 12 | D W | Simmons | 12 | ALP 13 | A J | MacKenzie | 13 | NCP 13 | A J | MacKenzie | 13 | NP
select * from member, memberships where member.memid = memberships.memid;
• Display everything in both member table and membership table (cross-product):
- shows the members name and the party they belong in
Query: Group ByQuery: Group By
state | total-------+------- TAS | 1 NT | 1 ACT | 1 SA | 1 VIC | 3 WA | 1 QLD | 3 NSW | 3(8 rows)
select state, count(*) as total from division group by state;
• Determine the total amount of divisions in the each state
Query: Group By & HavingQuery: Group By & Having
state | total-------+------- VIC | 3 QLD | 3 NSW | 3(3 rows)
select state, count(*) as total from division group by state having count(*) >2;
• Determine the states with more than 2 divisions
Query: Sub-QueryQuery: Sub-Query
memfirstname | memsurname | divisionname | length--------------+-------------+--------------+-------- J | Crosio | Prospect | 14 T | Uren | Reid | 32 PE | Nugent | Aston | 11 D W | Simmons | Calare | 13 J A | England | Calare | 15 Dr A C | Theophanous | Calwell | 17 R F | Shipton | Higgins | 15 H E | Holt | Higgins | 18 B C | Humphreys | Griffith | 19 D M | Cameron | Griffith | 11 E E | Darling | Lilley | 13 W | Truss | Wide Bay | 16 B P | Hansen | Wide Bay | 13 N | Andrew | Wakefield | 21 D | Williams | Tangney | 11(15 rows)
select memfirstname, memsurname, divisionname, yrend-yrstart as length from member natural join period where yrend-yrstart> (select avg(yrend-yrstart) from period);
• Display previous members who we’re members for a division greater then the average length
Query: Self JoinQuery: Self Join
divisionname | demographic--------------+------------- Prospect | Inner Metro Reid | Inner Metro Higgins | Inner Metro Griffith | Inner Metro Lilley | Inner Metro Tangney | Inner Metro Solomon | Inner Metro Denison | Inner Metro Canberra | Inner Metro(9 rows)
SELECT d2.divisionname, d2.demographic FROM division d1, division d2 WHERE d1.divisionname = 'Lilley' AND d1.demographic = d2.demographic;
Show divisions which are considered the same demographic as Lilley
Action Statement: “CHECK”Action Statement: “CHECK”
CREATE TABLE Period(
MemID INTEGER ,YrStart INTEGER NOT NULL,YrEnd INTEGER ,DivisionName TEXT ,
………………,………………………,
CONSTRAINT di_Period_YrStart CHECK ((YrStart >= 1900) AND (YrStart <= 2500)), …………);
Output – When constraint is violated
INSERT INTO Period VALUES (37,1880,null,'Denison');
ERROR: new row for relation "period" violates check constraint "di_period_yrstart"
““CHECK” ConstraintsCHECK” Constraints
CREATE TABLE Division(
DivisionName TEXT ,State Varchar(3) ,Size INTEGER NOT NULL,FirstElection INTEGER ,Demographic TEXT NOT NULL,
…………………,……………………,
CONSTRAINT di_Division_Demographic CHECK (Demographic IN ('Inner Metro', 'Outer Metro', 'Provincial', 'Rural')));
Output – When constraint is violated
INSERT INTO Division VALUES ('Canberra','ACT',1900,1974, ‘Inner');
ERROR: new row for relation "division" violates check constraint "di_division_demographic"
Action Statement:Action Statement:“ON DELETE RESTRICT”“ON DELETE RESTRICT”
• To prevent delete in Member Table without deleting data from Memberships Table first
CREATE TABLE Memberships( MemID INTEGER NOT NULL,
PartyAbv TEXT ,CONSTRAINT MSPK PRIMARY KEY (MemID, PartyAbv),CONSTRAINT MS_MemIdFK FOREIGN KEY (MemID) REFERENCES Member
ON DELETE RESTRICTON UPDATE CASCADE,
CONSTRAINT MS_PartyFK FOREIGN KEY (PartyAbv) REFERENCES PartyNameON DELETE RESTRICTON UPDATE CASCADE);
DELETE FROM Member WHERE MemId = 1;
ERROR: update or delete on "member" violates foreign key constraint "ms_memidfk" on "memberships"
Output – When constraint is violated
Creating a “VIEW”Creating a “VIEW”• To create a view containing the previous/current members of the division ‘Prospect’
CREATE VIEW Prospect (Id, Surname, FirstName, Start) AS SELECT memid, memsurname, memfirstname, yrstart FROM member natural join period WHERE divisionname = 'Prospect';
id | surname | firstname | start----+---------+-----------+------- 1 | Bowen | C | 2004 2 | Crosio | J | 1990 3 | Klugman | R E | 1969(3 rows)
select * from Prospect;
Query a “View”Query a “View”
id | surname | firstname | start----+---------+-----------+------- 1 | Bowen | C | 2004 2 | Crosio | J | 1990(2 rows)
select * from prospect where start > 1980;
• To search the view table ‘Prospect’ for members who started after 1980