Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Databases.

Similar presentations


Presentation on theme: "Relational Databases."— Presentation transcript:

1 Relational Databases

2 What is a database? An organized collection of records presented in a standardised format searched by computers A collection of related data stored in one or more computerised files in a manner that can be accessed by users or computer programs via a database management system

3 Computer Databases Types of database- Flat file Relational

4 Flat file The simplest type of database where data is stored in rows & columns Each row represents an instance or “record” Each column represents an “attribute” or piece of data about that instance Easy for us to build in Access or Excel

5 Flat file example Attribute or Field Instance or Record

6 Relational Data is divided into tables which are ‘related’ together.

7 Key Terms Entities are real life objects represented in the database. We know them as TABLES Examples: customer, product, invoice. Attributes are individual pieces of data about each entity. We know them as FIELDS Examples: customer name, product ID, invoice number, order date. If there are just the two entities then the other pieces of data are the attributes of this entity. For example: Artist name Artist DOB Artist country of origin Artist current location Track name Track length Track release date Track genre Track rating Track position reached are all the attributes and I’ve added in the entity bit also so that it’s really clear to see what attribute ‘belongs to’ which entity

8 Instances or records Each row entry into this flat file database is called an ‘instance’ as it is an actual instance of the data occurring. This collection of pieces of data belonging to the one instance is more commonly referred to as a ‘RECORD. Attributes or Fields So every time you entered in a row of data into your flat file database you have created a record or instance Instances or Records

9 Uniquely identifying an instance
Usually one primary key attribute is used to uniquely identify the instance (or record) of an entity What are suitable primary key attributes for the following entities? Product Order Customer Sales Person Why are these necessary? Why do we not just use the business or person name?

10 What does this means to the user?
Redundant data Redundant data is any data that has more than one copy of it in the database or in other words it is duplicate data What does this means to the user? Think about: server space/memory the ease of updating this data if it is stored in more then one place

11 Let us consider music data recorded in a flat file database:
Designing the tables in a relational database Entities and their attributes Designing and making a relational database reduces this redundant data. Let us consider music data recorded in a flat file database: Track name Track position reached Track rating Track genre Track release date Track length Track ID Artist name Artist Current Location Artist country of Origin Artist DOB Artist ID These two boxes show my list of attributes for the entities ‘track’ and ‘artist’ You may want to re-arrange your flat file data so that it corresponds to this clear idea

12 Can a track belong to more than one artist?
Designing the tables in a relational database The relationships between entities What is the relationship between the instances of data for each of the two entities (the boxes on the last slide)? In other words what is the relationship between one individual track and one artist? Can a track belong to more than one artist? Can an artist have more than one track? Look at the data for the artist and look at the data for the track – how are these things related to each other? It may help to just look at one record/instance in your flat file database. How a relational database reduces redundant data 2 Often, a relational database has multiple tables in it each one representing an entity This means that the instances (data records) are usually stored once and then referred to

13 identify each instance
Designing the tables in a relational database Defining the relationships The relationship between the instances of data for each of the entities tells us how the entities are linked to each other. For example we can think about vehicle ownership in the UK . Here is a car and its owner What is their relationship? Instance of a ‘Vehicle’ entity Instance of a ‘Person’ entity Some attributes: Some attributes: Reg: CA51 ABC NI No: JG021316A Brand: Audi Ethnicity: Caucasian Model: Q7 Gender: Female Colour: Blue Hair colour: Brown Year: DOB: 06/04/82 There is a ‘Car’ entity and a ‘Person’ entity which have various attributes and a unique ID – the car registration number and the person’s National Insurance number used to uniquely identify each individual person or car (each instance of the ‘Car’ or ‘Person’ entity). Unique way to identify each instance

14 Designing the tables in a relational database Relationship cardinality example
The relationship between the instances of the vehicle entity and the instances of the person entity can be stated as: One vehicle instance can only have one owner (person) One person instance is allowed to own one or more vehicles. This value to the relationship between the individual instances of each entity is called the relationship cardinality and can be shown as: This diagram is from the SSADM notation for database design and is part of an Entity Relationship Diagram (ERD). Yes it does seem odd that the lines indicating a multiple or solo relationship are at the opposite ends of the diagram but this is the correct way of doing this. There are other ways to model relationships between entities including IDEF1X Bachman notation Martin notation The (min, max)-notation of Abrial UML EXPRESS The cardinality of a set is a measure of the "number of elements of the set“ so this means that the cardinality of the instances of one entity in a relationship Person Vehicle Owns This line shows that an instance of the vehicle entity can have a relationship with one instance of the person entity This crows foot notation shows that at an instance of the person entity can have a relationship with one or more instances of the vehicle entity Relationship name goes in a diamond

