You are on page 1of 71

Topic 05

Surrogate Keys vs Natural Keys


Entity Relationship Modelling and Diagrams
SQL

1
More on Keys
Surrogates | Natural | Composite

2
Recap on Keys
Consider Unit codes and descriptions for university units.
UnitCode Description
INS2156 Database Analysis and Design
INS2055 Database Systems
INS2063 Business Intelligence

Imagine that you see this data in the Enrolment table (similar to casting table in Movie Database)
UnitCode StudentID
INS2055 16071234
INS2063 16071234
INS2063 17075678

You can easily see that student 16071234 is enrolled in INS2055 & INS2063

You can easily see that INS2063 has 2 enrolments

3
Surrogate Keys
However, so far in Access we have used tables that have surrogate
keys

A surrogate key is
– A key that has no real world / business meaning
– Is usually numeric
– Is often a sequential number supplied by the RDBMS
(e.g. the AutoNumber option in field settings)

Many databases around the world have been created with all their
tables using surrogate keys

4
Surrogate Keys (cont.)
However, if a Unit table uses a surrogate key (no business meaning), we may have this:
Unit ID UnitCode Description
1 INT1004 Basic Informatics
2 INS2055 Database Systems
3 INS2063 Business Intelligence

This is the data in the Enrolment table


UnitCode StudentID
1 16071234
3 16071234
3 17075678

It is more difficult to see that student 16071234 is enrolled in It is more

difficult to see that has 2 enrolments

5
Natural Keys
If our database used the university UnitCode as it's primary key, then it
would be considered to be a Natural Key
UnitCode Description

INS2055 Database Systems


INS2063 Business Intelligence

Some database developers suggest that Natural Keys are


good/better and should be used in a RDBMS

There are many who disagree…

6
Key Wars

Arguments about Natural Keys Arguments about Surrogate Keys


Advantages Advantages
– They have business meaning – The key has no business meaning
– Non IT people understand what the keys mean – The key should never have to change (I.e. if the business rules change)
– The key provides information to the user without having to perform lookups:
Fred Blogs worked 8 hours this week on running tutorials in unit
Disadvantages
– You will not 'know' if invalid values (non existent unit codes) have been
entered.
Disadvantages
– Tutor 1 worked 8 hours this week on running tutorials in unit 74 (what is unit
– Natural Keys may change: The university renames all INF units to ICT 74??)
units ( becomes ICT10002)
– They value of a natural key may be long and cumbersome
http://stackoverflow.com/questions/63090/surrogate-vs-natural-
– A key value containing text may not be processed as quickly as a numeric business-keys
value
http://www.agiledata.org/essays/keys.html

Clever people from both sides of the fence argue about this topic.
https://en.wikipedia.org/wiki/Timeline_of_country_and_capital_changes

7
Modelling with Natural Keys
In the coming weeks, we commence modelling business requirements using Entity
Relationship Diagrams (ERDs)

When modelling business requirements


– You speak to clients
– You use terms applicable to their business
– You do not invent terms / fields that do not match their business
– You use Natural Keys

For example, if a small college teaches 20 units, that business may not have/use unit codes.
– When identifying a unit, they simply use natural keys such as Unit Name
– While Unit Name may seem obviously inadequate for a large database, it may be sufficient for our
Modelling requirements
– Forcing a term such as Unit Code into the conversation may confuse clients

8
Modelling with Natural Keys (cont.)
Modelling typically uses Natural Keys

Modelling is sometimes the end of the process

The ER Model that is produced may be the final product


– There is no database
– Both the client and the modeler learn about the business & it’s requirements via the modelling process.

If a Database is required…
– Database implementers can choose to add surrogate keys
– When adding a surrogate key – you do not lose any data
– The natural key data is not removed. It's just not the PK

http://stackoverflow.com/questions/63090/surrogate-vs-natural-business-keys

9
Composite Keys
Natural Keys are sometimes So you may store this data:
Composite Keys BuildingCode RoomNo Capacity HasWindows
BA 302 161 No
BA 404 20 Yes
A composite key is a key made up of
multiple values
The Primary Key for this room would
be BuildingCode + RoomNo.
Consider a room in this university:
BA302
– BA refers to the building that the room is in (Business
This is a single Primary Key. It is
Arts) made up of multiple fields/columns
– 302 refers to a particular room in that building

