0% found this document useful (0 votes)
179 views70 pages

Topic 5 - ERD & SQL 1

1. The document discusses the differences between surrogate keys and natural keys in database design. Surrogate keys are numeric and have no business meaning, while natural keys are attributes that have inherent business meaning. 2. When modeling business requirements, natural keys should be used as they are understandable to clients and reflect real-world terminology. Entities in an entity relationship diagram use natural keys. 3. After modeling is complete, database implementers can choose to add surrogate keys to tables while retaining the natural key attributes. This allows flexibility without data loss.

Uploaded by

Vũ Quếanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views70 pages

Topic 5 - ERD & SQL 1

1. The document discusses the differences between surrogate keys and natural keys in database design. Surrogate keys are numeric and have no business meaning, while natural keys are attributes that have inherent business meaning. 2. When modeling business requirements, natural keys should be used as they are understandable to clients and reflect real-world terminology. Entities in an entity relationship diagram use natural keys. 3. After modeling is complete, database implementers can choose to add surrogate keys to tables while retaining the natural key attributes. This allows flexibility without data loss.

Uploaded by

Vũ Quếanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Topic 05

Surrogate Keys vs Natural Keys


Entity Relationship Modelling and Diagrams
SQL

1
Key values/meanings
Consider Unit codes and descriptions for university units.
UnitCode Description
INS2055 Databases
INS3082 Database Systems
INS2061 Data mining and business analytics

Imagine that you see this data in the Enrolment table (similar to Casting table in Movie Database)
UnitCode StudentID
INS2055 111222333
INS2061 111222333
INS2061 555666777

You can easily see that student 111222333 is enrolled in INS2055


You can easily see that INS2061 has 2 enrolments

2
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.

3
Surrogate Keys
However, if a Unit table uses a surrogate key (no business meaning), we may have this:
Unit ID UnitCode Description
1 INS2055 Databases
2 INS3082 Database Systems
3 INS2061 Data mining and business analytics

This is the data in the Enrolment table


UnitID StudentID
1 111222333
3 111222333
3 555666777

It is more difficult to see that student 111222333 is enrolled in INS2055


It is more difficult to see that INS2061 has 2 enrolments
4
Natural Keys
If our database used the university UnitCode as its primary key,
then it would be considered to be a Natural Key
UnitCode Description
INS2055 Database
INS3082 Database Systems
INS2061 Data mining and business analytics

Some database developers suggest that Natural Keys are good/better


and should be used in a RDBMS

There are many who disagree…

5
Key Wars
Arguments about Surrogate Keys:

Advantages
• The key has no business meaning
• The key should never have to change (i.e. if the business rules change)

Disadvantages
• You will not 'know' if invalid values (non-existent unit codes) have been entered.
• Tutor 1 worked 8 hours this week on running tutorials in unit 74 (what is unit 74??)

http://stackoverflow.com/questions/63090/surrogate-vs-natural-business-keys
http://www.agiledata.org/essays/keys.html

6
Key Wars
Arguments about Natural Keys:
Advantages
• They have business meaning
• Non IT people understand what the keys mean
• The key provides information to the user without having to perform lookups
• Fred Blogs worked 8 hours this week on running tutorials in unit INS2055
Disadvantages
• Natural Keys may change
• The university renames all INS units to ICT units (INS2055 becomes ICT2055)
• They value of a natural key may be long and cumbersome
• A key value containing text may not be processed as quickly as a numeric value

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.
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
E.g. 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
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 & its 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

9
Composite Keys
Natural Keys are sometimes Composite Keys.
A composite key is a key made up of multiple values.

Consider a room in this university: BA302

BA refers to the building that the room is in (Business Arts)


302 refers to a particular room in that building

So you may store this data:


BuildingCode RoomNo Capacity HasWindows
BA 302 161 No
BA 404 20 Yes
The Primary Key for this room would be BuildingCode + RoomNo.

This is a single Primary Key. It is made up of multiple fields/columns.


