Presentation is loading. Please wait.

Presentation is loading. Please wait.

Helen Spiropoulos Benjamin Mills Nicoleta Bikrogiannis Jessica El-chaar.

Similar presentations


Presentation on theme: "Helen Spiropoulos Benjamin Mills Nicoleta Bikrogiannis Jessica El-chaar."— Presentation transcript:

1 Helen Spiropoulos Benjamin Mills Nicoleta Bikrogiannis Jessica El-chaar

2 Models the mycareer website (www.mycareer.com.au)www.mycareer.com.au This database models the real world process of searching and applying for jobs. It can be applied both to job seekers and recruiters who are looking to increase the efficiency of sorting and searching their data. Allows jobseekers to search for and apply for jobs Jobs can be searched via a range of attributes such as salary, location, description, date of posting etc

3 Job seekers must register in order to apply for jobs Jobs must belong to a location and sector Recruiters can search for applicants who have applied to certain jobs within a chosen sector and notify them of future listings

4 LocID LocCity LocCountry Location CanEmail CanFirstName CanLastName CanSex CanBirthYear Candidate SecID SecTitle SecDesc Sector JobID* CanEmail* Application JobID JobPostDate JobSalary JobPosition LocID* SecID* JobDesc Job

5 SQL Queries on a Single Entity/ Table

6 Projecting tables’ contents using ‘SELECT’ select * from carnie_mycareer_candidate; select sectitle, secdesc from carnie_mycareer_sector; Selecting Everything in a table Selecting a couple of columns from a table canemailcanfirstnamecanlastnamecanSexcanbirthyear chris@hotmail.comChrisCarnieM08-01-1981 david11@hotmail.comDavidSaddingtonM21-06-1980 katiele@hotmail.comKatieLenehanF04-04-1978 sussiekelly@hotmail.comSussieKellyF24-11-1969 ron12@hotmail.comRonHowardM01-01-2000 secTitlesecDesc AccountingAll aspects of accounting disipline ITIT industry including sales, technical, support AdministrationReception, admin and supporting roles ConstructionBuilding, Plumbing, Electrical, Labouring RetailShop assistance, customer service

7 Restrict using ‘WHERE’ ** Show all male candidates Restricts the rows shown select * from carnie_mycareer_candidate where cansex = 'M'; canEmailcanFirstNamecanLastNamecanSexcanBirth chris@hotmail.comChrisCarnieM08-01-1981 david11@hotmail.comDavidSaddingtonM21-06-1980 katiele@hotmail.comKatieLenehanF04-04-1978 sussiekelly@hotmail.comSussieKellyF24-11-1969 ron12@hotmail.comRonHowardM01-01-2000 canEmailcanFirstNamecanLastNamecanSexcanBirth chris@hotmail.comChrisCarnieM08-01-1981 david11@hotmail.comDavidSaddingtonM21-06-1980 ron12@hotmail.comRonHowardM01-01-2000

8 Project and Restrict combo Can use to project a combination of columns and rows. Example: List the last name and email address of all male candidates over 30yrs select canlastname,canemail from carnie_mycareer_candidate where canbirthyear < 1977 and cansex = 'M'; canLastNamecanEmail Carniechris@hotmail.com Howardron12@hotmail.com

9 IS NULL List the last name and email address of the candidates where the candidate has no email address entered. select canlastname, canemail from carnie_mycareer_candidate where canemail is null; canlastname | canemail ------------------+---------- (0 rows) There are no null entries so nothing appears in the table!

10 IS NOT NULL List the last name and email address of the candidates who have an email address. select canlastname, canemail from carnie_mycareer_candidate where canemail is not null; canLastNamecanEmail Carniechris@hotmail.com Saddingtondavid11@hotmail.com Lenehankatiele@hotmail.com Kellysussiekelly@hotmail.com Howardron12@hotmail.com