10
Composite Keys (cont.)
You could take it a step further with BA302
– BA refers to the building that the room is in (Business Arts)
– 3 refers to the floor number within the building
– 02 refers to a particular room in that floor

So you may need to store this data:


BuildingCode LevelNo RoomNo Capacity HasWindows
BA 3 02 161 No
BA 4 04 20 Yes

The Primary Key for this room would be BuildingCode + LevelNo + RoomNo

Advantage: The key is self explanatory / documenting. The key has meaning.
DisAdvantage: The key is long and cumbersome. What happens if they rename BA to BS (Business
School) or BL (Business Law)?

11
Entity Relationship Diagrams (ERDs)
Overview | Entities | Attributes | Relationships | Business Rules | Cardinality |
Instances | Identifiers

12
Getting into Database Design
Creating a table is easy

Creating multiple tables is just as easy

Deciding what data is stored in each table can be a difficult


decision

One way to assist in the design of the database tables is to


use Entity Relationship Models & Diagrams

13
Entity Relationship Model & Diagrams
Entity Relationship Model
– A logical representation of data required by an organisation
– Uses entities to represent people, objects, events…
– Identifies relationships between various entities
– Based around business rules of the organisation

Entity Relationship Diagram


– A graphical representation of a the ERM
– An ER Model / ER Diagram does not actually store any data
– An ER Model is a plan.

14
ERDs vs Access Relationship Diagrams
An Entity-Relationship Diagram (ERD) use natural keys
– No surrogate key ( non real world keys )

An Access Relationship Diagram matches the database


tables
– If surrogate keys are used in a table, then these are used in the
Access RD

An Access Relationship Diagram is not an ERD

15
ERDs
An Entity -Relationship
Diagram (ERD) is a way RatingCode
MovieNo
to express the structure Title Description
of information used by an YearReleased

organization or business
in the form of a diagram MOVIE has RATING

used to assist in
PG Parental Guidance
database design 391 Avatar
231 The Matrix
2009
1999 M Mature Audience

16
Entities
The E-R Diagram has three major components:
The first are ‘Entities’
This diagram has two entities: Subject and Lecturer

SUBJECT LECTURER

17
Attributes
Entities: Subject, Lecturer
Attributes: This diagram has six attributes:
SubjectCode, Title, CreditPoints, LecId, LecName, Age

SubjectCode LecId

Title LecName
CreditPoints Age

SUBJECT LECTURER

18
Relationships
Entities: Subject, Convenor
Attributes: SubjectCode, Title, CreditPoints, LecID…
Relationships: This diagram shows a that there is a relationship
between the entities subject and lecturer

SubjectCode LecId

Title LecName
CreditPoints
Age

SUBJECT Convened By LECTURER

19
Using Sample Data
When building a ER Diagram, SubjectCode LecId
Title
always consider examples of CreditPoints
LecName
Age
data that would be stored in
each entity. SUBJECT Convened by LECTURER

After all, that's why we build a


database – to store data Subject Data
Database, Analysis & Design 12.5
INT1003 Intro to Business Info Systems 12.5

Lecturer Data
207 John Smith 37
119 Jane Pitt 26

20
Business Rules
Consider some business rules:
• A student must only be enrolled in one course at any time
• A student may enrol in many subjects at one time
• An subject must only have one convenor
• An employee must only have one tax file number

Some business rules are common amongst many organisations

Some business rules are unique to a single organisation

Very few organisations have identical business rules


• (Thank goodness – or jobs in the IT industry are in trouble)

Your ERD design and subsequent database design must


meet the needs of the organisation's business rules
21
Business Rules (cont.)
What are all of the organisation's business rules about data requirements?

Where are there business rules stored?


– "Documenting rules and policies of an organization that govern data is exactly
what data modeling is all about” )Hoffer, Prescott, McFadden)

There is an entire industry based around obtaining business rules

Some computing jobs can be 'off-shored'.

Data modelling is not one of those jobs.

22
Business Rules (cont.)
Why is discovering business rules so difficult?
– No single person knows all of the business rules of an organisation
– Individuals can't tell you every rule
– Individuals won't to tell you every rule
– Some will tell you rules that don't exist (Fear, distrust, changing their job…)
– Lack of existing documentation
– Rules change as business needs changes, regulations change, competition changes, technology
changes
– Changes can occur during the modelling phase

