Vous êtes sur la page 1sur 8

Outline of Lecture

Introduction to Databases
Structure and Organization
Tables, Fields, Records

Encoding Relationships

COMPSCI 111 / 111G

Microsoft Access

An Introduction to Practical Computing

Creating Tables in Access


Adding Relationships in Access
Referential Integrity in Access

Databases Organization and Creation


30/04/2014

COMPSCI 111/111G - Databases 01

Databases and Digital Computers

The Notion of a Database

The advent of digital computers has enabled


electronic databases that can:

What is a database?
A (typically large) collection of data about a particular topic

Store large amounts of information

Data organised in a systematic fashion

Copy that information with little effort

Examples of databases:

Retrieve desired information rapidly

Catalogue of books in a library


Patients files in a doctor's office

Modern society relies heavily on the electronic databases


that digital computers make possible.

A catalog of movies in a video store


Records of a stores stock and transactions
Contents of an address book
Lists of student marks in a class
http://en.wikipedia.org/wiki/Database
30/04/2014

COMPSCI 111/111G - Databases 01

30/04/2014

COMPSCI 111/111G - Databases 01

Database Management Systems

Aspects of Databases

A database management system (DBMS) is a software


package designed to store and manage databases.

Before we can use any database, physical or digital, we


must first:

Some well-known examples include:


Microsoft Access

Determine how to organize the database


Enter content into the database
Retrieve that content selectively when needed
Present the retrieved content in a useful form

MySQL
Oracle

We will examine each of these subtasks during our two


lectures on this topic.

Database management systems play a central role in the


vast majority of modern businesses.

30/04/2014

COMPSCI 111/111G - Databases 01

Database Organization

30/04/2014

COMPSCI 111/111G - Databases 01

Relational Databases

A database organizes information in some systematic

The relational model of databases

way. A common database organization incorporates:

Was introduced in the late 1970s


Provides a conceptual view of a database

Entities
Things about which we store information

Stores tables with rows and columns

Will have attributes, characteristics that describe the entity

Uses multiple tables to express relations

E.g., students, classes, lecturers

Is the most widely used approach today

Relationships
Specific connections among entities
E.g., Paul Smith is enrolled in CompSci111
Usually expressed simply (e.g., customers who made orders)
Often one to many (e.g., one customer can make many orders)
Edgar Codd
30/04/2014

COMPSCI 111/111G - Databases 01

Some Definitions

Relationships Example

Field (a column in a table)


Information of the same type (e.g., a set of names)
Record (a row in a table)
A collection of field values (e.g., name, address, email)
Table
A collection of records (e.g., a Students table)
Relational database
A collection of tables

30/04/2014

COMPSCI 111/111G - Databases 01

30/04/2014

Keys in Relational Databases

COMPSCI 111/111G - Databases 01

10

Primary Keys Example

Multiple tables can express relations among entities.


The notion of keys is central to encoding such relations.
The most important is the primary key, which:
Is a field present in all tables.
Must have a value; cannot be null.
Ensures that each record is unique; i.e. Cannot have two
records in a table with the same primary key.

Primary key

30/04/2014

COMPSCI 111/111G - Databases 01

11

30/04/2014

COMPSCI 111/111G - Databases 01

12

Relations and Foreign Keys

Examples of Foreign Keys

Also central to relations is the notion of a foreign key, which:


Is a field in one table that is related to the primary key in another
table.
Lets us join information in the tables by creating a connection
through this primary key foreign key link.
Can take on a null value when information is unavailable.
Examples include:
Course in the Academic Transcript table
Student in the Academic Transcript table.

30/04/2014

COMPSCI 111/111G - Databases 01

13

30/04/2014

Exercises

COMPSCI 111/111G - Databases 01

14

Exercises

Exercise 1: What is the difference between a database and a


database management system?

Exercise 4: What would be the best field(s) to use as the primary


key for this table? Why?

Exercise 2: What is a primary key?

Exercise 3: What is a foreign key?

30/04/2014

COMPSCI 111/111G - Databases 01

15

30/04/2014

COMPSCI 111/111G - Databases 01

16

Referential Integrity

Relationships and Integrity

Another important idea that underlies relational databases is


referential integrity.
This states that all values in the foreign key field of a table
must either:
Be present in the table to which it is related, or
Have a null value.