10
Composite Keys
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
Adv: The key is self explanatory / documenting. The key has meaning.
DisAdv: The key is long and cumbersome.
What happens if they rename BA to BS (Business School) or BL (Business Law)?
11
Database Creation overview
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

12
Entity Relationship Model & Diagram
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.

13
ERDs vs Access Relationship Diagrams
An Entity-Relationship Diagram (ERD) use natural keys
• No surrogate key ( only real-world keys )

An Access Relationship Diagram matches the existing database tables


• If surrogate keys are used in a table,
then these appear in the Access Relationship Diagram

An Access Relationship Diagram is not an ERD

➢ ERD exists before a database is built


➢ Access relationship diagram occurs after the database is built

14
E-R Diagrams
An Entity-Relationship Diagram (ERD)
• is a way to express the structure of information used by an organization or
business in the form of a diagram
• used to assist in database design
MovieNo RatingCode
Title Description
YearReleased

MOVIE has RATING


LecOffice

391 Avatar 2009 PG Parental Guidance


231 The Matrix 1999 M Mature Audience

15
E-R Diagrams - Entities
The E-R Diagram has three major components:

Entities
This diagram has two entities: Subject and Lecturer

SUBJECT LECTURER
LecOffice

16
E-R Diagrams - Attributes
The E-R Diagram has three major components:

Entities: Subject, Lecturer


Attributes: This diagram has six attributes:
SubjectCode, Title, CreditPoints,
LecId, LecName, Age

SubjectCode LecId
Title LecName
CreditPoints Age

SUBJECT LECTURER
LecOffice

17
E-R Diagrams - Relationships
The E-R Diagram has three major components:
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


LecOffice

18
E-R Diagrams & Sample Data
When building an ER Diagram, always consider examples of data that
would be stored in each entity.
After all, that's why we build a database – to store data
SubjectCode LecId
Title LecName
CreditPoints Age

SUBJECT Convened by LECTURER


LecOffice

Subject Data
INS2055 Database 4
INS1053 Intro to Business Data Analytics 3
Lecturer Data
207 John Smith 37
119 Jane Pitt 26
19
E-R Diagrams & 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
• A 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
20
E-R Diagrams & Business Rules
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. Modern Database Management.

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.

21
E-R Diagrams & Business Rules
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)


22 This semester, you will be given all of the business rules
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 our school:
• 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 has

MANY is drawn as a crow's foot has


23
Determining Cardinality - part 1
Let's consider the "convened by" relationship SUBJECT Convened by LECTURER
LecOffice

Let's consider the LEFT end of the relationship


Let's consider just ONE instance of a subject (e.g. INS2055)

How many lecturers convene this one subject? ONE


– Draw the cardinality symbol at the RIGHT end of the relationship

SUBJECT Convened by LECTURER


LecOffice

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"
24
Determining Cardinality - part 2
Let's again consider the "convened by" relationship SUBJECT Convened by LECTURER
LecOffice

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 this one lecturer? MANY


Draw the cardinality symbol at the LEFT end of the relationship

SUBJECT Convened by LECTURER


LecOffice

Now read this diagram right to left


Start the sentence by using one instance of the lecturer entity
"One Lecturer convenes MANY Subjects"
25
Summary - Cardinality Constraints
What does this diagram say? SUBJECT Convened by LECTURER
LecOffice

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
26
Entities 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


SubjectCode LecId
LecId 207 Title LecName
LecName John Smith CreditPoints
Age 37 Age

SUBJECT Convened by LECTURER


LecOffice
E.g. This is another instance of the Lecturer Entity

LecId 119
LecName Jane Pitt
Age 26
27
E-R Diagrams – Identifiers
An identifier is an attribute(s) that uniquely identifies an entity instance
➢ Same concept as a Primary Key
Rules
• Every entity must have an identifier
• Every instance of an entity must have a unique identifier
• No duplicates
• The value of an identifier cannot be empty / null
SubjectCode LecId
Title
Entity identifiers are LecName
CreditPoints Age
underlined on the ERD

SUBJECT Convened by LECTURER