Rule gathering topic discussed in future subjects such as RAM (Req. Analysis & Modelling)

This semester in DAD, you will be given all of the business rules 

23
Cardinality Constraints
Cardinality constraints specify how many instances of one entity are related to
instances of another entity

The answer is always One or Many

At many universities:
• How many lecturers convene a single subject? ONE
• How many subjects can a lecturer convene? MANY

The Cardinality is drawn onto the Relationship line


– ONE is drawn as a single line
– MANY is drawn as a crow's foot

24
Determining Cardinality
Let's consider the "convened by"
relationship
Let's consider the LEFT end of the
relationship
Let's consider just ONE instance of a
subject (e.g. )
How many lecturers convene this one
subject? ONE
– Draw the cardinality symbol at the
RIGHT end of the relationship
Now read this diagram left to right
– Start the sentence by using one
instance of the subject entity
"One Subject is convened by ONE
lecturer"
25
Determining Cardinality (cont.)
Let's again consider the "convened by"
relationship
Let's consider the RIGHT end of the
relationship
Let's consider ONE instance of a
lecturer (e.g. Fred Smith)
How many subjects are convened by SUBJECT Convened by LECTURER
this one lecturer? MANY
Draw the cardinality symbol at the LEFT
end of the relationship
Now read this diagram right to left
– Start the sentence by using one instance
of the lecturer entity
"One Lecturer convenes MANY
Subjects"
26
Cardinality Constraints (Summary)
What does this diagram say?
– ONE Subject is convened by ONE Lecturer
– ONE Lecturer convenes MANY Subjects
Always begin each sentence with the word ONE (never begin with "many")
The only difficulty with this diagram is that the relationship name "Convened
by" only reads left to right.
The reader must rephrase those words so that it makes sense when reading
right to left

27
Entity Instances
An entity instance is one set of values for the attributes of an Entity

e.g. This is an instance of the Lecturer Entity


LecId 207
LecName John Smith
Age 37

e.g. This is another instance of the Lecturer Entity

28
Identifiers
An identifier is an attribute(s) that
uniquely identifies an entity
instance
SubjectCode LecId
Same concept as a Primary Key
Title LecName
Rules:
CreditPoints Age
– Every entity must have an identifier
– Every instance of an entity must
have a unique identifier SUBJECT Convened by LECTURER
– No duplicates
– The value of an identifier cannot be
empty / null
– Entity identifiers are underlined on
the ERD

29
Converting ERDs
RDM | Schemas | Nulls | Foreign Keys |

30
Converting ERDs
Modelling is great, but, to be really useful we need to be
able to translate the ERD into a Relational Schema…

31
Revisiting the Relational Data Model (RDM)
Developed by E.F.Codd in 1970

Data represented in the form of 2 dimensional tables

Each two-dimensional table is called a relation

A relation has the following properties:


– A set of uniquely named columns called Attributes
– A list of unnamed/unnumbered rows called Rows (or Tuples)
– The order of the rows is irrelevant.
– If rows are shuffled, the relation still has the same meaning.
– A Row consists of a sequence of cells
– One cell for each Attribute
– Only one value per cell is allowed.

32
Converting Entities to a Relational Schema
During conversion from an ERD to a Relational
Model: LecId
– The name of each entity becomes the name of
the relation LecName
– The name of each attribute becomes an Age
attribute name
in the relation
– Underline the Primary Key value LECTURER

Relational Schema

The Lecturer Entity becomes a Lecturer Relation


The name of the relation is normally written in
uppercase
LECTURER (LecId, LecName, Age)

The names of attributes are normally written in


lowercase or capitalized lower case
33
Nulls
A Null is a special value that may be assigned to
an attribute when a value is unknown or
inapplicable.

A Null is not a 'space'. It is not a zero. It has a


value of NULL. LecID LecName Age
– Q. Is Karen younger than John? 207 John Smith 37
– A. Unknown. We do not know. It is neither True
nor False. 771 Karen Fry

Be careful with results from queries where


NULLS may be found
– Task: List all lecturers older than Lecturer 207
– Would Karen Fry appear in that list?

34
Converting 2 Entities to Relational Schema

