|
|
|
|
|
1) Tutorial on Data Modelling for Young People :
In this Tutorial, we follow two young visitors to the Blue Mosque in Turkey.
Our visitors are Dimple, a 10-year old girl, who likes sightseeing and icecream
and Toby, Dimple's 12-year-old brother, who likes sightseeing and databases.
[Dimple] : Toby, It's great being in Turkey which has so many things to see and enjoy.
[Toby] : I'm glad you like it, Dimple. What would you like to do today ?
[Dimple] : Toby, we have come to Turkey, and I would like to see Instanbul and visit the Blue Mosque, because it's one of the most popular tourist attractions here,
then I would like to do some shopping, then see the sea, and I would like to finish up at Starbucks.
[Toby] : OK. Let's go ...
|
|
2) THE BLUE MOSQUE
[Toby] Dimple, look at the beautiful Blue Mosque.
|
|
3) A STREET SCENE IN ISTANBUL
[Dimple] Wow, Toby, look at all these people !!!
[Toby] : Yes, and we can start thinking about our Data Model.
[Dimple] But Toby, how can we get started when there are so many people ?
[Toby] Let's see if we can simplify things ...
|
|
4) PEOPLE AND THINGS :
[Toby] : Look Dimple, we have manageed to find one man in a quiet corner of on his own.
Let's start here ..
[Dimple] : Toby, why is he smiling ?
[Toby] : Well Dimple, it's really quite easy. Turkish people are naturally happy and welcoming
He is happy because he is looking forward to meeting peoplle and selling his Giant Cookies, that are called
SIMITs.
They are a local delicacy and very popular with both locals and visitors.
[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".
|
|
5) STARTING OUR DATA MODEL :
[Dimple] : How do we get started ?
[Toby] : Well, we know that we have People and Places.
The simplest start is to call all these places Establishments.
Then we simply have different kinds of Establishments.
And we have people - local people, visitors, 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 !!!
|
|
6) IDENTIFIERS AND PRIMARY KEYS :
[Dimple] : Toby, I am one of these People so how I am I going to identify myself from everybody else.
[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 ...
|
|
7) RELATIONSHIPS AND FOREIGN KEYS :
[Toby] : Dimple, now we can add some interesting details because we know that one person can visit many Establishments.
We also know that one Establishment is visited by many Visitors.
Then we call this a Many-to-Many Relationship between People and Establishments.
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 ?
[Toby] : Yes, Dimple - that's great - and we can also say that One Establishments 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'.
And 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.
|
|
8) PRODUCTS AND PRODUCT TYPES :
[Dimple] : Toby, this looks good - what is it ?
[Toby] : Well Dimple, it's a typical Turkish breakfast.
You can see it's a great combination of cheese, olives, tomatoes and cucumbers, washed down with fruit juice.
[Dimple] : Hmm - I don't know hiw I would create a Data Model for this breakfast.
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, Simit 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".
|
|
9) PRODUCTS AND PRODUCT TYPES :
[Dimple] : Toby, when we go into a shop we want to buy something.
And there are thousands and thousands of possibilities.
How do we deal with all that in our little Data Model ?
[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".
|
|
10) PRODUCTS, TYPES AND PRODUCT HIERARCHIES:
[Dimple] : Toby, when we look closely at the Menu Board to try to decide what to order we can see lots of possibilities
But after a while we can see a pattern that helps us decide.
How do we deal with all that in our little Data Model ?
[Toby] : Well Dimple, it's really quite easy.
We define something called a Hierarchy.
Hierarchies are very common and simply mean any situation where there are Parents, Children, Gand-children and so on.
If we look at the Starbucks Menu Board on the right-hand side we can see a simple example of 'Expresso' and under it a number of different drinks.
My favourite is Caramel Macchiatto.
So in this case, the top-level of our hierarchy is a Product Category called Expresso, and the next level down is a Product called Caramel Macchiatto.
[Dimple] : OK. That sounds OK.
[Toby] : Finally, we show this Herarchy by a dotted line that in the top-right hand corner in the Entity called 'Ref_Types_of_Products'.
This is formally called a 'Reflexive' relationship and is informally called 'Rabbits-Ears'
|
|
11) PEOPLE IN A BAZAAR
[Dimple] Wow, Toby, look at all these people in the Bazaar
And of course, there is the beautiful Blue Mosque.
[Toby] : Yes, we can see a mixture of people selling things, locals and tourists.
|
|
12) TYPES OF PEOPLE :
[Dimple] : Toby, that looks OK.
I guess we can deal with Types of People the same way, can we ?
[Toby] : Yes, Dimple, and Types of Establishments as well.
[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 in case you want to get a refund later.
|
|
13) TYPES OF PEOPLE AND ESTABLISHMENTS:
[Dimple] : Toby, that looks OK.
I guess we can deal with Types of Establishments the same way, can we ?
[Toby] : Yes, Dimple.
[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 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.
[Toby] : Before we move on, let's talk about Establishments.
One special thing about Istanbul is that it has the Blue Mosque which id a woprld-famous very beautiful building.
But when we think about it, we find that we can simply fit it into our definition of Establishments.
|
|
14) TYPES OF PEOPLE AND ESTABLISHMENTS: Hotel in a Cave
[Dimple] : Toby, that looks OK.
I guess we can deal with Types of Establishments the same way, can we ?
[Toby] : Yes, Dimple.
[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 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.
[Toby] : Before we move on, let's talk about Establishments.
One special thing about Istanbul is that it has the Blue Mosque which id a woprld-famous very beautiful building.
But when we think about it, we find that we can simply fit it into our definition of Establishments.
|
|
15) TYPES OF PEOPLE AND ESTABLISHMENTS: Whirling Dervishes
[Dimple] : Toby, that looks OK.
I guess we can deal with Types of Establishments the same way, can we ?
[Toby] : Yes, Dimple.
[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 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.
[Toby] : Before we move on, let's talk about Establishments.
One special thing about Istanbul is that it has the Blue Mosque which id a woprld-famous very beautiful building.
But when we think about it, we find that we can simply fit it into our definition of Establishments.
|
|
16) TYPES OF PEOPLE AND ESTABLISHMENTS: UNESCO World Heritage Site
[Dimple] : Toby, that looks OK.
I guess we can deal with Types of Establishments the same way, can we ?
[Toby] : Yes, Dimple.
[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 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.
[Toby] : Before we move on, let's talk about Establishments.
One special thing about Istanbul is that it has the Blue Mosque which id a woprld-famous very beautiful building.
But when we think about it, we find that we can simply fit it into our definition of Establishments.
|
|
17) CITY TOURISM GUIDE:
[Dimple] : Toby, that looks OK.
I guess we can deal with Types of Establishments the same way, can we ?
[Toby] : Yes, Dimple.
[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 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.
[Toby] : Before we move on, let's talk about Establishments.
One special thing about Istanbul is that it has the Blue Mosque which id a woprld-famous very beautiful building.
But when we think about it, we find that we can simply fit it into our definition of Establishments.
|
|
18) MAKING RESERVATIONS :
[Toby] : During our holiday, Dimple, we have to make reservations for lots of things we want to do. We can find a basic style to reservations and some differeneces that depen in whatever we are trying reserve.
For example, for a Car, we choose a make and model, and for a Hotel, we choose the room.
[Dimple] : OK, that sounds sensible. And how do we use this understanding ?
[Toby] : Well, we start by calling this common style a Design Pattern.
That is what Data Modellers find a common solution that applies to many different situations.
|
|
19) VISITS AND PURCHASES :
[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 ?
[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 in case you want to get a refund later.
|
|
20) PEOPLE AND STAFF:
[Toby] : Dimple, let's take a closer look at the different types of people we can find in Istanbul.
[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.
[Dimple] : OK, Toby. I promise to do that.
[Toby] : We already said that we have Local People and Tourists.
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 more about them than we do about the Visitors.
For example, we will probably know the Gender of everybody just by looking at them.
For Staff, we will usually also know their Data of Birth and their Home Address.
Therefore, although they are all People, we know more about Staff than we do about Visitors.
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.
Does that make sense, Dimple ?
[Dimple] : I think so, Toby.
Is it like saying that we inherit having two arms and two legs from our Parents because they have two arms and two legs, but that we have also have things that are just us ?
[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 icecream ?
|
|
21) STAFF, ESTABLISHMENTS AND DERIVED FIELDS :
[Dimple] : Toby, how do we specify that Staff must work in some Establishment ?
[Toby] : Dimple, that's a very good question.
Fortunately, the answer is very easy.
We add a One-to-Many Relationship between the Staff and Establishment Entities
In English, we would say that every member of Staff must work in one Establishemnt and every Establishment can employ many members of Staff.
In the diagram, we show this with a Foreign Key by the establishment_id field in the Staff Entity.
So if we look closely at the Staff Entity, we will see 'FK' by the establishment_id field.
[Dimple] : OK, that sounds good, and I can see how the Identifiers are very important.
[Toby] : I am glad to hear it, Dimple.
There is one more thing I have to say.
We are learning Data Modelling and one important thing about Data Modelling is that it has to follow a set of Rules.
These Rules help us to produce good Data Models and so they they are very important.
One of the Rules is that we cannot include any bits of data that can be derived from any other bits of data.
For example, we usually want to know how many people work in a Shop or Cafe.
Therefore we include a Staff Count field with the Establishment.
But when to comes to finding the value that goes in here, we will count the records in the Staff Table for each Establishment.
Therefore, it's a derived Field and we call it a name that starts with 'derived_' to make things clear.
Does that sound sensible, Dimple ?
[Dimple] : I suppose so, Toby.
But I've got a headache, can we go for an icecream now ?
|
|
22) REFERENCE DATA :
[Toby] : Dimple, you can see that I am using a Gender Table and People Types Table.
I have given them both names that begin with 'Ref_' to make it clear that they are Reference Data.
This means that the values don't change much and I can use them to define what the valid values can be.
This is a technique that professional Data Modellers but we don't need to worry about it today.
[Dimple] : I'm glad to hear it, Toby !!!
Although it isn't difficult to understand and it seems like a good idea.
[Toby] : In our small example, we have only four kinds altogether -
Gender, Types of Establishment, People and Products.
|
|
23) BRINGING IT ALL TOGETHER :
[Toby] : Dimple, if we bring together everything we have talked about, we will see that we have quite a good Data Model that any professional would be proud of.
[Dimple] : OK, Toby. Do you think I will understand it ?
[Toby] : Let me help you by making a list of the Business Rules for our Model :-
People can be either Staff or Visitors.
People can make Visits to Establishments
Products can be Coffee, Souvenirs and so on.
Staff have a Date of Birth and a Home Address.
People can make Visits to Establishments.
During a Visit they can purchase Products.
[Toby] : OK, Dimple - we have a very nice Data Model and now we can take the break I promised you.
[Dimple] : That's great, Toby - can I have an icecream ?
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 icecream ?
[Toby] : OK, Dimple. You've been a very good girl and you deserve a break.
|
|
24) STARBUCKS in TURKEY
[Toby] : Dimple, I 've got some good news for you.
[Dimple] : I'm glad to hear it, Toby - what is it ?
[Toby] : I have found your favourite coffee shop here in Istanbul, which is Starbucks ;)
[Dimple] : Toby, are you teasing me ?
[Toby] : No, Dimple - look, there it is across the road from the Blue Mosque !!
[Dimple] : Wow - that's great, so I can have my favourite Caramel Macciatto.
|
|
25) PLEASE EMAIL ME
I hope you have found this Tutorial fun and useful.
I would be very pleased if you could
email me with your questions or suggestions.
In this Tutorial we have covered the following Topics :-
- Hierarchies
- Identifiers
- Inheritance
- One-to-Many Relationships
- Many-to-Many Relationships
- Primary Keys and Foreign Keys
- Reference Data
Barry Williams
Principal Consultant
Database Answers Ltd.
London, England
|
|
|
|