LecOffice

28
ERD to Relation Model Conversion
What next?
Soon, we will begin to translate the ERD into a Relational Schema
SubjectCode LecId

E.g.
Title This: LecName
CreditPoints Age

UBJECT Convened by LECTURER


LecOffice

will be translated to: LECTURER (LectId, LecName, Age)


Primary Key (LecId)

or simply: LECTURER (LectId, LecName, Age)


29
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.
30
Conversion to Relational Schema
During conversion from an ERD to a Relational Model:
➢ The name of each entity becomes the name
SubjectCode
of the relation LecId

➢ The name of each attribute becomes an attribute name


Title LecName
CreditPoints Age
in the relation
➢ Underline the Primary Key value
SUBJECT Convened by LECTURER
LecOffice
LECTURER (LecId, LecName, Age)
– The above is called a Relational S chema
– The Lecturer Entity becomes a Lecturer Relation
• The name of the relation is normally written in uppercase.
• The names of attributes are normally written in lowercase or
capitalized lower case.

31
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


207 John Smith 37
771 Karen Fry
Q. Is Karen younger than John?
A. Unknown. We do not know. It is neither True nor False.

Be careful with results from queries where NULLS may be found.

Task: Select all lecturers older than Lecturer 207


Would Karen Fry appear in that list?
32
Converting two entities to Relational Schema
The relational schema for this ERD model would be written as:
SubjectCode
SubjectCode LecId
LecId
LECTURER (LecId, LecName, Age) TitleTitle LecName
LecName
Primary Key (LecId) CreditPoints
CreditPoints Age Age

SUBJECT(SubjectCode, Title, CreditPoints) SUBJECT


SUBJECT Convened by by
Convened LECTURER
LECTURER
LecOffice
LecOffice
Primary Key (SubjectCode)
or
LECTURER (LecId, LecName, Age)
SUBJECT(SubjectCode, Title, CreditPoints)

Notice that there is nothing that associates a subject to a lecturer.


The two entities are currently unrelated.

33
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


SubjectCode LecId
will have an additional column(s)*
Title
– a foreign key
LecName
CreditPoints Age

* - This would be additional columns if


the key was a composite key. SUBJECT Convened by LECTURER
LecOffice
34
Creating a Foreign Key column
➢ The foreign key column is added to the table at the MANY end of the
relationship.
SubjectCode LecId
➢ The foreign key column name is
usually spelt the same as Title LecName
the Primary Key it refers to. CreditPoints Age

SUBJECT Convened by LECTURER


LecOffice

SUBJECT(SubjectCode, Title, CreditPoints, LecId)

Note: An attribute named LecId is NOT added to the Subject entity.


35
A single attribute name must only appear once in an entire ERD
The new column is a Foreign Key
The foreign key column is added to SubjectCode LecId
the table at the MANY end of the
Title
relationship. LecName
CreditPoints Age

The foreign key column name is Convened by


SUBJECT LECTURER
usually spelt the same as the LecOffice

Primary Key it refers to.

SUBJECT
SubjectCode Title CreditPoints
LecId
INS1004 Basic Informatics 3 207
INS2055 Databases 4 345
INS1053 Intro to BDA 2 207
INS4006 Thesis A 10 119
36
Converting two entities in the RM
The relational schema for our model is finally written as this:

LECTURER (LecId, LecName, Age)


SUBJECT(SubjectCode, Title, CreditPoints, LecId)
Foreign Key (LecId) References LECTURER LECTURER
LecID LecName Age
SubjectCode LecId 207 John Smith 37
Title LecName 119 Jane Pitt 26
CreditPoints Age
345 Carol Kent 34

SUBJECT Convened by LECTURER


LecOffice SUBJECT
SubjectCode Title CreditPoints LecId
INS1004 Basic Informatics 3 207

INS2055 Databases 4 345

INS1053 Intro to BDA 2 207

INS4006 Thesis A 10 119


37
ERD to Relational Schema
After this ERD is converted to a relational schema…
• How many columns will exist in table Worker?
• How many columns will exist in table Dept?