Example:
Suppose Student in the Academic Transcript table is a linked by
foreign key to ID in the Student table, a primary key.

Each student in the Academic


Transcript table must correspond to some ID entry in the
Students table.

Each student in the Academic Transcript table must corres- pond


to some ID entry in the Students table.
http://en.wikipedia.org/wiki/Referential_Integrity
30/04/2014

COMPSCI 111/111G - Databases 01

17

30/04/2014

Types of Relationships

COMPSCI 111/111G - Databases 01

18

Types of Relationships

Most databases support three distinct types of relations:


One to many
An entity in table A is related to many entities in table B.
E.g., each customer may purchase many items.

One to one
An in table A is related to a unique entity in table B.
E.g., each person may have a unique spouse.

Many to many
Different entities in table A and table B can occur together.
Implemented indirectly through two one-to-many relations.

One-to-many relations are the most widely used variety.

30/04/2014

COMPSCI 111/111G - Databases 01

19

30/04/2014

COMPSCI 111/111G - Databases 01

20

Creating a Database in Access

Microsoft Access
One widely used database management system is Microsoft
Access.
Using this software, we can:
Create database tables and relations

Create blank
database or
use templates

Store content in the resulting database


Query the database for specific information
Generate reports from these data

Access is targeted at small businesses, with other DBMSs aimed


at medium to large companies.

30/04/2014

COMPSCI 111/111G - Databases 01

File save
location

21

30/04/2014

Creating a Table in Access

COMPSCI 111/111G - Databases 01

22

Creating a Table in Access


Access lets you create tables in different ways or "views".

The Design View lets you set or view the fields your table is characterised
by.
The Datasheet View is used to view or enter data.

Choose
Table,
Query, or
Report

Select the view


30/04/2014

Change to Design View


COMPSCI 111/111G - Databases 01

23

30/04/2014

COMPSCI 111/111G - Databases 01

24

Creating a Table in Access

Specifying a Primary Key


When creating a table, we must also specify its primary key.

When creating a table in Design View, we specify the set of


fields that make up that table.

We can achieve this in Access by:

For each field, we must provide:

Selecting the field(s) that will form the key

Field name

Right clicking and choosing "Primary Key

Field type
Description
(optional)

When entering values later, a field that serves as a primary key


cannot have either null or repeated values.

25
30/04/2014

COMPSCI 111/111G - Databases 01

30/04/2014

We can specify a relationship between tables in Access by:

We have already discussed the idea of referential integrity:

Entering the Database Tools menu

All values in a foreign key field must either appear in the


linked table or have null values.

Selecting Relationships
Adding the two tables to the relationship diagram (if not present)

Access enforces such referential integrity; for instance,

Selecting a primary key field from one table

Suppose we try to add a student to the Academic Transcript


table but his or her ID does not exist in the Students table.

Dragging the cursor to the related field in the other table


Checking the results when the Edit Relationships menu appears

Access would reject this data entry because it would violate


referential integrity.

Result of dragging the ID


field in the Customers
table to the CustomerID
field in the Orders table

COMPSCI 111/111G - Databases 01

26

Referential Integrity in Access

Adding Relationships

30/04/2014

COMPSCI 111/111G - Databases 01

This means we must enter values in a certain order (e.g.,


add a student to Students first and then add an entry for that
student in Academic Transcript.

27

30/04/2014

COMPSCI 111/111G - Databases 01

28

Some Rules to Remember

More Exercises

Related fields in different tables must have the same type.


E.g., ID in Students table, Student in Academic Transcript table)

You should not delete a record from a table if its primary


key is linked to records in a related table.
E.g., we do not want to delete a record from the Students table if that
student occurs in the Academic Transcript table.

Exercise 5: What is the primary key and the foreign key (if
one exists) for the Customers table?

This would expunge all records related to that student in the Academic
Transcript table if you check Cascade Delete Related Records.
Generally not a good idea, e.g. if the deletion is done by mistake.

You should not change the values of a tables primary key


for similar reasons.

30/04/2014

COMPSCI 111/111G - Databases 01

29

Exercise 6: What is the primary key and the foreign key (if
one exists) for the Room Booking table?
Exercise 7: What is the primary key and the foreign key (if
one exists) of the Room Information table?
30/04/2014

COMPSCI 111/111G - Databases 01

30

Vous aimerez peut-être aussi