11 IN Used when you want to return various values. Example; List the job position and salary of all jobs position available from Sydney, Canberra and Wellington. select Jobposition, jobsalary from carnie_mycareer_job where locid in (1,2,5) ; It’s the same as doing the below but easier! Select Jobposition, Jobsalary from carnie_mycareer_job where locid = 1 OR locid = 2 OR locid = 5; jobPositionjobSalary Senior50000 CFO120000 Jr45000 Reception20000 Team Member45000 2IC60000

12 NOT IN Used when you want to exclude various values. Example; List the job position and salary of all jobs position available except those from Sydney, Canberra and Wellington. select Jobposition, jobsalary from carnie_mycareer_job where locid not in (1,2,5) ; It’s the same as doing the below but easier! Select Jobposition, Jobsalary from carnie_mycareer_job where locid <> 1 AND locid <> 2 and locid <> 5; jobPositionjobSalary Graduate38000 Team Member85000 Logistics75000

13 ORDERING COLUMNS The order stated after the select statement is the order that the Columns will appear. Example; select canfirstname, canlastname from CARNIE_MYCAREER_CANDIDATE;  select canfirstname, canlastname from CARNIE_MYCAREER_CANDIDATE;  canFirstNamecanLastName ChrisCarnie DavidSaddington KatieLenehan SussieKelly RonHoward canLastNamecanFirstName CarnieChris SaddingtonDavid LenehanKatie KellySussie HowardRon

14 ORDER ROWS: ‘ORDER BY’ List the Lastnames and Firstnames of all candidates. Order the candidates in alphabetical order by their last name. select canlastname, canfirstname from CARNIE_MYCAREER_CANDIDATE ORDER BY canlastname; canLastNamecanFirstName CarnieChris HowardRon KellySussie LenehanKatie SaddingtonDavid