DeptID WID
Firstname
DeptName
Surname
Address PhoneNo

DEPT employs WORKER


LecOffice

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

SUBJECT Convened by LECTURER


LecOffice

This line shows a Relationship

LECTURER (LecId, LecName, Age)

SUBJECT(SubjectCode, Title, CreditPoints, LecId)

39 Lecturer and Subject are both known as a Relation


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

40
Relational Schema to RDBMS table
Previously we created a Relational schema

LECTURER (LecId, LecName, Age)

SUBJECT(SubjectCode, Title, CreditPoints, LecId)


Foreign Key (Lecid) References LECTURER

We will now create tables for this schema using SQL

41
Using MySQL Server

42
SQL: Executing the Movie Table script
Lab 1
Download a file CreateMovie01.TXT
Open the file using NotePad (or NotePad++ or any other text editor)
Choose SELECT ALL & Copy in Notepad
CREATE TABLE MOVIE (
MOVIENO NUMBER(6, 0) PRIMARY KEY
, TITLE VARCHAR(100)
, RELYEAR NUMBER(4) Varchar() is used for text data
, RUNTIME NUMBER(4) varchar requires a maximum length value
, RATINGCODE VARCHAR(2)
, COLOUR_CODE VARCHAR(1) Number() is used for numeric data
, TMDB_SCORE NUMBER(3,1) Number has an optional max length and decimal places
, TMDB_VOTES NUMBER
, TMDB_ID VARCHAR(12)
);
INSERT INTO MOVIE (MOVIENO, TITLE, RELYEAR, RUNTIME, RATINGCODE, COLOUR_CODE, TMDB_SCORE,
TMDB_VOTES, TMDB_ID) VALUES(620, 'Ghostbusters', 1984, 107, 'PG', 'C',
6.8, 570, 'tt0087332' );
INSERT INTO MOVIE (MOVIENO, TITLE, RELYEAR, RUNTIME, RATINGCODE, COLOUR_CODE, TMDB_SCORE,
TMDB_VOTES, TMDB_ID) VALUES (324668,'Jason Bourne', 2016, 123, 'M', 'C',
7.2, 121, 'tt4196776' );
43
SQL: Why learn SQL?
➢ Why learn SQL DML? Why not just use a GUI interface just like Access?

➢ Programmers write SQL statements in their code


– A C# or VB or PHP applications request data from a database for student data.
– Programmer embeds SQL statement in code
– "SELECT Id, Name FROM Student where Id = 1234" (or where id = [stuid_textbox] )

➢ GUI software actually generates SQL statements


– The user usually doesn't see the SQL statement
– Experts sometimes need to modify / tweak the SQL

➢ Database Administrators / Programmers use DML DML = data manipulation language

➢ Business Intelligence workers who know a bit of SQL


– Don't have to wait for database experts to write simple code
– 3 days wait for SELECT * FROM STUDENT to be coded.

➢ Some power end users use SQL statements when existing systems / reports are lacking

➢ Used in INS3082 Database Systems (2nd database unit)


44
Saving your work & Notepad++
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 Other editors:
Download Notepad++ to your USB / network drive • Sublime
It can automagically highlights / colours SQL features
From the menu choose Language / S / SQL. It will highlight:
• Brackets
• Keywords • many more
• Comments
• Numbers
• Strings
• Functions

45
SQL: DDL & 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…

46
SQL DDL: 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.

47
SQL DDL: CREATE TABLE
CREATE TABLE Lecturer ( Commas and brackets are important.
LecId number Errors if not used correctly.
, LecName varchar (50)
, Age number
, PRIMARY KEY (LecId) Short Text is replaced by varchar()
); 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
);

48
SQL DML: 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> , …
VALUES ( <value1> , <value2> , … ) ;

E.g. INSERT INTO Lecturer ( LecId, LecName, Age )


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

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…)

49
BEWARE of Smart Quotes
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:
50 ORA-00911: invalid character
BEWARE of Smart Quotes
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

