Week 5 - ERD & SQL 1
Week 5 - ERD & SQL 1
Week 5 - ERD & SQL 1
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
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
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
6
Key Wars
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)
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
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
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
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
14
ERDs vs Access Relationship Diagrams
An Entity-Relationship Diagram (ERD) use natural keys
– No surrogate key ( non real world keys )
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
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
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
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
At many universities:
• How many lecturers convene a single subject? ONE
• How many subjects can a lecturer convene? MANY
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
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
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
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
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
38
Fully Converted…
The relational schema for our SubjectCode LecId
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
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
42
Relational Schema to RDBMS Tables
We now know how to create a relational schema
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
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
48
DDL and DML
Structured Query Language (SQL) is the language used by all RDBMSs
– DML - Data Manipulation Language – works with data within the database
– Inserting Data
– Querying Data (SELECT)
– Updating Data
– Deleting Data…
49
CREATE TABLE
Syntax:
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
Syntax:
INSERT INTO <table-name> (<column-name1> , <column-
name2> , … e.g. INSERT INTO
VALUES ( <value1> , <value2> , … ) ;
52
Smart Quotes – Beware!
Microsoft's Notepad or Don Ho's Notepad ++ are typically used as
SQL text editors (there are many others)
The problem is that RDBMS software such as MySQL does not recognize smart quotes
54
INSERT STATEMENT (cont.)
Code required to add 4 rows to the existing tables:
55
INSERT STATEMENT (cont.)
If a value is unknown, then use the Null keyword
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
62
Coding Rules & Debugging
Errors are not typically caused by Text Case, Spaces, Multiple Lines and Semicolons
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
67
String Literal Values
When specifying string / text / character literals
– You must use quotes
– Oracle, SqlServer, MySQL use single quotes
– MSAccess uses double quotes
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‘
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;
DESCRIBE movie;
70
Code Standards and Style
Can you guess which of the statements below is easier to read?
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