databases

26
Basic Database Concepts By Odaly Fernandez

Upload: odalyfer

Post on 22-Jan-2017

490 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Databases

Basic Database Concepts

By Odaly Fernandez

Page 2: Databases

What is a database

A database is any collection of information (data).

This collection represents part of the real world.

The data is organized in some manner so that the information contained within the database can be easily retrieved.

Page 3: Databases

Examples of database

Phone books Library School Search engines

Page 4: Databases

Relational Database Modelling Uses a collection of tables to represent both

data and the relationship among those data Each table has multiple columns Each column has a unique name

Page 5: Databases

Relational database – Basic conceptsEMPLOYEE table (collection of related records)EmployeeID Name Hire Date1122 Bill White 1/1/20012387 Ron Smith 27/9/20014456 Greg King 24/8/1999

CUSTOMER tableCustomerID Name Address Phone100 Lyons 123 A Street 916-444-55101 Dennys 435 Elm Street 916-478-23102 IHOP 654 17th Av. 916-458-77

ORDERS tableOrder Number CustomerID EmployeeID Cost101 100 1122 23.78 € 145 100 2387 100.25 € 152 102 2387 54.89 €

Record (collection of related fields)

Field (a single piece of information)

Page 6: Databases

Database Management System (DBMS) Software that allows users to create,

maintain, and query your data in the related tables

Examples: MS Access, Oracle, FoxPro, MySql

Page 7: Databases

Primary and foreign keys

Primary Key, a value that is unique to each record

Foreign Key, a primary key of one table included in another table. Link tables to each other.

Find the PK and FK on the last relational database example.

Page 8: Databases

Referential Integrity

Referential integrity is a database concept that ensures that relationships between tables remain consistent.

What happens if: Marketing’s DID is changed

to 16 in DEPARTMENT? The entry for Accounts is

deleted from DEPARTMENT?

DID DName

13 Marketing

14 Accounts

15 Personnel

EID EName DID

1515 John Smith 13

1600 Mary Brown 14

1717 Mark Jones 13

1800 Jane Smith 13

DEPARTMENT

EMPLOYEE

Page 9: Databases

Types of relationshipsOne to oneAn entity in A is associated with at most one

entity in B, and an entity in B is associated with at most one entity in A

a1

a2

a3

a4

b1

b2

b3

b4

A B

Page 10: Databases

One to one examples

Husband and wife Person and passport Teachers and computers

Page 11: Databases

Types of relationshipsOne to manyAn entity in A is associated with any number

(zero or more) of entities in B. An entity in B, however, can be associated with at most one entity in A

a1

a2

a3

a4

b1

b2

b3

b4

b5

A B

Page 12: Databases

One to many examples

Mother - children One department contains many employees,

but one employee works in one department only.

Customers – orders Car - owner

Page 13: Databases

Types of relationshipsMany to manyAn entity in A is associated with any number

(zero or more) of entities in B, and an entity in B is associated with any number of entities in A

a1

a2

a3

a4

b1

b2

b3

b4

b5

A B

Page 14: Databases

Many to many examples Each student takes several subjects, and each subject is

taken by several students. Authors – Books A project can have many employees working on it. And, an

employee can work on many projects. In a library, a book can be borrowed by many borrowers. A

borrower can borrow many books.

Page 15: Databases

Types of relationships

In a database we store information about books, the authors, the category of the books, and the publishers.

A book can have more than one author. A book can be of more than one category. A publisher publishes many books. A book is

published only for one publisher. What types of relationships do we have here?

Page 16: Databases

Designing a database structure As you are gathering information in the

requirements phase, you should be thinking about these items:

Identify all of the fields you are going to need and ways to organize the related fields into tables

Determine or build a primary key for each table if needed

Page 17: Databases

Designing a database structure Compare the two tables below. You will see situations that you

want to avoid:1)     Avoid data redundancy.  2)     If you can avoid data redundancy, you can avoid data

mistakes.  Look at the customer name for customer #635 in both tables.  Two different spellings. Which one is correct? 

3)     Notice that there are two different customers in the Orders table that have the same customer number (#104).  If your billing system uses the customer number to drive it, who’s going to get the bill?

4)     Notice that some of the customers in the Orders table aren’t even listed in the Customer table.  Will these customers ever get a bill?

Page 18: Databases

CUSTOMER tableCustID Name Address Phone City State104 Meadows Restaurant Pond Hill Rd 313-792 Monroe Mi128 Grand River Restaurant 37 Queue Av. 313-729 Lacota MI163 Bentham’s Restaurant 1366 36th St 517-792 Monroe MI635 Oaks Restaurant 3300 West Russell St 419-336 Maumee OH741 Prime Cut Steakhouse 2819 East 10th St 219-336 Mishawalka IN779 Gateway Lounge 3408 Gateway B1 419-361 Sylvania OH

ORDERS tableOrdID CustID Name Billing Date Invoice

Amount202 104 Meadows Restaurant 15/1/12 1280.50226 635 Oakes Restaurant 15/1/12 1939.00231 779 Gateway Lounge 15/1/12 1392.50309 741 Prime Cut Steakhouse 15/2/12 1928.00313 104 Stokes Inn 15/2/12 1545.00377 128 Grand River Restaurant 15/3/12 562.00359 635 Raks Restaurant 15/3/12 1939.00373 779 Gateway Lounge 15/3/12 1178.00

Page 19: Databases

Designing a database structure

The South India Motor Company is designing a new database to store information on its employees, their job details and information about the branches in which they work. For each employee, it needs to hold information on the employee's name, their payroll number (which is unique to them and has two letters followed by four digits, the first letter is always M or C), the branch that they work at and the job that they perform. For each employee, the job code and description needs to be stored along with the rate of pay for that job. For each branch of the company, it needs to record the branch number, the branch name, the address and a weighting which is used to calculate salaries. This weighting ensures that employees living in areas where housing and other costs are high receive more pay than workers in areas where housing costs are lower. The weighting is a decimal value between 0 and 2.

Design the structure of the database you would build to hold this data.

Page 20: Databases

Designing a database structure Identify tables, fields, and relationships from

the problem description. How do I find tables and fields? Tables and fields are often represented by

nouns in the description

Page 21: Databases

Designing a database structure How do I tell the difference between a

table and a field? Field values are atomic, so fields can't have

smaller parts or be groups of things. Fields can't participate in relationships. Fields have a single value (a string, a

number, etc), whereas tables have complex values (groups of fields generally).

Page 22: Databases

Designing a database structure How do I find potential relationships

between tables? Relationships are often verbs or phrases that

link two tables in the problem description.

Page 23: Databases

Designing a database structure How do I determine the type of relationship? Sometimes this is apparent from the description. Sometimes it is less clear, and you need to think

about what situations are possible.

Page 24: Databases

Designing a database structureSo which are our tables?

What data type should you use for each field?

Which fields should be primary key?What relationships will be needed?

Page 25: Databases

Validation rules

Range check Look-up check Format check Length check: passwords Type check

Page 26: Databases

Data verification

There are two main methods of verification: Double entry - entering the data twice and

comparing the two copies. Proofreading data - this method involves

someone checking the data entered against the original document.