51
SQL DML: INSERT STATEMENT
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 ('INT1053', 'Intro to BDA’, 3, 207) ;

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


VALUES (‘INS2055', ' Databases’, 4, 345) ;

52
SQL DML: INSERT STATEMENT
Nulls

If a value is unknown, then use the Null keyword

INSERT INTO Lecturer ( LecId, LecName, Age )


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

53
SQL DML: SELECT STATEMENT
The remainder of this lecture examines the SELECT clause
The Select clause is how users extract / display data stored in
database tables
Each column name in the
SELECT clause,
➢ Syntax: causes a column to be
SELECT <item> included in the result set.
FROM <table name>
The sequence of column
… names in the select clause
matches the sequence of
Example: columns in the result set
SELECT LecId, LecName, Age
FROM Lecturer ;
LecId LecName Age
Output / Result Set: 207 John Smith 37
345 Carol Kent 34
54
SQL: SELECT columns
Only columns specified in the Select Clause are returned
Sequence of columns is based on the sequence of column names
The * symbol can be used to indicate all columns are required
SELECT lecid, lecname FROM lecturer; Age LecName
37 John Smith
26 Jane Pitt
LecID LecName
34 Carol Kent
207 John Smith
119 Jane Pitt
345 Carol Kent SELECT age, lecname FROM lecturer;

LecID LecName Age


207 John Smith 37
SELECT * FROM lecturer; 119 Jane Pitt 26
55 345 Carol Kent 34
SQL: DELETE command
➢ When deleting rows in a table, it is a good idea to create a Select
statement first.
➢ Suppose you want to delete all movies that have a run_time < = 100
➢ Select the rows first SELECT *
FROM Movie
WHERE Run_time < =100 ;
➢ Check the result set. Are these the rows you want to delete?
➢ Once satisfied, simply change the first few words of the statement:
➢ Change from SELECT *
FROM Movie
to
DELETE
From Movie
56 WHERE Run_time <= 100 ;
SQL: Update command
➢ Updates one or more rows in a single table.

➢ UPDATE <table-name>
➢ SET <column-name> = <value> [ , … ]
➢ WHERE <condition>
• UPDATE student
• SET Fees_Paid = 'Y'
• WHERE StuID = 1122334
•Warning. If you do not include the where clause, ALL rows from the table
•will be updated!
•Many DBMSs do not have an Undo or Oops feature.
• Suggestion: Before updating, write a Select statement. Check the rows selected.
•Then convert the Select statement to an Update statement

57
SQL: Update command
➢ This example updates multiple columns in the same statement.

➢ UPDATE <table-name>
➢ SET <column-name> = <value> [ , … ]
➢ WHERE <condition>

➢ UPDATE employee
➢ SET salary = salary + 5000,
➢ WHERE UPPER(branch) = 'HAWTHORN'
➢ OR years_of_service >= 10;

58
SQL: Order By– sequence of result set rows
The Order By clause specifies the sequence of rows in the result-set.
• Use the column name or column number
• Multiple columns can be specified.
• Each column may be ordered in ASCending or DESCending sequence.
Ascending is the default
LecName Age
SELECT LecName, Age FROM lecturer Carol Kent 34
ORDER BY LecName; Jane Pitt 26
John Smith 37

LecName Age
John Smith 37
SELECT LecName, Age FROM lecturer
Jane Pitt 26
Carol Kent 34
ORDER BY LecName DESC;
LecName Age
Jane Pitt 26
SELECT LecName, Age FROM lecturer Carol Kent 34

59
ORDER BY Age ASC; John Smith 37
SQL: Order By – column number
A column number in the Order By clause
refers to one of the columns in the select clause.
This can be useful in more complex queries

This example sequences the result set by the 3rd column listed in the
select clause

  
LecID LecName Age
SELECT LecId, LecName, Age
207 John Smith 37
FROM lecturer
345 Carol Kent 34
ORDER BY 3 DESC;
119 Jane Pitt 26

So 3 means "Sort the displayed rows by the 3rd column of the result set"

60
SQL: Order By– multi column
Multiple columns can be used the Order By clause
SELECT LecName, Age FROM Lecturer ORDER BY Age, LecName
Rows in the result set are in ascending age sequence 
Then within each age group, the rows are in ascending name
sequence
Original LecID LecName Age Result LecName Age
Table 207 John Smith 37 Set Lisa Simmons 21
345 Carol Kent 34 Aaron Peters 26
119 Jane Pitt 26 Jane Pitt 26
231 Jim Brady 26 Jim Brady 26
118 Sanjay Parekh 34 Bruce Lee 34
521 Lisa Simmons 21 Carol Kent 34
404 Bruce Lee 34 Sanjay Parekh 34
103 Aaron Peters 26 John Smith 37
61
SQL: Coding Rules & Debugging
Your SQL statements will often cause errors. Most errors are caused by
• misspelling column names
LecId LecName Age
• misspelling table names
207 John Smith 37
• misspelling keywords 345 Carol Kent 34
• excluding commas

Each of these statements will cause an MySQL Error


• SELECT LecId LecName Age FROM LECTURER ;
• SELCT LecId, LecName, Age FROM LECTURER ;
• SELECT LecId, LecName, Age FORM LECTURER ;
• SELCT LecId, LecName, Age FROM LECTURE ;
• SELCT LecId, LectName, Age FROM LECTURER ;
62
SQL: 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 ; is same as
select lecid, lecname, age from lecturer ;
– Number of spaces between key words and commas is not important
SELECT LecId,LecName,Age FROM Lecturer ; is same as
SELECT LecId , LecName , Age FROM LECTURER ;
– 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 ; is same as
SELECT LecId, LecName, Age FROM Lecturer
Though Semi-Colons are essential when combining multiple statements in a script

63
SQL: DML – SELECT statement clauses
A SELECT statement may have additional clauses
• Clauses must be in the correct sequence
• Most clauses are optional
• Each clause begins with a KEYWORD
• For easy reading the KEYWORD is written in UPPER CASE
SELECT empno, branch, salary
FROM employee
Keywords WHERE branch = 'KEW'
ORDER BY branch, empno;
• This statement has 4 clauses
– The SELECT clause
– The FROM clause
– The WHERE clause
64 – The ORDER BY clause
SQL: WHERE clause – restricting rows
Syntax: SELECT … FROM …
[ WHERE <search-condition> ]
[ ORDER BY …]
The WHERE clause specifies a condition(s) that each row must satisfy to be
included in the result set.
SELECT *
FROM Lecturer
WHERE Age < 35
Each row in the table is evaluated against the condition
If the condition is true (i.e. the age is < 35) then the row is included in the result
set.
The WHERE clause is often referred to as a Restriction
The clause reduces the number of rows in the result set.

65
SQL: WHERE clause – comparison operators
SQL has many operators that compare two values
= Equal to
<> Not equal to (can use != in most DBMSs)
< Less than
<= Less than or Equal to (two keystrokes < and = in that order)
> Greater than
>= Greater than or Equal to

SELECT * FROM lecturer


WHERE Age = 34; - - - - 2 rows selected LecID LecName Age

…WHERE Age < 34; - - - - 4 rows selected 521 Lisa Simmons 21


103 Aaron Peters 26
…WHERE Age <> 21; - - - - 6 rows selected
119 Jane Pitt 26
…WHERE Age >= 34; - - - - 3 rows selected 231 Jim Brady 26
…WHERE Age > 34; - - - - 1 rows selected 404 Bruce Lee 34
…WHERE Age <= 50; - - - - 7 rows selected 118 Sanjay Parekh 34
…WHERE Age < 20; - - - - 0 rows selected 207 John Smith 37
66
SQL: 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
SQL: 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
SQL: 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

…WHERE LecName = 'Jane Pitt' 1 row returned

LecID LecName Age


207 John Smith 37
345 Carol Kent 34
Results using MySQL 's default setting
119 Jane Pitt 26
69
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
70

You might also like