35
Converting the M:1 Relationship
The final step in the conversion process is converting any M:1 relationships

Every M:1 relationship generates


a single Foreign Key
M:1 relationship
This means that a relation
will have an additional column*
– a foreign key
SubjectCode LecId
* This would be additional columns if
Title LecName
the key was a composite key
CreditPoints Age

SUBJECT Convened by LECTURER

36
Creating a Foreign Key Column
The foreign key column SubjectCode LecId
is added to the table at Title LecName
the MANY end of the CreditPoints Age

relationship.
SUBJECT Convened by LECTURER

The foreign key column


name is usually spelt the
same as the Primary Key
it refers to.
SUBJECT(SubjectCode, Title, CreditPoints, LecId)
37
Creating a Foreign SubjectCode LecId

Key Column (cont.) Title LecName


CreditPoints Age
• The foreign key column is
added to the table at the SUBJECT Convened by LECTURER
MANY end of the
relationship.
SUBJECT
SubjectCode Title CreditPoints
• The foreign key column LecId
name is usually spelt the INF10005 Intro to Web 12.5 207
same as the Primary Key it INF10017 EBIS 12.5 345
refers to. HPA30088 Honours Project 50 207
MMD20006 Media Thesis A 25 119

38
Fully Converted…
The relational schema for our SubjectCode LecId

model is finally written as… Title LecName


CreditPoints Age

SUBJECT Convened by LECTURER


LECTURER (LecId, LecName, Age)
SUBJECT(SubjectCode, Title, CreditPoints, LecId)
Foreign Key (LecId) References LECTURER

LECTURER SUBJECT
LecID LecName Age SubjectCode Title CreditPoints LecId
207 John Smith 37 INF10005 Intro to Web 12.5 207
119 Jane Pitt 26 INF10017 EBIS 12.5 345
345 Carol Kent 34 HPA30088 Honours Project 50 207
MMD20006 Media Thesis A 25 119
39
ERD to Relational Schema
When this ERD is converted to a relational schema…
– How many columns will be exist in table Worker?
– How many columns will be exist in table Dept?

DeptID WID
Firstname
DeptName
Surname
Address PhoneNo

DEPT employs WORKER

40
Relation vs Relationship
A confusing part of this SubjectCode LecId
process is the terminology Title LecName
having similarly sounding CreditPoints Age

terms…
SUBJECT Convened by LECTURER

This line shows a Relationship


LECTURER (LecId, LecName, Age)
SUBJECT(SubjectCode, Title, CreditPoints, LecId)

Lecturer and Subject are both known as a Relation


41
Terminology
Every system has it's own terminology!

Relational Model terminology MS Access RDBMS terminology MySQL RDBMS terminology


Relation Table Table
Attribute Field Column
Tuple or Row Record Row

42
Relational Schema to RDBMS Tables
We now know how to create a relational schema

LECTURER (LecId, LecName, Age)

SUBJECT(SubjectCode, Title, CreditPoints, LecId)


Foreign Key SubjectCode References LECTURER

Now, let’s find out how to create tables for this schema using
SQL

43
SQL
Overview | Statements | Executing | Create | Insert | Select | Tips & Tricks

44
Example: Executing Movie Table Script
Download a file week_5_SQLScript.TXT from Teams
Open the file using NotePad (or NotePad + + or any other text editor)
Choose SELECT ALL & Copy in Notepad
CREATE TABLE MOVIE (
MOVIENO NUMBER(6) PRIMARY KEY
, TITLE VARCHAR(100)
, RELYEAR NUMBER(4)
, RATING VARCHAR(2)
, RUNTIME NUMBER(4)
);

45
In MySQL Server, Paste
Example (cont.) the statements into the
textbox & click Run

CREATE TABLE MOVIE ( If you try to create a table that already


MOVIENO NUMBER(6) PRIMARY KEY exists, MySQL will show Error.
, TITLE VARCHAR(100)
, RELYEAR NUMBER(4)
, RATING VARCHAR(2) Hence the Drop Table statement at
, RUNTIME NUMBER(4) the top of the script.
);

Ignore this error message.


Generated by the Drop Table statement

46
Why learn SQL?
Why learn SQL DML? Why not just use a GUI Database Administrators / Programmers use
interface just like Access? DML

