Learn Data Modelling by Example: Barry Williams
Learn Data Modelling by Example: Barry Williams
Learn Data Modelling by Example: Barry Williams
Barry Williams
Page 1
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
Page 2
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
1.0 Welcome
This is a Tutorial on Data Modelling for Young People
It provides a user-friendly introduction to Data Modelling which we hope you find interesting and easy to read.
It covers the basic concepts and has a very user-friendly approach featuring a teddy bear and kitten creating a
Data Model on a trip as tourists to Windsor Castle, which is just outside London, England.
You can find this Chapter as a Tutorial on my Database Answers Web Site :-
http://www.databaseanswers.org/tutorial4_data_modelling_dimple_and_toby_visit_windsor_castle/index.htm
You are invited to follow Data Model developments on our Web Site :-
http://www.databaseanswers.org/data_models/index.htm
I hope you enjoy this eBook and would be very pleased to have your comments at [email protected].
Barry Williams
Principal Consultant
Database Answers Ltd.
London, England
1.1 Introduction
In this Tutorial, we will follow two young Tourists as they visit Windsor Castle just outside London in England and
create a Data Model.
Our Tourists are Dimple, a 10-year old girl, who likes sightseeing and ice cream
and Toby, Dimple's 12-year-old brother, who likes sightseeing and designing Data Models.
It provides a vehicle for communication among a wide variety of interested parties, including
management, developers, data analysts, DBAs and so on.
A physical Database can easily be generated from a Data Model using a commercial Data Modelling Tool.
Page 3
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
1.2 Topics
In this Chapter, we will cover some basic Concepts in Data Modelling :-
Reference Data
[Dimple] : Toby, It's great being in London which is so exciting and buzzing.
[Toby] : I'm glad you like it, Dimple. What would you like to do today ?
[Dimple] : Toby, we have seen Buckingham Palace, where the Queen of England lives, and now
I'd like to visit Windsor Castle, because it's one of the most popular tourist attractions in
England, and it's just a short trip from London.
Page 4
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
We are starting from Buckingham Palace, where the Queen of England lives
Page 5
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] Yes, Dimple, and when we look around there are so many Banks, Cafes, Pubs, Restaurants, Shops, Wine
Bars and Hospitals !!!
The other thing that we see when we look around is people - lots of people.
And we have People - local people, Tourists, students, people passing through, people working here, people here
on business and so on.
[Dimple] : Hmmm - so how do we translate what we know to help us get started with our Data Model?
Page 6
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] : We will give everybody a Unique Identifier and every Establishment its own unique Identifier.
When we use these we call them Primary Keys, and show them in the diagram with a PK on the left-hand side.
[Dimple] : That sounds good, Toby, but I don't know what it means.
[Toby] : Well, Dimple, let's look at how we use these Identifiers ...
To make it easier for you to understand I have expanded the Many-to-Many Relationship into two different
things, which are called One-to-Many Relationships.
[Dimple] : So Toby, is that like saying that One Person can make Many Visits to many Establishments ?
Page 7
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] : Yes, Dimple - that's great - and we can also say that One Establishment can have Visits from many People.
At this point, we can show how all these boxes are related, and that is a very big step, because it takes us to the
idea of 'Relationships'.
We can call these boxes Tables - or Entities if we want to speak to professional Data Modellers.
A Table simply stores data about one particular kind of Thing of Interest.
Each record in a Table will be identified by its own unique identifier, which we call the Primary Key.
It is not usually easy to find a specific item of data already in the Table that will always be unique.
For example, in the States, Social Security Numbers are supposed to be unique, but (for various legitimate reasons)
that is not always the case.
Therefore, it is Best Practice to create a new field just for this purpose.
This will be what is called an Auto-Increment data type which will be generated automatically by the DBMS at
run-time.
This is called a Surrogate Key and it does not have any other purpose.
It is a meaningless integer that is generated automatically by the Database Management Software, such as Oracle
or SQL Server, The values are usually consecutive integers, starting with 1,2,3,4 and so on.
Now we can see how useful our Identifiers can be because we can include the Person and Establishment Identifiers
in our Visits table.
Then the Person_ID field becomes a link to a record for a Person in the Person Table.
This link is what is called a Foreign Key and we can see it's shown with 'FK' on the left-hand side.
Page 8
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] : Well Dimple, it's really quite easy. It's like all our Modelling where we look for
simple patterns that cover many situations.
[Dimple] : Hmm - I don't know what that means. Maybe if you showed me I might
understand it.
[Toby] : OK.
Everything that we buy is called a Product, and all we have to do is simply define the type of
each Product - such as a Coffee, Muffin or a Newspaper.
Then we draw a little box called Products and say that every Product has a Type.
In other words, there is a Relationship between the Products and Product_Types boxes.
The lines are called Relationships and they are very important in Data Modelling.
We are now creating an Entity-Relationship Diagram or "ERD".
The symbol at the Products end is called crows-feet and it shows the Many end.
The short straight line at the Product_Types end shows the One end.
Dimple, let me explain about the dotted line. It means that the Relationship results in a
Foreign Key in the Products table. This is shown by the FK symbol next to the
product_type_code field and it means that there is a link back to the Product_Types.
Page 9
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
However, the Primary Key is only the product_id, and of course, this is shown by the PK
symbol next to the product_id field.
Later, when we talk about Inheritance, we will use a straight line, in contrast to this dotted
line here. This is to show that the Foreign Key field is also a Primary Key.
I have to say something a bit difficult about Primary Keys right now.
In the Products Table, we have to allow for a very large number of Products being stored.
This number has no meaning and is simply used to identify each record uniquely among
possibly millions or hundreds of millions.
These are what we call enumerated data and are typically Reference Data.
They are always relatively small in number and we choose a Code for the Primary Key
because we can create them and review them manually.
It also helps us to create a Code that we can use and refer to, in contrast to the ID fields
that have no meaning.
Sizes Small, Medium and Large where we are accustomed to seeing S,M and L.
Page 10
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
We know that they are organised in groups, like Food and Drink, and each of these has
more groups and so on, right down to the particular Product, like Caramel Macciato or a
Panini.
This top-down organisations is called a Hierarchy and appears all over the place.
Luckily we can show this very easily and neatly in our Data Model.
Page 11
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] : Finally, we show this Hierarchy by a dotted line in the top-right hand corner in the
Entity called 'Ref_Types_of_Products'.
Page 12
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Dimple] : OK, that sounds sensible. And do they use these Identifiers in a Database ?
[Toby] : Yes, and what is even better is that the Database will automatically generate a new
unique Identifier for you and your Visits and Purchases if you want to get a refund later.
[Dimple] : OK, that sounds sensible. And do they use these Identifiers in a Database ?
[Toby] : Yes, and we can use our new unique Identifier for you and your Visits and
Purchases in case we want to keep track of things.
Like maybe you want to get a refund later so we need to get your details from the
Database.
Page 13
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
Page 14
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Dimple] : Toby, with so many People, Establishments and Purchases how do they keep
track of everything ?
[Toby] : Well, Dimple, by this time, everything has its own Identifier that is used wherever
they need to keep track.
[Dimple] : OK, that sounds sensible. And do they use these Identifiers in a Database ?
Page 15
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] : Yes, Dimple, and in this diagram, we can see that we can use the unique Identifiers
which are shown as PK, for Primary Keys.
We can see that we have a PK for every Entity or Table so we can be pretty sure we can get
from any Table to any other Table.
This is called navigating around the Data Model and is a good test for a well-designed Data
Model.
Page 16
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Dimple] : OK,Toby. I hope I don't have to think too much because I might get a headache?
[Toby] : No, Dimple, I will do the thinking and talking and all you have to do is nod your
head when you understand.
There are always lots and lots of People visiting Windsor Castle.
When we look at this picture, we can see Ceremonial Guards in ceremonial red uniforms,
and a big crowd, with mainly tourists but also staff in shops responsible for controlling the
crowd, Tourists, local people and so on.
Some of these Local People are shoppers and some of them will be working in the shops.
We will call the workers 'Staff' and we know different things about them than the things we
know about the Tourists.
For example, we will probably know the Gender of everybody just by looking at them.
For Staff, we will usually also know their Date of Birth and their Home Address.
Page 17
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
In Data Modelling we have a very powerful approach that we call Inheritance that we can
use here.
If we want to describe this in English, we would say that Staff inherit the People_Type_Code
and Gender from the parent Entity of People, and in addition, they have a Date of Birth and
Home Address.
For Tourists, we dont know much, except for the Date of their Visit, and maybe, if they buy
something in a shop using a Credit Card, then the shop would know the Credit Card details.
For the Ceremonial Guards in red uniforms, we can tell their Rank by looking at their
Uniform and maybe it would also tell us which Unit of the Army they belong to.
[Toby] : Yes, Dimple - that's great - let's take a break and do some shopping !!!
[Dimple] : I like the sound of that, Toby. Can I have an ice cream ?
[Toby] : Yes, of course, Dimple this diagram shows we are doing well.
It show Inheritance between People and the three different types of People :-
Page 18
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
We can see a field marked as PF in the three tables for Ceremonial Guards, Staff and
Tourists.
This is unusual because it means a field which is a Primary Key in the three Tables and also a Foreign Key
to the People Table.
Therefore, if your first record was a Ceremonial Guard, then we would have a record in the
People table with a person_id of 1 and a record in the Ceremonial Guard with a guard_id of
1.
Similarly, if our second record was a member of Staff, we would have a record in the People table with
a person_id of 2 and a record in the Staff table with a staff_id of 2.
[Dimple] : OK, that sounds good, and I can see how the Identifiers are very important.
I have shown it here simply as an example because it is a situation that occurs quite often
so its good to recognise it when you see it.
Page 19
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Dimple] : I suppose so, Toby. But I've got a headache, can we go for an ice cream now ?
[Toby] : In our small example, we have only four kinds of Reference Data altogether -
Gender, Types of Establishment, People and Products.
Page 20
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] : Let me help you by making a list of the Business Rules for our Model :-
[Toby] : OK, Dimple - we have a very nice Data Model and now we can take the break I
promised you.
[Toby] : Sure, but before we do I should say something about PF, which appears in the
Staff Table.
It's unusual and it's called PF because it means a field which is a Primary Key in the Staff
Table and a Foreign Key to the People Table.
[Dimple] : Hmmm, I've got a headache, Toby - can we please go an get an ice cream ?
[Toby] : OK, Dimple. You've been a very good girl and you deserve a break.
You can admire what we have created, which is this very professional-looking Data Model.
Page 21
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
This is suitable for explaining what we saw in Windsor to our family or friends.
There are lots of People in Windsor, including Ceremonial Guards, Staff and Tourists
There are also lots of Establishments, like Shops and the Castle !!!
Page 22
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
This level of display is suitable if we want to confirm to each other how the Tables (or
Entities) are related.
Page 23
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
You can see that the amount of detail involved makes it more difficult to understand whats
going on and to identify what is important.
This level of display is suitable if we want to talk about details and develop a Database from
our Data Model.
Page 24
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
[Toby] : I have found your favourite Baskin-Robbins ice cream here in Windsor ;)
[Toby] : No, Dimple - look, there it is across the road from Windsor Castle !!
[Dimple] : Wow - that's great, so I can have my favourite Butter Pecan ice-cream.
Page 25
Learning Data Modelling by Example Chapter 1) Modelling Windsor Castle
Barry Williams
Principal Consultant
Database Answers Ltd.
London, England
Page 26