15 > > PROGRESS CHECK < <
Designing the tables in a relational database Using the relationship to plan the tables > > PROGRESS CHECK < < So we now know the relationship cardinality between the entities ‘Person’ and ‘Vehicle’ and we know each Primary Key and some of their attributes How does this information help us with the tables we need to create? If we were to design the two tables they would contain these attributes: But they are not yet linked together and the relationship has not been created Vehicle Table Registration Number Brand Model Colour Year Person Table National Insurance No Ethnicity Gender Hair colour DOB

16 Designing the tables in a relational database The Primary Key is the key to a good relationship!
To create a relationship between these two tables (and therefore create a relational database) we need to use a primary key (PK) from one table and place it as a foreign key (FK) in the other table. Doing this allows us to then link this one duplicate attribute which is in both tables and create a relationship between the two tables

17 Let’s look at this logically. We have 2 options:
Designing the tables in a relational database Choosing which Primary Key Let’s look at this logically. We have 2 options: to take the PK from the vehicle table and put it as a FK in the person table to take the PK from the person table and put it as a FK in the vehicle table It is good to consider some example data to see this in reality. The woman in the scenario also owns this car, a green Fiat Punto: example data is on the next slide

18 Lots of Data Redundancy! Person Table Vehicle Table
Designing the tables in a relational database Choosing which Primary Key – Option 1 Person Table NI No Ethnicity Gender Hair Colour DOB Vehicle Reg No JG021316A Caucasian Female Brown 06/04/82 CA51 ABC GH65 GYT BV677514I Oriental Male Black 23/02/75 BS45 HJJ FT121285Y African 03/06/90 HT7 UKK OPTION 1 Take the PK from the vehicle table and put it as a FK in the person table example data Lots of Data Redundancy! Every instance entry in the Vehicle table would result in a complete instance record being entered into the Person table Vehicle Table Registration Brand Model Colour Year CA51 ABC Audi Q7 Blue 2006 GH65 GYT Fiat Punto Green 2002 BS45 HJJ Rover 75 Scarlet 1996 HT7 UKK Ford Orion Grey 1986

19 The only duplicate data
Designing the tables in a relational database Choosing which Primary Key – Option 2 Person Table NI No Ethnicity Gender Hair Colour DOB JG021316A Caucasian Female Brown 06/04/82 BV677514I Oriental Male Black 23/02/75 FT121285Y African 03/06/90 OPTION 2 Take the PK from the person table and put it as a FK in the vehicle table example data Vehicle Table Registration Brand Model Colour Year Owner NI No. CA51 ABC Audi Q7 Blue 2006 JG021316A GH65 GYT Fiat Punto Green 2002 BS45 HJJ Rover 75 Scarlet 1996 BV677514I HT7 UKK Ford Orion Grey 1986 FT121285Y Minimal Data Redundancy! The only duplicate data is correctly in the foreign key column

20 Designing the tables in a relational database Choosing the Primary Key and using it
We select Option 2 to reduce duplicate data and therefore save memory in our database So we take the Primary Key from the person table and put it as a Foreign key in the vehicle table and now our table attributes planning is: Vehicle Table Registration Number Brand Model Colour Year Owner NI Number Person Table National Insurance No Ethnicity Gender Hair colour DOB

21 Designing the tables in a relational database 1 to many relationship cardinality and the primary key
In a one to many relationship between entities it is quick to learn which Primary key to use. It is always the Primary Key of the entity that is on the one end being used as a Foreign Key of the entity that is on the many end NOTE: ‘one or more’ and ‘many’ are often interchangeable when used in relationships The way I remember this cardinality and keys rule is to say to myself “one gives to many” meaning that the ‘one’ end entity gives their primary key to the ‘many’ end entity and it is their foreign key Person Vehicle Owns


Download ppt "Relational Databases."

Similar presentations


Ads by Google