Programmers write SQL statements in their Business Intelligence workers who know a bit of
code SQL
– A C# or VB or PHP applications request data from a – Don't have to wait for database experts to write
database for student data. simple code
– Programmer embeds SQL statement in code – 3 day wait for SELECT * FROM STUDENT to be coded.
– "SELECT Id, Name FROM Student where Id = 1234"
(or where id = [stuid_textbox] )
Some power end users use SQL statements
when existing systems / reports are lacking
GUI software actually generates SQL statements
– The user usually doesn't see the SQL statement
Used in INS2055 Database Systems (2nd
– Experts sometimes need to modify / tweak the SQL
database unit)

47
Saving Work / Editors
SQL statements typed into ISQL JR are not saved!

Solution
– Copy and Paste the SQL statements into a text file.
– Save the file on USB or Network drive

Notepad++ is far more powerful than Microsoft's


Notepad.EXE
– Notepad + + is free
– Download Notepad++ to your USB / network drive
– It can automagically highlights / colours SQL features
– From the menu choose Language / S / SQL.
– It will highlight: Keywords, Comments, Numbers,
Strings, Functions

Other Editors include Sublime and, Brackets

48
DDL and DML
Structured Query Language (SQL) is the language used by all RDBMSs

SQL statements can generally be placed in one of two groups:

– DDL - Data Definition Language – maintains the database structures / objects


– CREATE TABLE
– ALTER TABLE
– DROP TABLE…

– DML - Data Manipulation Language – works with data within the database
– Inserting Data
– Querying Data (SELECT)
– Updating Data
– Deleting Data…

49
CREATE TABLE
Syntax:

CREATE TABLE <table-name> (


<Column-name1> <data-type> [<max length>] ,
[ , <Column-name2> <data-type> [<max length>] ]…
[ , PRIMARY KEY (<column-name>) ]
[ , FOREIGN KEY (<column-name>) REFERENCES (<table-name>) ] ) ;

Words in uppercase green are MySQL Keywords


Words within < > symbols are user defined - You supply the words
Words within [ ] symbols are optional

50
CREATE TABLE
Create Table LECTURER ( Commas and brackets are important.
LecId number Errors if not used correctly.
, LecName varchar (50)
, Age number Short Text is replaced by varchar()
, Primary Key (LecId) varchar requires a maximum length value
);
Create Table SUBJECT (
SubjectCode varchar (10)
, Title varchar (100)
, CreditPoints number
, LecId number
, Primary Key (SubjectCode)
, Foreign Key (LecId) References
LECTURER ) ;

51
INSERT STATEMENT
Adding data to a table is done via Insert statements

You must specify the table name, column names,


the values to be inserted