15 CALCULATING PostgreSQL can even do calculations (Such as divide, times and present them for you! Maybe you want to work out how much money the jobs available will be earning per week instead of per year! (Assumption that there are 52 paid weeks per year) select jobposition, jobsalary / 52 AS SalaryPerWeek FROM CARNIE_MYCAREER_JOB ; ‘AS’ allows you to name your calculation results to whatever you wish. In this case we rename it to salaryperweek jobPositionSalaryPerWeek Senior961 CFO2307 Jr865 Reception384 Graduate730 Team Member1634.. etc …

16 Two ways to use count: COUNT(*) – This is used to count ALL of the rows. Remember * means everything… Example: How many candidates are registered? Select count(*) from CARNIE_MYCAREER_CANDIDATE; COUNT(NameofCollumn) – This can be used to count the number of non null values in the column. See the next slide … BUILT IN FUNCTIONS: COUNT count 5

17 The second way to use count: COUNT(NameofCollumn) – This can be used to count the number of non null values in the column. Here is an example of using it with DISTINCT in order to see how many different types of values are in a specified collumn. Example: How many different countries does MyCareer cater to? Select count(distinct loccountry) from CARNIE_MYCAREER_LOCATION ; If we did not use the distinct, it would return the same values as COUNT(*) since we have no NULL data values in this database. Thus COUNT(Collumn) had no use for us in this example. BUILT IN FUNCTIONS: COUNT count 4

18 OTHER BUILT IN FUNCTIONS: MAX, MIN, AVG & SUM The above built in functions are mostly only useful when dealing with numbers such as price, salaries, quantities etc. What is the highest Salary? select MAX(jobsalary) from carnie_mycareer_job; What is the name of the job of the lowest Salary? select MIN(jobsalary) from carnie_mycareer_job; What is the average Salary? select AVG(jobsalary) from carnie_mycareer_job; SUM (jobsalary) could be used to add all of the salaries together. Max 120000 Min 20000 Avg 59777.7777777778

19 LIKE – PATTERN MATCHING List all candidates with a last name starting with ‘S’ select canfirstname, canlastname from CARNIE_MYCAREER_CANDIDATE where canlastname LIKE 'S%'; List all candidates with a first name which contains ‘ie’ select canfirstname, canlastname from CARNIE_MYCAREER_CANDIDATE WHERE canfirstname LIKE '%ie%'; canFirstNamecanLastName DavidSaddington canFirstNamecanLastName KatieLenehan SussieKelly

20 LIKE – PATTERN MATCHING List all candidates with ‘i’ as the fourth letter in their first name SELECT canfirstname, canlastname FROM CARNIE_MYCAREER_CANDIDATE WHERE canfirstname LIKE '___i%'; List all candidates who do not have ‘i’ in their first name select canfirstname, canlastname from CARNIE_MYCAREER_CANDIDATE WHERE canfirstname NOT LIKE '%i%‘ AND canfirstname NOT LIKE '%I%'; canFirstNamecanLastName ChrisCarnie DavidSaddington KatieLenehan canFirstNamecanLastName RonHoward

21 DISTINCT Distinct removes duplicate rows. For example: List the different countries where jobs are available. SELECT distinct loccountry FROM CARNIE_MYCAREER_LOCATION ; locCountry Australia Iraq New Zealand USA

22 INSERTING ROWS INSERT INTO CARNIE_MYCAREER_CANDIDATE VALUES ('robertp@hotmail.com', 'Robert', 'Pratt', 'M', '09-04-1963'); … OR … INSERT INTO CARNIE_MYCAREER_CANDIDATE (canemail, canfirstname, canlastname, cansex,canbirthyear) VALUES ('robertp@hotmail.com', 'Robert', 'Pratt', 'M', '09-04-19-63'); canemailcanfirstnamecanlastnamecanSexcanbirthyear robertp@hotmail.comRobertPrattM09-04-1963 … etc …

23 Foreign Keys & Natural Joins

24 canemail | canfirstname | canlastname | cansex | canbirthyear -------------------------+--------------+-------------+--------+-------------- chris@hotmail.com | Chris | Carnie | M | 08-01-1981 david11@hotmail.com | David | Saddington | M | 21-06-1980 katiele@hotmail.com | Katie | Lenehan | F | 04-04-1978 sussiekelly@hotmail.com | Sussie | Kelly | F | 24-11-1969 ron12@hotmail.com | Ron | Howard | M | 01-01-2000 Carnie_MyCareer_Candidate jobid | canemail -------+------------------------- 1 | chris@hotmail.com 1 | sussiekelly@hotmail.com 1 | katiele@hotmail.com 2 | sussiekelly@hotmail.com 3 | david11@hotmail.com 5 | chris@hotmail.com 5 | david11@hotmail.com 5 | katiele@hotmail.com 5 | sussiekelly@hotmail.com 8 | david11@hotmail.com 8 | katiele@hotmail.com 9 | chris@hotmail.com 9 | katiele@hotmail.com Carnie_MyCareer_Application Primary Key Foreign Key

25 The Natural Join SELECT jobid, jobsalary, jobposition, locid, secid, canemail FROM carnie_mycareer_job NATURAL JOIN carnie_mycareer_application; jobidjobsalaryjobpositionLocidsecidappidcanemail 150000Senior111chris@hotmail.com 150000Senior112sussiekelly@hotmail.com 150000Senior113katiele@hotmail.com 2120000CFO114sussiekelly@hotmail.com 345000Jr125david11@hotmail.com 538000Graduate316chris@hotmail.com 538000Graduate317david11@hotmail.com 538000Graduate318katiele@hotmail.com 538000Graduate319sussiekelly@hotmail.com 8600002IC2210david11@hotmail.com 8600002IC2211katiele@hotmail.com 975000Logistics4312chris@hotmail.com 975000Logistics4313katiele@hotmail.com

26 Cross Product Joins SELECT jobid, jobsalary, jobposition, locid, secid, canemail FROM carnie_mycareer_job, carnie_mycareer_application WHERE carnie_mycareer_job.jobID = carnie_mycareer_application.jobID; This query produces the same result as a natural join, making use of the cross product function: jobidjobsalaryjobpositionLocidsecidappidcanemail 150000Senior111chris@hotmail.com 150000Senior112sussiekelly@hotmail.com 150000Senior113katiele@hotmail.com 2120000CFO114sussiekelly@hotmail.com 345000Jr125david11@hotmail.com … etc …

27 Cross Product Joins versus Natural Joins Natural join simply takes two or more selected tables and joins them via the common column(s) that each of them share. For example: Select * from carnie_mycareer_job natural join carnie_mycareer_application; The common column in this case is “jobID” (see previous slides). Cross product joins essentially do the same thing, however, the user must specify the join: Select * from carnie_mycareer_job, carnie_mycareer_application Where carnie_mycareer_job.jobID = carnie_mycareer_application.jobID

28 Entities & Relationships

29 SecID SecTitle SecDesc Sector JobID JobPostDate JobSalary JobPosition LocID* SecID* Job Desc Job Foreign Key The Sector – Job 1:m relationship secidjobidjobpositionjobsalary 11Senior50000 12CFO120000 23Jr45000 34Reception20000 15Graduate38000 46Team Member85000 47Team Member45000 282IC60000 39Logistics75000 secidsectitle 1Accounting 2IT 3Administration 4Construction 5Retail

30 JobID JobPostDate JobSalary JobPosition LocID* SecID* Job Desc Job LocID LocCity LocCountry Location Foreign Key The Location – Job 1:m relationship locidjobidjobpositionjobsalary 11Senior50000 12CFO120000 13Jr45000 24Reception20000 35Graduate38000 46Team Member85000 57Team Member45000 282IC60000 49Logistics75000 locidloccityloccountrylocpostcode 1SydneyAustraliaNew South Wales 2WellingtonNew Zealand 3AlbanyUSANew York 4BaghdadIraq 5CanberraAustraliaACT

31 CanEmail CanFirstName CanLastName CanSex CanBirthYear Candidate JobID* CanEmail* Application JobID JobPostDate JobSalary JobPosition LocID* SecID* Job Desc Job canemail | canfirstname | canlastname |cangender| canbirthyear -------------------------+---------------+------------+---------+------------- chris@hotmail.com | Chris | Carnie | M | 08-01-1981 david11@hotmail.com | David | Saddington | M | 21-06-1980 katiele@hotmail.com | Katie | Lenehan | F | 04-04-1978 sussiekelly@hotmail.com | Sussie | Kelly | F | 24-11-1969 ron12@hotmail.com | Ron | Howard | M | 01-01-2000 jobid | canemail ------+------------------------- 1 | chris@hotmail.com 1 | sussiekelly@hotmail.com 1 | katiele@hotmail.com 2 | sussiekelly@hotmail.com 3 | david11@hotmail.com 5 | chris@hotmail.com 5 | david11@hotmail.com 5 | katiele@hotmail.com 5 | sussiekelly@hotmail.com 8 | david11@hotmail.com jobid | jobpostdate | jobsalary | jobposition | locid | secid | jobdesc -------+-------------+-----------+-------------+-------+-------+------------------------------------------------ 1 | 01-01-2005 | 50000 | Senior | 1 | 1 | Account manager, blue chip company 2 | 01-01-2005 | 120000 | CFO | 1 | 1 | CFO of big four bank 3 | 01-03-2005 | 45000 | Jr | 1 | 2 | Entry level Database position. Great start!! 4 | 05-03-2005 | 20000 | Reception | 2 | 3 | Basic office admin, phones and coffee making 5 | 07-02-2005 | 38000 | Graduate | 3 | 1 | Great start for a fresh graduate (5 rows) Foreign Key Dependant Entities The Job – Application – Candidate 1:m relationship

32 CanEmail CanFirstName CanLastName CanSex CanBirthYear Candidate JobID* CanEmail* Application JobID JobPostDate JobSalary JobPosition LocID* SecID* Job Desc Job The Job – Candidate m:m relationship jobid | jobpostdate | jobsalary | jobposition | -------+-------------+-----------+-------------+ 1 | 01-01-2005 | 50000 | Senior | 2 | 01-01-2005 | 120000 | CFO | 3 | 01-03-2005 | 45000 | Jr | 4 | 05-03-2005 | 20000 | Reception | 5 | 07-02-2005 | 38000 | Graduate | (5 rows) jobid | canemail ------+------------------------- 1 | chris@hotmail.com 1 | sussiekelly@hotmail.com 1 | katiele@hotmail.com 2 | sussiekelly@hotmail.com 3 | david11@hotmail.com 5 | chris@hotmail.com 5 | david11@hotmail.com 5 | katiele@hotmail.com 5 | sussiekelly@hotmail.com 8 | david11@hotmail.com (10 rows) canemail | canfirstname | -------------------------+--------------+ chris@hotmail.com | Chris | david11@hotmail.com | David | katiele@hotmail.com | Katie | sussiekelly@hotmail.com | Sussie | ron12@hotmail.com | Ron | (5 rows)

33 The Job – Candidate m:m relationship (cont.) As demonstrated by the ERD on the previous slide, the links between the separate entities show that one candidate can have many applications, while one job can also have many applications. Furthermore, as a result of this link, a candidate can have one application to one specific job, however, can have numerous applications, each for a different job. Therefore a many- to-many relationship is formed between candidates and jobs.

34 An example of creating a view CREATE VIEW CREATE VIEW ausjobs (JobID,PostDate,Salary,Position,City,Sector,Description) AS SELECT FROM AS SELECT jobid,jobpostdate,jobsalary,jobposition,loccity,sectitle,jobdesc FROM carnie_mycareer_job job, carnie_mycareer_sector sec, carnie_mycareer_location loc WHEREAND WHERE job.secid = sec.secid AND job.locid = loc.locid AND AND loc.loccountry = 'Australia';

35 An example of querying a view Query exactly as if a table SELECT jobid, postdate, salary, position, city, sector FROM ausjobs WHERE salary >= 50000; jobIDpostdatesalarypositioncitysector 201-01-2005120000CFOSydneyAccounting 101-01-200550000SeniorSydneyAccounting

36 Group by, sub-queries and complex joins

37 The GROUP BY clause is an elementary form of control break reporting. It permits grouping of rows that have the same value for a specified column or columns, and it produces one row for each different value of the grouping column(s) SQL’s built- in functions (COUNT, SUM, AVERAGE, MIN, and MAX) can be used with the GROUP BY clause

38 select SecTitle, AVG(JobSalary) AS AvgSalary from CARNIE_MYCAREER_JOB, CARNIE_MYCAREER_SECTOR where CARNIE_MYCAREER_JOB.SecID= CARNIE_MYC AREER_SECTOR.SecID GROUP BY SecTitle; SecTitleAvgsalary Accounting69333.333333333333 Construction65000.000000000000 Administration47500.000000000000 IT52500.000000000000 GROUP BY Report by sector the average value for salary

39 select SecTitle, COUNT (*), AVG(JobSalary) As AvgSalary from CARNIE_MYCAREER_JOB, CARNIE_MYCAREER_SECTOR where CARNIE_MYCAREER_JOB.SecID= CARNIE_MYCAREER_SECTOR.SecID GROUP BY SecTitle; SecTitleCountAvgsalary Accounting369333.333333333333 Construction265000.000000000000 Administration247500.000000000000 IT252500.000000000000 Another example of GROUP BY – with COUNT(*) Report by Sector the average value for salary

40 select CanEmail, AVG(JobSalary) As AvgSal from CARNIE_MYCAREER_APPLICATION, CARNIE_MYCAREER_JOB where CARNIE_MYCAREER_APPLICATION.JobId = CARNIE_MYC AREER_JOB.JobID GROUP BY CanEmail HAVING COUNT (*) <= 3; CanemailAvgsal chris@hotmail.com 54333.333333333333 david11@hotmail.com47666.666666666667 sussiekelly@hotmail.com69333.333333333333 HAVING – Like WHERE, but after the grouping Show candidates’ email and report those with less than three applications and their average salary

41 Subqueries SELECT JobPosition, JobSalary FROM CARNIE_MYCAREER_JOB WHERE JobSalary > ( SELECT AVG(JobSalary) from CARNIE_MYCAREER_JOB); JobpositionJobsalary CFO120000 Team Member85000 2IC60000 Logistics75000 avg 59777.777777777778 (1 row) Report all Job Positions with a Job Salary greater than the average of all job salaries

42 SELECT LocID FROM CARNIE_MYCAREER_LOCATION WHERE LocCountry = ‘Australia’ and LocID <= all ( SELECT LocID from CARNIE_MYCAREER_LOCATION where LocCountry = ‘Australia’); LocID 1 (1 row) locid 1 5 (2 rows) Subquery - returns one value Returns

43 SELECT JobPosition, JobSalary FROM CARNIE_MYCAREER_JOB WHERE JobSalary >= ( SELECT max(JobSalary) from CARNIE_MYCAREER_JOB where LocID = 1); max 120000 (1 row) Using subqueries to find the maximum (or minimum) JobpositionJobsalary CFO120000 Team Member85000 Logistics75000

44 SELECT JobPosition, JobSalary FROM CARNIE_MYCAREER_JOB WHERE JobSalary >= ALL ( SELECT JobSalary from CARNIE_MYCAREER_JOB where LocID = 1); jobsalary 50000 120000 45000 (3 rows) Alternate way to find the maximum (or minimum): “ALL” JobpositionJobsalary CFO120000 Team Member85000 Logistics75000

45 SELECT JobPosition, JobSalary FROM CARNIE_MYCAREER_JOB WHERE JobSalary <= ALL (SELECT JobSalary from CARNIE_MYCAREER_JOB); That’s equivalent to … Where JobSalary = (SELECT min(JobSalary) from CARNIE_MYCAREER_JOB); Another example jobpositionjobsalary Reception20000 jobsalry 50000 120000 45000 20000 38000 85000 45000 60000 75000 (9 rows) min 20000 (1 row)

46 SELECT DISTINCT JobPosition FROM CARNIE_MYCAREER_JOB WHERE LocID = ANY ( SELECT LocID FROM CARNIE_MYCAREER_LOCATION WHERE LocCountry = 'Australia‘ ); Any operator locid 1 5 (2 rowms) jobposition CFO Jr Senior Team Member

47 In: an Alternate to ANY SELECT DISTINCT JobPosition FROM CARNIE_MYCAREER_JOB WHERE LocID IN ( SELECT LocID FROM CARNIE_MYCAREER_Location WHERE LocCountry = 'Australia‘ ); jobposition CFO Jr Senior Team Member locid 1 5 (2 rowms)

48 LEFT Outer Join SELECT jobID, appID, canEmail, jobsalary, jobposition FROM carnie_mycareer_application LEFT JOIN carnie_mycareer_job USING (jobid) where jobsalary < 60000; jobIDappIDcanEmailjobSalaryjobPosition 11 chris@hotmail.com50000 Senior 12sussiekelly@hotmail.com50000 Senior 13katiele@hotmail.com50000 Senior 35david11@hotmail.com45000Jr 56 chris@hotmail.com38000Graduate … etc … Not a great example as all applications match a job

49 RIGHT Outer Join SELECT jobID, appID, canEmail, jobsalary, jobposition FROM carnie_mycareer_application RIGHT JOIN carnie_mycareer_job USING (jobid) where jobsalary < 60000; jobIDappIDcanEmailjobSalaryjobPosition 11 chris@hotmail.com50000 Senior … etc … 420000Reception 56 chris@hotmail.com38000Graduate 57david11@hotmail.com38000Graduate 58katiele@hotmail.com38000Graduate 59sussiekelly@hotmail.com38000Graduate 745000Team Member

50 SELF Join SELECT job.jobid, can1.canlastname AS applicant1, can2.canlastname AS applicant2 FROM carnie_mycareer_candidate can1, carnie_mycareer_candidate can2, carnie_mycareer_application app1, carnie_mycareer_application app2, carnie_mycareer_job job WHERE can1.canemail = app1.canemail ANDcan2.canemail = app2.canemail AND job.jobID = app1.jobID AND job.jobID = app2.jobID AND app1.jobID = 9 AND app2.canemail < app1.canemail; Joining a table to itself. In this case, carnie_mycareer_candidate is joined to itself jobIDApplicant1Applicant2 9LenehanCarnie Produces

51 Data Integrity with SQL

52 jobid | jobpostdate | jobsalary | jobposition | locid | secid | jobdesc -------+-------------+-----------+-------------+-------+-------+----------------------------------------------- 1 | 01-01-2005 | 50000 | Senior | 1 | 1 | Account manager, blue chip company 2 | 01-01-2005 | 120000 | CFO | 1 | 1 | CFO of big four bank 3 | 01-03-2005 | 45000 | Jr | 1 | 2 | Entry level Database position. Great start!! 4 | 05-03-2005 | 20000 | Reception | 2 | 3 | Basic office admin, phones and coffee making 5 | 07-02-2005 | 38000 | Graduate | 3 | 1 | Great start for a fresh graduate 6 | 25-06-2005 | 85000 | Team Member | 4 | 4 | Help rebuild peoples lives in Iraq 7 | 25-06-2005 | 45000 | Team Member | 5 | 4 | Anti-Terror proofing parliment house 8 | 25-06-2005 | 60000 | 2IC | 2 | 2 | Second in command of well run NZ IT company 9 | 25-06-2005 | 75000 | Logistics | 4 | 3 | Team player with excellent organisation skills Job locid | loccity | loccountry | locstate -------+------------+-------------+----------------- 1 | Sydney | Australia | New South Wales 2 | Wellington | New Zealand | 3 | Albany | USA | New York 4 | Baghdad | Iraq | 5 | Canberra | Australia | ACT Loc Foreign Key Primary Key

53 Creating the linked tables: CREATE TABLE CARNIE_MYCAREER_JOB ( ….Individual fields go here LocIDINTEGER NOT NULL, CONSTRAINT PKJobID PRIMARY KEY (JobID), CONSTRAINT FKLocID FOREIGN KEY (LocID) REFERENCES CARNIE_MYCAREER _LOCATION ON DELETE RESTRICT, CONSTRAINT FKSecID FOREIGN KEY (SecID) REFERENCES CARNIE_MYCAREER_SECTOR ON DELETE RESTRICT ); CREATE TABLE CARNIE_MYCAREER_LOCATION ( LocIDINTEGER NOT NULL, LocCityVARCHAR NOT NULL, LocCountryVARCHAR NOT NULL, CONSTRAINT PKLocID PRIMARY KEY (LocID) );

54 CHECK constraints example CREATE TABLE Carnie_MyCareer_Candidate ( … fields go here … CONSTRAINT Invalid_CanGender CHECK (CanGender IN (‘M’,’F’)) ); Name of constraint Checks that no error has been made when entering the candidates’ gender (CanGender)

55 SQL Syntax for Actions CREATE TABLE CARNIE_MYCAREER_APPLICATION ( JobIDINTEGER NOT NULL, CanEmailINTEGER NOT NULL, CONSTRAINT PkApplication PRIMARY KEY (JobID, CanEmail), CONSTRAINT FkJobID FOREIGN KEY (JobID) REFERENCES CARNIE_MYCAREER_JOB ON DELETE CASCADE ON UPDATE CASCADE ON UPDATE CASCADE, CONSTRAINT FkCanID FOREIGN KEY (CanEmail) REFERENCES CARNIE_MYCAREER_CANDIDATE ON DELETE CASCADE ON UPDATE CASCADE ); Will delete all applications to a job if the job is deleted

56 Normalization

57 Problem: Some of the non-key fields only depends on part of the key. Solution: split into two or more tables (see next slide) An example of 1st normal form (1NF) JobIDLocIDJobPositionJobSalarySecNameLocCityLocCountry 11Senior50000AccountingSydneyAustralia 2 1CFO 120000AccountingSydneyAustralia 3 1Jr 45000ITSydneyAustralia 4 2 Reception 20000AdministrationWellingtonNew Zealand Primary Key JobID JobPosition, JobSalary, Sector LocID LocCity, LocCountry

58 After splitting the table on the previous slide … JobIDJobPositionJobSalarySecNameLocID 1Senior50000Accounting1 2 CFO 120000Accounting1 3 Jr 45000IT1 4 Reception 20000Administration2 Primary Key LocIDLocCityLocCountry 1SydneyAustralia 2WellingtonNew Zealand Primary Key Foreign Key

59 Something in the non- key is determined by (the key and also) something else in the non-key. JobPosition JobSalary Problem with second normal form JobIDJobPositionJobSalary 10Burger Flipper Grade 130000 20Burger Flipper Grade 130000 30Burger Flipper Grade 245000 40 Burger Flipper Grade 245000 Primary Key Solution: split into two or more tables (see next slide) Suppose that for a given job title the salary is always the same … Primary Key

60 After splitting the table on the previous slide … JobIDJobPosition 10Burger Flipper Grade 1 20Burger Flipper Grade 1 30Burger Flipper Grade 2 40 Burger Flipper Grade 2 Primary Key JobPositionJobSalary Burger Flipper Grade 130000 Burger Flipper Grade 245000 Foreign Key Primary Key

61 Canemail  everything else …but also … CanePhone  Canemail CaneTaxNo  Canemail Something in the non-key also determines the key. Problem with third normal form Primary Key Solution: split into two or more tables (see next slide) More than one attribute can determine a person or thing … … Primary Key CanemailcanLastNamecanSexCanePhoneCaneTaxNo chris@hotmail.comCarnieM 0414 123456123456 david11@hotmail.comSaddingtonM0414 456123654321 sussiekelly@hotmail.comKellyF0414 654321135791

62 After splitting the table on the previous slide … Primary Key The above tables are in Boyce Codd Normal Form (BCNF) CanemailcanLastNamecanSex chris@hotmail.comCarnieM david11@hotmail.comSaddingtonM sussiekelly@hotmail.comKellyF CanemailCanePhoneCaneTaxNo chris@hotmail.com 0414 123456123456 david11@hotmail.com0414 456123654321 sussiekelly@hotmail.com0414 654321135791

63 An example of creating a view CREATE VIEW CREATE VIEW ausjobs (JobID,PostDate,Salary,Position,City,Sector,Description)AS SELECT SELECT jobid,jobpostdate,jobsalary,jobposition,loccity,sectitle,jobdesc FROM FROM carnie_mycareer_job job, carnie_mycareer_sector sec, carnie_mycareer_location loc WHEREAND WHERE job.secid = sec.secid AND job.locid = loc.locid AND AND loc.loccountry = 'Australia';

64 An example of querying a view Query exactly as if a table SELECT jobid, postdate, salary, position, city, sector FROM ausjobs WHERE salary >= 50000; jobIDpostdatesalarypositioncitysector 201-01-2005120000CFOSydneyAccounting 101-01-200550000SeniorSydneyAccounting

65


Download ppt "Helen Spiropoulos Benjamin Mills Nicoleta Bikrogiannis Jessica El-chaar."

Similar presentations


Ads by Google