Top Banner
Database Design By Joysy John Code First Girls 15 Aug 2013
32

Basics of Database Design

Jan 15, 2017

Download

Technology

Joysy John
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Basics of Database Design

Database Design

By Joysy John

Code First Girls 15 Aug 2013

Page 2: Basics of Database Design

Agenda

• Introduction

• Tables and Keys

• Types of Relationships

• Normalisation

• Q&A

Page 3: Basics of Database Design

Computer Engineer, MBA Founder, Social Entrepreneur Angel Investor, Ex-Banker, Mother

Education

Corporate & Entrepreneurial Career

Introduction

Page 4: Basics of Database Design

Tables and Keys

• Entities: An entity is a distinct clearly identifiable object of the database e.g. Book

• Attribute: Each Entity is characterised by a set of attributes e.g. Title

• Entity set: Set of all entities having attributes of the same type

• Relationships: A relationship is a mapping between entity sets

Book

Acc_No

Title

Author

YearofPub

Users

Card_No

Name

Address

Borrowed_By

Card_No

Acc_No

DOI

*

1

*

1

Page 5: Basics of Database Design

Sample data – library data

Acc_No Name Address Title Author YearofPub CardNo DOI

4 Doe Ray 4 sunshine lane Mindset Carol Dweck 2005 103 01/06/2013

8 Doe Ray 4 sunshine lane Learning Mindset Carol Dweck 2006 103 01/06/2013

2 Jacob John 7 Deer St Mindset Carol Dweck 2005 101 22/05/2013

1 John Smith 34 London Road Happy Days A.J. Roy 2009 100 23/07/2013

5 John Smith 34 London Road Learn to code Tom Close 2013 100 23/07/2013

6 John Smith 34 London Road Ruby on Rails M.H. Bedi 2010 100 23/07/2013

3 Mary jane 2 mill drive The art of writing M.B. Thomas 2001 102 15/08/2013

7 Mary jane 2 mill drive Learn to code Tom Close 2013 102 15/08/2013

9 Mary jane 2 mill drive ABC of photography K. Janice 2012 102 15/08/2013

Page 6: Basics of Database Design

Exercise: Managing a school

Create Students and Classes tables in a database for managing a school. • Each student can register for many classes and each class can

have many students. • Students must register for a class with their firstname, lastname

and age. They can select multiple classes within the same subject.

• Class has a class name and belongs to a subject and is taught in venue assigned to the class.

Page 7: Basics of Database Design

School data tables

Students

Student_ID

FirstName

LastName

Age

Classes

Class_id

className

Subject

StudentsClasses

Student_ID

Class_id

venue

*

1

*

1

Page 8: Basics of Database Design

Basic rules: • One table for one entity set • One column for one attribute

• One table for one relationship set • Columns are:

• Key attributes of all participating entity sets

• All descriptive attributes

Page 9: Basics of Database Design

1:1 Relationship

N:1 Relationship N:M Relationship

1:N Relationship

Types of Relationships

Page 10: Basics of Database Design

Example of 1:1 Relationship

1 1

Person

person_id

first_name

last_name

Spouse_contact

person_id

Spouse_name

Email

Phone

Page 11: Basics of Database Design

Example of 1:N Relationship

* 1

Person

person_id

name

country_id

Country

country_id

country_name

capital

Page 12: Basics of Database Design

Example of N:M Relationship

*

1

Shop

shop_id

shop_name

location

Prices

product_id

shop_id

price

Product

product_id

product_name

category

1

*

A many-to-many relationship is one where two data tables or entities have multiple rows that are connected to one or more rows in the other table.

Page 13: Basics of Database Design

Process of Organising Your Data

What is Normalisation?

Page 14: Basics of Database Design

Why Normalise?

• Reduces redundancies and dependencies

• Saves space

• Reduces data anomalies

• Makes for easier maintenance

Page 15: Basics of Database Design

Normal Form

• Normal Forms (NF) are “standardised” rules

• Each builds off the previous form

• Higher the number the more normalised the data id (1NF, 2NF, 3NF, etc)

Page 16: Basics of Database Design

Example Data Parents student Name emergency contact student age classroom teacher school year grade level

John Smith John Smith Jr John Smith 10 C110 Ms Brown 2010 6

Mary Smith John Smith Jr John Smith 10 C110 Ms Brown 2010 6

John Smith John Smith Jr Mary Smith 10 C80 Mr Green 2010 6

Mary Smith John Smith Jr Mary Smith 10 C80 Mr Green 2010 6

John Smith April Smith John Smith 9 A25 Ms Baker 2009 5

Mary Smith April Smith John Smith 9 A25 Ms Baker 2009 5

John Smith April Smith Mary Smith 9 A99 Mr Close 2009 5

Mary Smith April Smith Mary Smith 9 A99 Mr Close 2009 5

Dave Harris Julie Harris Dave Harris 6 A10 Mr Jones 2010 3

Page 17: Basics of Database Design

Base Table We Will Start With

Page 18: Basics of Database Design

First Normal Form

• Remove “repeating” data

• A primary key can be defined

Primary key can uniquely identify any row in a table

Page 19: Basics of Database Design

Parents student Name emergency contact student age classroom teacher school year grade level

John Smith John Smith Jr John Smith 10 C110 Ms Brown 2010 6

Mary Smith John Smith Jr John Smith 10 C110 Ms Brown 2010 6

John Smith John Smith Jr Mary Smith 10 C80 Mr Green 2010 6

Mary Smith John Smith Jr Mary Smith 10 C80 Mr Green 2010 6

John Smith April Smith John Smith 9 A25 Ms Baker 2009 5

Mary Smith April Smith John Smith 9 A25 Ms Baker 2009 5

John Smith April Smith Mary Smith 9 A99 Mr Close 2009 5

Mary Smith April Smith Mary Smith 9 A99 Mr Close 2009 5

Dave Harris Julie Harris Dave Harris 6 A10 Mr Jones 2010 3

Page 20: Basics of Database Design
Page 21: Basics of Database Design

Second Normal Form

• Meet all requirements of 1NF

• Isolate repeated subsets of data

• Create the relationships

Page 22: Basics of Database Design
Page 23: Basics of Database Design
Page 24: Basics of Database Design
Page 25: Basics of Database Design

Normalised tables

Page 26: Basics of Database Design

* *

*

1

Normalised tables with relationships

1

1

*

1

Page 27: Basics of Database Design

Third Normal Form

• Meet all requirements of 2NF

• Pull out data that is not dependent on primary key

Page 28: Basics of Database Design
Page 29: Basics of Database Design

Exercise: Task Table

Task

task_id

Name

Due_date

description

project

completed

Project

project_id

name

Task

task_id

Name

Due_date

description

Project_id

completed

Page 30: Basics of Database Design

Allow Tasks to be assigned to a user (1:N relationship)

Task

task_id

Name

Due_date

description

Project_id

completed

User_id

User

user_id

name

*

1

Page 31: Basics of Database Design

Allow Tasks to be tagged with tags (N:M relationship)

Task

task_id

name

due_date

description

project_id

completed Tag

tag_id

name

TagTask

id

tag_id

task_id

*

1

* 1

Page 32: Basics of Database Design