Syntax:
INSERT INTO <table-name> (<column-name1> , <column-
name2> , … e.g. INSERT INTO
VALUES ( <value1> , <value2> , … ) ;

Text data must be surrounded by single quotes (not


double quotes in MySQL)

Numeric data does not have quotes

No punctuation is used (no commas, dollar signs…)

52
Smart Quotes – Beware!
Microsoft's Notepad or Don Ho's Notepad ++ are typically used as
SQL text editors (there are many others)

Beware of Word Processors such as MS Word

Really beware of the Smart Quote feature

Smart quotes are fancy looking quote symbols


…VALUES (207, 'John Smith', 37) ; using notepad
…VALUES (207, ‘John Smith’, 37) ; using MS Word

The problem is that RDBMS software such as MySQL does not recognize smart quotes

So MySQL does not view ‘John Smith’ as a text value.


Error at line 1:
ORA-00911: invalid character
53
Smart Quotes – Beware (cont.)
Solutions:
1. Don’t use MS Word
(or Powerpoint or
Onenote…)
2. If you a forced to, then
disable the
appropriate Auto
Correct options within
MS Word

54
INSERT STATEMENT (cont.)
Code required to add 4 rows to the existing tables:

INSERT INTO LECTURER ( LecId, LecName, Age)


VALUES (207, 'John Smith', 37) ;

INSERT INTO LECTURER ( LecId, LecName, Age )


VALUES (345, 'Carol Kent', 34) ;

INSERT INTO SUBJECT ( SubjectCode, Title , CreditPoints, LecId )


VALUES ('HIT1405', 'Intro to Web', 12.5, 207) ;

INSERT INTO SUBJECT ( SubjectCode, Title , CreditPoints, LecId )


VALUES ('HIT1407', 'EBIS', 12.5, 345) ;

55
INSERT STATEMENT (cont.)
If a value is unknown, then use the Null keyword

INSERT INTO LECTURER ( LecId, LecName, Age )


VALUES (974, 'Raj Vasa', Null) ;

56
SELECT
STATEMENT
The remainder of this
lecture examines the
SELECT clause
The Select clause is how
users extract / display
data stored in database
tables

57
SELECT columns
Only columns specified
SELECT lecid, lecname FROM lecturer;
in the Select Clause are
returned
LecID LecName Age LecName
Sequence of columns in 207 John Smith 37 John Smith
119 Jane Pitt 26 Jane Pitt
based on the sequence
345 Carol Kent 34 Carol Kent
of column names
The * symbol can be
used to indicate all SELECT age, lecname FROM lecturer;
columns are returned LecID LecName Age
207 John Smith 37
SELECT * FROM lecturer; 119 Jane Pitt 26
345 Carol Kent 34
58
Order By – Sequence of Result Set Rows
The Order By clause LecName Age
specifies the sequence of SELECT LecName, Age FROM lecturer Carol Kent 34
rows in the result-set. Order By LecName; Jane Pitt 26
John Smith 37
• Use the column name
LecName Age
or column number
John Smith 37
SELECT LecName, Age FROM lecturer
• Multiple columns can Jane Pitt 26
be specified. Carol Kent 34
Order By LecName Desc;
• Each column may be
ordered in ASCending
LecName Age
or DESCending
sequence. Ascending SELECT LecName, Age FROM Jane Pitt 26
is the default lecturer Order By Age ASC; Carol Kent 34
John Smith 37

59
Order By– Column Number
A column number in the Order By clause
– Refers to one of the columns in the select clause
– Can be useful in more complex queries
– This example sequences the result set by the 3rd column listed in the select clause
  
SELECT LecId, LecName, Age LecID LecName Age
FROM lecturer 207 John Smith 37
Order By 3 Desc; 345 Carol Kent 34
119 Jane Pitt 26
So 3 means "Sort the displayed rows by the 3rd column of the result set"

60
Order By – Multi Column
Multiple columns
can be used the
SELECT LecName, Age FROM lecturer Order By Age, LecName
 
Order By clause

Rows in the result


set are in Original LecID LecName Age Result LecName Age
ascending age Table 207 John Smith 37 Set Lisa Simmons 21
sequence 345 Carol Kent 34 Aaron Peters 26
Then within each 119 Jane Pitt 26 Jane Pitt 26
age group, the 231 Jim Brady 26 Jim Brady 26
rows are in
ascending name 118 Sanjay Parekh 34 Bruce Lee 34
sequence 521 Lisa Simmons 21 Carol Kent 34
404 Bruce Lee 34 Sanjay Parekh 34
103 Aaron Peters 26 John Smith 37
61
Coding Rules & Debugging
Your SQL statements will often cause errors. Most errors are caused by
– misspelling column names
– misspelling table names
– misspelling keywords
– excluding commas

Each of these statements will cause an Error


• SELECT LecId LecName Age FROM LECTURER ;
• SELCT LecId, LecName, Age FROM LECTURER ; LecId LecName Age
• SELECT LecId, LecName, Age FORM LECTURER ; 207 John Smith 37
• SELCT LecId, LecName, Age FROM LECTURE ; 345 Carol Kent 34
• SELCT LecId, LectName, Age FROM LECTURER ;

62
Coding Rules & Debugging
Errors are not typically caused by Text Case, Spaces, Multiple Lines and Semicolons

– The Case of SQL keywords is not important.


SELECT LecId, LecName, Age FROM LECTURER ;
select lecid, lecname, age from lecturer ;
is same as

– Number of spaces between key words and commas is not important


SELECT LecId,LecName,Age FROM LECTURER ;
SELECT LecId , LecName , Age FROM LECTURER ; is same as
– An SQL statement may be split over any number of lines

SELECT LecId, LecName, Age FROM LECTURER


;
is same as
SELECT LecId, LecName, Age
FROM LECTURER ;

– Semi-colon to terminate each statement is optional.


SELECT LecId, LecName, Age FROM LECTURER ;
SELECT LecId, LecName, Age FROM LECTURER
is same as
Though Semi Colons are essential when combining multiple statements in a script

63
SELECT Statement Clauses
A SELECT statement may
have additional clauses:
– Clauses must be in the correct
sequence
– Most clauses are optional SELECT empno, branch, salary
– Each clause begins with a KEYWORD Keywords FROM employee
– For easy reading the KEYWORD is WHERE branch = 'KEW'
ORDER BY branch, empno;
written in UPPER CASE
– This statement has 4 clauses
• The Select clause
• The From clause
• The Where clause
• The Order By

64
WHERE Clause – Restricting Rows
• The WHERE clause specifies a • Each row in the table is
condition(s) that each row must
evaluated against the
satisfy to be included in the
result set. condition
- If the condition is true (i.e. the age is <
35) then the row is included in the result
• Syntax and Example
set.
SELECT … FROM …
[ WHERE <search-condition> ]
[ ORDER BY …] • The WHERE clause is often
referred to as a Restriction
SELECT *
FROM lecturer - The clause reduces the number of rows
WHERE age < 35 in the result set.

65
WHERE Clause – Comparison Operators
SQL has many operators that compare two values
= Equal to
<> Not equal to (can use != in most DBMSs) LecID LecName Age
< Less than 521 Lisa Simmons 21
<= Less than or Equal to (two keystrokes < and = in that order)
103 Aaron Peters 26
– > >= Greater than , Greater than or Equal to
119 Jane Pitt 26
231 Jim Brady 26
SELECT * FROM lecturer 404 Bruce Lee 34
WHERE age = 34; - - - - 2 rows selected
118 Sanjay Parekh 34
…WHERE age < 34; - - - - 4 rows selected
…WHERE age <> 21; - - - - 6 rows selected 207 John Smith 37
…WHERE age >= 34; - - - - 3 rows selected
…WHERE age > 34; - - - - 1 rows selected
…WHERE age <= 50; - - - - 7 rows selected
…WHERE age < 20; - - - - 0 rows selected

66
Numeric Literal Values
When specifying numeric literals
– Do not use quotes
– Do not include formatting
– You may use decimal points and leading negative signs

… WHERE annual_budget < 50000 OK


… WHERE annual_budget < 50,000 X
… WHERE annual_budget < '50000' X
… WHERE annual_budget > 9.99 OK
… WHERE annual_budget > -100000 OK

67
String Literal Values
When specifying string / text / character literals
– You must use quotes
– Oracle, SqlServer, MySQL use single quotes
– MSAccess uses double quotes

… WHERE Gender = 'M' OK


… WHERE Gender = "M" X
… WHERE Gender = M X

68
String Values and Case-Sensitivity
The default setting for case-sensitivity of each DBMS may be different

Oracle, MySQL upper-case and lower-case characters are not considered equal
– E.g. 'smith' is not equal to 'SMITH‘

SqlServer, MSAccess upper-case and lower-case characters are considered equal


– E.g. 'smith' is equal to 'SMITH‘

…WHERE LecName = 'JANE PITT' 0 rows returned


LecID LecName Age
…WHERE LecName = 'Jane Pitt' 1 row returned 207 John Smith 37
345 Carol Kent 34
Results using MySQL's default setting 119 Jane Pitt 26

69
Existing Tables & Describe Table
This SQL statement lists the table
name of all tables that you have
created

SELECT table_name
FROM user_tables; OR
SELECT table_name FROM tabs;

This SQL statement lists the


column details of a table that you
have created

DESCRIBE movie;

70
Code Standards and Style
Can you guess which of the statements below is easier to read?

select rdate, temperature, rainfall from weather_readings where


temperature <= -6.5 or rainfall >= 1000 order by rdate desc


SELECT rdate, temperature, rainfall
FROM weather_readings
WHERE temperature <= - 6.5
OR rainfall >= 1000
ORDER BY rdate desc

Good style makes the job of code reading and maintenance easier.
– Real world queries are often 20 or 30 lines long. Good style helps
– Queries are often viewed / modified by other users / programmers
– Organisations often have a SQL style that you must adhere to
– Tutors 'hate' debugging poorly styled queries

71

You might also like