databases3b

17
Review of database basics

Upload: cwest

Post on 16-Apr-2017

609 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: databases3b

Review of

database basics

Page 2: databases3b

A2 Applied ICT

FieldsAttributes ‘atomic’

Data typesPrimary key

Validation rulesInput masks

L: letter A-Z (required)?: letter A-Z (optional)0: number 0-9 (required)9: number (optional)

range (or limit) check, e.g. between 10 and 20, >10list check, e.g. ‘a’ OR ‘b’ OR ‘c’(better as a dropdown list)

Design a database to store personal information of 6th Form students

Page 3: databases3b

A2 Applied ICT

tables queries(select some data only)

macros

forms(on screen)

reports(on paper)

what the user can see

add a new student

see all studentssee all in Year

13

list of all students

list of all in Year 13

?

Design a database to store personal information of 6th Form students

Page 4: databases3b

A2 Applied ICT

orderNum stockNum quantitySold price

1122 12 2 £2.00

1122 13 3 £4.00

1123 12 2 £2.00

1123 14 1 £3.00

1124 13 1 £4.00

1125 12 2 £2.00

1125 13 3 £4.00

Primary key?Starter

Page 5: databases3b

A2 Applied ICT Database exercise

Page 6: databases3b

A2 Applied ICT

Project number Project name

Employee number Employee nameRate category

Hourly rate

1. consider single table

Consider the Employees and Projects table on the webpage

2. assign primary key(s)?

Project number Project name

Employee number Employee nameRate category

Hourly rate

5. spot another (less obvious)

Employee number Employee nameRate category

Hourly rate

Project numberEmployee number

6. …and remove it

Employee number Employee nameRate category

Project numberEmployee number

Project number Project name

Rate categoryHourly rate

3. spot repeated groups of data…

Project number Project name

Employee number Employee nameRate category

Hourly rate

4. …and remove them

Employee number Employee nameRate category

Hourly rate

Project numberEmployee number

Project number Project name

Page 7: databases3b

A2 Applied ICT

Entity-relationsh

ip diagrams

Page 8: databases3b

A2 Applied ICT

By the end of these exercises you should be able to:

• explain the terms ‘entity’, ‘attribute’ and ‘relationship’

• identify whether the relationship between two ‘entities’ is• one-to-one• one-to-many• many-many

• draw an ‘entity-relationship diagram’ to represent the database for a given scenario

Page 9: databases3b

form student

ONE form(s) contains ONE student(s) MANY MANY

event world record

ONE event(s) has ONE world record(s)MANY MANY

employee project

ONE employee(s) can work on ONE project(s) MANY MANY

( ) ( )

( ) ( )

( ) ( )

Page 10: databases3b

event world record

ONE event(s) has ONE world record(s)MANY MANY( ) ( )

event

world record

might as well merge into the one table(nothing to be gained from having two)

a ONE-to-ONE relationship

Page 11: databases3b

form student

ONE form(s) contains ONE student(s) MANY MANY( ) ( )

This is what you are afterIt makes your database more efficient

a ONE-to-MANY relationship

form student

Page 12: databases3b

employee project

ONE employee(s) can work on ONE project(s) MANY MANY( ) ( )

Impossible to do in your database!Needs to be resolved…

a MANY-to-MANY relationship

employee project

employee projectemployee_project

Use a ‘link’ table between them

Page 13: databases3b

husband wife

mother child

ward patient

employee company car

person main residence

publisher book

author book

film actor

film director

student A-level course

customer product

stock item supplier

Extensions:• How will you resolve the many-to-many relationships?

• Can any of these ER diagrams be merged?

• Come up with a primary key field for each entity.• Come up with all other attributes (inc. FK’s)

Page 14: databases3b

Hospital ERD exercise

A hospital is organised into wards. Each ward has a ward number and a name recorded, along with the number of beds in that ward.

Each ward is staffed by nurses. They have their staff number and name recorded and are assigned to a single ward.

Each patient in the hospital has a patient identification number and their name, address and date of birth are recorded. Each patient is under the care of a single consultant and is assigned to a single ward.

Each consultant is responsible for a number of patients. Consultants have their staff number, name and specialism recorded.

• What are the entities?

• Come up with ERD’s in pairs

• Combine into one ERD

• Come up with primary key attributes for each entity

• Come up with other attributes (including foreign keys)Extension:Consider students, teachers and courses in the same way

Page 15: databases3b

nursepatient wardconsultant

staffIDTitleForenameSurname

staffIDTitleForenameSurnamewardID (FK)

patientIDTitleForenameSurnameAdmissionDatestaffID (FK)wardID (FK)

wardIDwardNameBuildingFloorTel

Page 16: databases3b

Police Database exercise

A system for tracking the officers on different cases is needed within a police unit.

Each officer belongs to a single department. Officers are allocated to cases. Each officer can be allocated to more than one case at any given time and each case could be worked on by a number of officers.

Each case has a single supervising officer (or ‘supervisor’) in charge of it who may supervise more than one case at a time.

The following information needs to be stored:case ID number; type of case; description of the case; the date a case is opened (and closed); the result of the case; officers’ name, rank, department and gender; supervisors’ name and rank

Extension: where would the date an officer was assigned to a case go?

• What are the entities?

• Come up with ERD’s in pairs

• Combine into one ERD

• Come up with primary key attributes for each entity

• Resolve any many-to- many relationships

• Come up with other attributes (including foreign keys)Extension:Consider a library in the same way.

Page 17: databases3b

supervisor

Cases

Officers Department

Officers

Cases

Clue for previous situation