Basic Database Concepts
By Odaly Fernandez
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.
Examples of database
 Phone books
 Library
 School
 Search engines
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
Relational database – Basic concepts
EMPLOYEE table (collection of related records)
EmployeeID Name Hire Date
1122 Bill White 1/1/2001
2387 Ron Smith 27/9/2001
4456 Greg King 24/8/1999
CUSTOMER table
CustomerID Name Address Phone
100 Lyons 123 A Street 916-444-55
101 Dennys 435 Elm Street 916-478-23
102 IHOP 654 17th
Av. 916-458-77
ORDERS table
Order Number CustomerID EmployeeID Cost
101 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)
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
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.
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
Types of relationships
One to one
An 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
One to one examples
 Husband and wife
 Person and passport
 Teachers and computers
Types of relationships
One to many
An 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
One to many examples
 Mother - children
 One department contains many employees,
but one employee works in one department
only.
 Customers – orders
 Car - owner
Types of relationships
Many to many
An 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
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.
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?
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
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?
CUSTOMER table
CustID Name Address Phone City State
104 Meadows Restaurant Pond Hill Rd 313-792 Monroe Mi
128 Grand River Restaurant 37 Queue Av. 313-729 Lacota MI
163 Bentham’s Restaurant 1366 36th
St 517-792 Monroe MI
635 Oaks Restaurant 3300 West Russell St 419-336 Maumee OH
741 Prime Cut Steakhouse 2819 East 10th
St 219-336 Mishawalka IN
779 Gateway Lounge 3408 Gateway B1 419-361 Sylvania OH
ORDERS table
OrdID CustID Name Billing Date Invoice Amount
202 104 Meadows Restaurant 15/1/12 1280.50
226 635 Oakes Restaurant 15/1/12 1939.00
231 779 Gateway Lounge 15/1/12 1392.50
309 741 Prime Cut Steakhouse 15/2/12 1928.00
313 104 Stokes Inn 15/2/12 1545.00
377 128 Grand River Restaurant 15/3/12 562.00
359 635 Raks Restaurant 15/3/12 1939.00
373 779 Gateway Lounge 15/3/12 1178.00
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.
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
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).
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.
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.
Designing a database structure
So which are our tables?
What data type should you use for each field?
Which fields should be primary key?
What relationships will be needed?
Validation rules
 Range check
 Look-up check
 Format check
 Length check: passwords
 Type check
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.

Databases

  • 1.
  • 2.
    What is adatabase  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.
  • 3.
    Examples of database Phone books  Library  School  Search engines
  • 4.
    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
  • 5.
    Relational database –Basic concepts EMPLOYEE table (collection of related records) EmployeeID Name Hire Date 1122 Bill White 1/1/2001 2387 Ron Smith 27/9/2001 4456 Greg King 24/8/1999 CUSTOMER table CustomerID Name Address Phone 100 Lyons 123 A Street 916-444-55 101 Dennys 435 Elm Street 916-478-23 102 IHOP 654 17th Av. 916-458-77 ORDERS table Order Number CustomerID EmployeeID Cost 101 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)
  • 6.
    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
  • 7.
    Primary and foreignkeys  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.
  • 8.
    Referential Integrity  Referentialintegrity 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
  • 9.
    Types of relationships Oneto one An 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
  • 10.
    One to oneexamples  Husband and wife  Person and passport  Teachers and computers
  • 11.
    Types of relationships Oneto many An 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
  • 12.
    One to manyexamples  Mother - children  One department contains many employees, but one employee works in one department only.  Customers – orders  Car - owner
  • 13.
    Types of relationships Manyto many An 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
  • 14.
    Many to manyexamples  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.
  • 15.
    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?
  • 16.
    Designing a databasestructure  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
  • 17.
    Designing a databasestructure  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?
  • 18.
    CUSTOMER table CustID NameAddress Phone City State 104 Meadows Restaurant Pond Hill Rd 313-792 Monroe Mi 128 Grand River Restaurant 37 Queue Av. 313-729 Lacota MI 163 Bentham’s Restaurant 1366 36th St 517-792 Monroe MI 635 Oaks Restaurant 3300 West Russell St 419-336 Maumee OH 741 Prime Cut Steakhouse 2819 East 10th St 219-336 Mishawalka IN 779 Gateway Lounge 3408 Gateway B1 419-361 Sylvania OH ORDERS table OrdID CustID Name Billing Date Invoice Amount 202 104 Meadows Restaurant 15/1/12 1280.50 226 635 Oakes Restaurant 15/1/12 1939.00 231 779 Gateway Lounge 15/1/12 1392.50 309 741 Prime Cut Steakhouse 15/2/12 1928.00 313 104 Stokes Inn 15/2/12 1545.00 377 128 Grand River Restaurant 15/3/12 562.00 359 635 Raks Restaurant 15/3/12 1939.00 373 779 Gateway Lounge 15/3/12 1178.00
  • 19.
    Designing a databasestructure 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.
  • 20.
    Designing a databasestructure  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
  • 21.
    Designing a databasestructure  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).
  • 22.
    Designing a databasestructure  How do I find potential relationships between tables?  Relationships are often verbs or phrases that link two tables in the problem description.
  • 23.
    Designing a databasestructure  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.
  • 24.
    Designing a databasestructure So which are our tables? What data type should you use for each field? Which fields should be primary key? What relationships will be needed?
  • 25.
    Validation rules  Rangecheck  Look-up check  Format check  Length check: passwords  Type check
  • 26.
    Data verification  Thereare 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.