Helen Spiropoulos Benjamin Mills Nicoleta Bikrogiannis Jessica El-chaar.
-
Upload
hugh-miles -
Category
Documents
-
view
219 -
download
6
Transcript of Helen Spiropoulos Benjamin Mills Nicoleta Bikrogiannis Jessica El-chaar.
•Helen Spiropoulos•Benjamin Mills
•Nicoleta Bikrogiannis•Jessica El-chaar
•Models the mycareer website (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
•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
LocIDLocCityLocCountry
Location
CanEmailCanFirstNameCanLastNameCanSexCanBirthYear
Candidate
SecIDSecTitleSecDesc
Sector
JobID*CanEmail*
Application
JobIDJobPostDateJobSalaryJobPositionLocID*SecID*JobDesc
Job
SQL Queries on a Single Entity/ Table
Projecting tables’ contents using ‘SELECT’
select * select * fromfrom carnie_mycareer_candidate;carnie_mycareer_candidate;
select sectitle, secdesc from select sectitle, secdesc from carnie_mycareer_sectorcarnie_mycareer_sector;;
Selecting Everything in a table
Selecting a couple of columns
from a table
canemail canfirstname canlastname canSex canbirthyear
[email protected] Chris Carnie M 08-01-1981
[email protected] David Saddington M 21-06-1980
[email protected] Katie Lenehan F 04-04-1978
[email protected] Sussie Kelly F 24-11-1969
[email protected] Ron Howard M 01-01-2000
secTitle secDesc
Accounting All aspects of accounting disipline
IT IT industry including sales, technical, support
Administration Reception, admin and supporting roles
Construction Building, Plumbing, Electrical, Labouring
Retail Shop assistance, customer service
Restrict using ‘WHERE’
** Show all male candidates
Restricts the rows shown
select * from carnie_mycareer_candidate where cansex = 'M';
canEmail canFirstName
canLastName canSex canBirth
[email protected] Chris Carnie M 08-01-1981
[email protected] David Saddington M 21-06-1980
[email protected] Katie Lenehan F 04-04-1978
[email protected] Sussie Kelly F 24-11-1969
[email protected] Ron Howard M 01-01-2000
canEmail canFirstName
canLastName canSex canBirth
[email protected] Chris Carnie M 08-01-1981
[email protected] David Saddington M 21-06-1980
[email protected] Ron Howard M 01-01-2000
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';
canLastName canEmail
Carnie [email protected]
Howard [email protected]
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!
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;
canLastName canEmail
Carnie [email protected]
Saddington [email protected]
Lenehan [email protected]
Kelly [email protected]
Howard [email protected]
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;
jobPosition jobSalary
Senior 50000
CFO 120000
Jr 45000
Reception 20000
Team Member 45000
2IC 60000
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;
jobPosition jobSalary
Graduate 38000
Team Member 85000
Logistics 75000
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;
canFirstName canLastName
Chris Carnie
David Saddington
Katie Lenehan
Sussie Kelly
Ron HowardcanLastName canFirstName
Carnie Chris
Saddington David
Lenehan Katie
Kelly Sussie
Howard Ron
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;
canLastName canFirstName
Carnie Chris
Howard Ron
Kelly Sussie
Lenehan Katie
Saddington David
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
jobPosition SalaryPerWeek
Senior 961
CFO 2307
Jr 865
Reception 384
Graduate 730
Team Member 1634
.. etc …
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
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
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
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%';
canFirstName canLastName
David Saddington
canFirstName canLastName
Katie Lenehan
Sussie Kelly
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%';
canFirstName canLastName
Chris Carnie
David Saddington
Katie Lenehan
canFirstName canLastName
Ron Howard
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
INSERTING ROWS
INSERT INTO CARNIE_MYCAREER_CANDIDATE VALUES ('[email protected]', 'Robert', 'Pratt', 'M', '09-04-1963');
… OR …
INSERT INTO CARNIE_MYCAREER_CANDIDATE (canemail, canfirstname, canlastname, cansex,canbirthyear) VALUES ('[email protected]', 'Robert', 'Pratt', 'M', '09-04-19-63');
canemail canfirstname canlastname canSex canbirthyear
[email protected] Robert Pratt M 09-04-1963
… etc …
Foreign Keys & Natural Joins
canemail | canfirstname | canlastname | cansex | canbirthyear-------------------------+--------------+-------------+--------+-------------- [email protected] | Chris | Carnie | M | 08-01-1981 [email protected] | David | Saddington | M | 21-06-1980 [email protected] | Katie | Lenehan | F | 04-04-1978 [email protected] | Sussie | Kelly | F | 24-11-1969 [email protected] | Ron | Howard | M | 01-01-2000
Carnie_MyCareer_Candidate
jobid | canemail-------+------------------------- 1 | [email protected] 1 | [email protected] 1 | [email protected] 2 | [email protected] 3 | [email protected] 5 | [email protected] 5 | [email protected] 5 | [email protected] 5 | [email protected] 8 | [email protected] 8 | [email protected] 9 | [email protected] 9 | [email protected]
Carnie_MyCareer_Application
Primary Key
Foreign Key
The Natural Join
SELECT jobid, jobsalary, jobposition, locid, secid, canemail FROM carnie_mycareer_job NATURAL JOIN carnie_mycareer_application;
jobid jobsalary jobposition Locid secid appid canemail
1 50000 Senior 1 1 1 [email protected]
1 50000 Senior 1 1 2 [email protected]
1 50000 Senior 1 1 3 [email protected]
2 120000 CFO 1 1 4 [email protected]
3 45000 Jr 1 2 5 [email protected]
5 38000 Graduate 3 1 6 [email protected]
5 38000 Graduate 3 1 7 [email protected]
5 38000 Graduate 3 1 8 [email protected]
5 38000 Graduate 3 1 9 [email protected]
8 60000 2IC 2 2 10 [email protected]
8 60000 2IC 2 2 11 [email protected]
9 75000 Logistics 4 3 12 [email protected]
9 75000 Logistics 4 3 13 [email protected]
Cross Product Joins
SELECT jobid, jobsalary, jobposition, locid, secid, canemail FROM carnie_mycareer_job, carnie_mycareer_applicationWHERE 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:
jobid jobsalary jobposition Locid secid appid canemail
1 50000 Senior 1 1 1 [email protected]
1 50000 Senior 1 1 2 [email protected]
1 50000 Senior 1 1 3 [email protected]
2 120000 CFO 1 1 4 [email protected]
3 45000 Jr 1 2 5 [email protected]
… etc …
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
Entities & Relationships
SecIDSecTitleSecDesc
SectorJobIDJobPostDateJobSalaryJobPositionLocID*SecID*Job Desc
Job
Foreign Key
The Sector – Job 1:m relationship
secid jobid jobposition jobsalary
1 1 Senior 50000
1 2 CFO 120000
2 3 Jr 45000
3 4 Reception 20000
1 5 Graduate 38000
4 6 Team Member 85000
4 7 Team Member 45000
2 8 2IC 60000
3 9 Logistics 75000
secid sectitle
1 Accounting
2 IT
3 Administration
4 Construction
5 Retail
JobIDJobPostDateJobSalaryJobPositionLocID*SecID*Job Desc
Job
LocIDLocCityLocCountry
Location
Foreign Key
The Location – Job 1:m relationship
locid jobid jobposition jobsalary
1 1 Senior 50000
1 2 CFO 120000
1 3 Jr 45000
2 4 Reception 20000
3 5 Graduate 38000
4 6 Team Member 85000
5 7 Team Member 45000
2 8 2IC 60000
4 9 Logistics 75000
locid loccity loccountry locpostcode
1 Sydney Australia New South Wales
2 Wellington New Zealand
3 Albany USA New York
4 Baghdad Iraq
5 Canberra Australia ACT
CanEmailCanFirstNameCanLastNameCanSexCanBirthYear
Candidate
JobID*CanEmail*
Application
JobIDJobPostDateJobSalaryJobPositionLocID*SecID*Job Desc
Job
canemail | canfirstname | canlastname |cangender| canbirthyear-------------------------+---------------+------------+---------+------------- [email protected] | Chris | Carnie | M | 08-01-1981 [email protected] | David | Saddington | M | 21-06-1980 [email protected] | Katie | Lenehan | F | 04-04-1978 [email protected] | Sussie | Kelly | F | 24-11-1969 [email protected] | Ron | Howard | M | 01-01-2000
jobid | canemail ------+------------------------- 1 | [email protected] 1 | [email protected] 1 | [email protected] 2 | [email protected] 3 | [email protected] 5 | [email protected] 5 | [email protected] 5 | [email protected] 5 | [email protected] 8 | [email protected]
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
Foreign Key
Dependant EntitiesThe Job – Application – Candidate 1:m relationship
CanEmailCanFirstNameCanLastNameCanSexCanBirthYear
Candidate
JobID*CanEmail*
Application
JobIDJobPostDateJobSalaryJobPositionLocID*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 | [email protected] 1 | [email protected] 1 | [email protected] 2 | [email protected] 3 | [email protected] 5 | [email protected] 5 | [email protected] 5 | [email protected] 5 | [email protected] 8 | [email protected] (10 rows)
canemail | canfirstname |-------------------------+--------------+ [email protected] | Chris | [email protected] | David | [email protected] | Katie | [email protected] | Sussie | [email protected] | Ron |(5 rows)
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.
An example of creating a view
CREATE VIEW CREATE VIEW ausjobs
(JobID,PostDate,Salary,Position,City,Sector,Description)
AS SELECT AS SELECT jobid,jobpostdate,jobsalary,jobposition,loccity,sectitle,jobdesc
FROMFROM carnie_mycareer_job job, carnie_mycareer_sector sec,
carnie_mycareer_location loc
WHEREWHERE job.secid = sec.secid ANDAND job.locid = loc.locid
ANDAND loc.loccountry = 'Australia';
An example of querying a view
•Query exactly as if a table
SELECT jobid, postdate, salary, position, city, sector FROM ausjobs WHERE
salary >= 50000;
jobID postdate salary position city sector
2 01-01-2005 120000 CFO Sydney Accounting
1 01-01-2005 50000 Senior Sydney Accounting
Group by, sub-queries and complex joins
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
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;
SecTitle Avgsalary
Accounting 69333.333333333333
Construction 65000.000000000000
Administration 47500.000000000000
IT 52500.000000000000
GROUP BY
Report by sector the average value for salary
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;
SecTitle Count Avgsalary
Accounting 3 69333.333333333333
Construction 2 65000.000000000000
Administration 2 47500.000000000000
IT 2 52500.000000000000
Another example of GROUP BY – with COUNT(*)
Report by Sector the average value for salary
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;
Canemail Avgsal
[email protected] 54333.333333333333
[email protected] 47666.666666666667
[email protected] 69333.333333333333
HAVING – Like WHERE, but after the grouping
Show candidates’ email and report those with less than three applications and their average salary
Subqueries
SELECT JobPosition, JobSalary
FROM CARNIE_MYCAREER_JOB
WHERE JobSalary > (
SELECT AVG(JobSalary) from CARNIE_MYCAREER_JOB);
Jobposition Jobsalary
CFO 120000
Team Member 85000
2IC 60000
Logistics 75000
avg
59777.777777777778
(1 row)
Report all Job Positions with a Job Salary greater than the average of all job salaries
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
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)
Jobposition Jobsalary
CFO 120000
Team Member 85000
Logistics 75000
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”
Jobposition Jobsalary
CFO 120000
Team Member 85000
Logistics 75000
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
jobposition jobsalary
Reception 20000
jobsalry
50000
120000
45000
20000
38000
85000
45000
60000
75000
(9 rows)min
20000
(1 row)
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
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)
LEFT Outer Join
SELECT jobID, appID, canEmail, jobsalary, jobposition
FROM carnie_mycareer_application LEFT JOIN carnie_mycareer_job
USING (jobid)
where jobsalary < 60000;
jobID appID canEmail jobSalary jobPosition
1 1 [email protected] 50000 Senior
1 2 [email protected] 50000 Senior
1 3 [email protected] 50000 Senior
3 5 [email protected] 45000 Jr
5 6 [email protected] 38000 Graduate
… etc …
Not a great example as all applicationsmatch a job
RIGHT Outer Join
SELECT jobID, appID, canEmail, jobsalary, jobposition
FROM carnie_mycareer_application RIGHT JOIN carnie_mycareer_job
USING (jobid)
where jobsalary < 60000;
jobID appID canEmail jobSalary jobPosition
1 1 [email protected] 50000 Senior
… etc …
4 20000 Reception
5 6 [email protected] 38000 Graduate
5 7 [email protected] 38000 Graduate
5 8 [email protected] 38000 Graduate
5 9 [email protected] 38000 Graduate
7 45000 Team Member
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
jobID Applicant1 Applicant2
9 Lenehan CarnieProduces
Data Integrity with SQL
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 KeyPrimary Key
Creating the linked tables:
CREATE TABLE CARNIE_MYCAREER_JOB(….Individual fields go hereLocID INTEGER 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(LocID INTEGER NOT NULL,LocCity VARCHAR NOT NULL,LocCountry VARCHAR NOT NULL, CONSTRAINT PKLocID PRIMARY KEY (LocID) );
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)
SQL Syntax for Actions
CREATE TABLE CARNIE_MYCAREER_APPLICATION ( JobID INTEGER NOT NULL, CanEmail INTEGER NOT NULL, CONSTRAINT PkApplication PRIMARY KEY (JobID, CanEmail),
CONSTRAINT FkJobID FOREIGN KEY (JobID) REFERENCES CARNIE_MYCAREER_JOB
ON DELETE CASCADEON DELETE CASCADEON UPDATE CASCADEON UPDATE CASCADE,
CONSTRAINT FkCanID FOREIGN KEY (CanEmail) REFERENCES CARNIE_MYCAREER_CANDIDATE
ON DELETE CASCADEON DELETE CASCADEON UPDATE CASCADEON UPDATE CASCADE
);
Will delete all applications to a job if the job is deleted
Normalization
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)
JobID LocID JobPosition JobSalary SecName LocCity LocCountry
1 1 Senior 50000 Accounting Sydney Australia
2 1 CFO 120000 Accounting Sydney Australia
3 1 Jr 45000 IT Sydney Australia
4 2 Reception 20000 Administration Wellington New Zealand
Primary Key
JobID JobPosition, JobSalary, Sector
LocID LocCity, LocCountry
After splitting the table on the previous slide …
JobID JobPosition JobSalary SecName LocID
1 Senior 50000 Accounting 1
2 CFO 120000 Accounting 1
3 Jr 45000 IT 1
4 Reception 20000 Administration 2
Primary Key
LocID LocCity LocCountry
1 Sydney Australia
2 Wellington New Zealand
Primary Key
Foreign Key
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
JobID JobPosition JobSalary
10 Burger Flipper Grade 1 30000
20 Burger Flipper Grade 1 30000
30 Burger Flipper Grade 2 45000
40 Burger Flipper Grade 2 45000
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
After splitting the table on the previous slide …
JobID JobPosition
10 Burger Flipper Grade 1
20 Burger Flipper Grade 1
30 Burger Flipper Grade 2
40 Burger Flipper Grade 2
Primary Key
JobPosition JobSalary
Burger Flipper Grade 1 30000
Burger Flipper Grade 2 45000
Foreign Key
Primary Key
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
Canemail canLastName canSex CanePhone CaneTaxNo
[email protected] Carnie M 0414 123456 123456
[email protected] Saddington M 0414 456123 654321
[email protected] Kelly F 0414 654321 135791
After splitting the table on the previous slide …
Primary Key Primary Key
The above tables are in Boyce Codd Normal Form (BCNF)
Canemail canLastName canSex
[email protected] Carnie M
[email protected] Saddington M
[email protected] Kelly F
Canemail CanePhone CaneTaxNo
[email protected] 0414 123456 123456
[email protected] 0414 456123 654321
[email protected] 0414 654321 135791
An example of creating a view
CREATE VIEW CREATE VIEW ausjobs
(JobID,PostDate,Salary,Position,City,Sector,Description)
AS AS
SELECT SELECT jobid,jobpostdate,jobsalary,jobposition,loccity,sectitle,jobdesc
FROMFROM carnie_mycareer_job job, carnie_mycareer_sector sec,
carnie_mycareer_location loc
WHEREWHERE job.secid = sec.secid ANDAND job.locid = loc.locid
ANDAND loc.loccountry = 'Australia';
An example of querying a view
•Query exactly as if a table
SELECT jobid, postdate, salary, position, city, sector
FROM ausjobs
WHERE salary >= 50000;
jobID postdate salary position city sector
2 01-01-2005 120000 CFO Sydney Accounting
1 01-01-2005 50000 Senior Sydney Accounting