Databases CS111 Part 1 Part 2 Know what a database is. Learning - - PowerPoint PPT Presentation

databases
SMART_READER_LITE
LIVE PREVIEW

Databases CS111 Part 1 Part 2 Know what a database is. Learning - - PowerPoint PPT Presentation

Databases CS111 Part 1 Part 2 Know what a database is. Learning how to use Microsoft Access , a database management system Understand why they are useful and when you might want to use one. - Create a database Have a basic understanding of


slide-1
SLIDE 1

Databases

CS111

slide-2
SLIDE 2

Part 1

Know what a database is. Understand why they are useful and when you might want to use one. Have a basic understanding of how the most common type of databases, “Relational Databases” are organized. Learning how to use Microsoft Access, a database management system

  • Create a database
  • Add information to the database
  • Retrieve information from the database

Part 2 Part 3

Lab exercises

slide-3
SLIDE 3

What is a Database?

slide-4
SLIDE 4

What is a database?

A database is a collection of data that is organized in a systematic way. The data stored in a database is generally about a single topic. For example: Patients’ files in a hospital The contents of an address book A catalog of movies in a video store

slide-5
SLIDE 5

Computers make large databases possible

Digital computers have made electronic databases possible, which facilitate the storage of very large quantities of information the efficient addition, modification or removal of that information rapid search and retrieval of desired information Modern society relies heavily on the electronic databases that digital computers make possible.

slide-6
SLIDE 6

Database: What

Database

is collection of related data and its metadata organized in a structured format for

  • ptimized information management

Database Management System (DBMS)

is a software that enables easy creation, access, and modification of databases for efficient and effective database management

Database System

is an integrated system of hardware, software, people, procedures, and datathat define and regulate the collection, storage, management, and use of data within a database environment

6

slide-7
SLIDE 7

Database Management System

7

Database Systems: Design, Implementation, & Management: Rob & Coronel

  • manages interaction between end users and database
slide-8
SLIDE 8

Database System Environment

8

Database Systems: Design, Implementation, & Management: Rob & Coronel

§ Hardware § Software

  • OS
  • DBMS
  • Applications

§ People § Procedures § Data

slide-9
SLIDE 9

Question: What websites have you visited that likely use a database?

slide-10
SLIDE 10

These websites use databases

A library catalogue Online stores or auction websites Database management systems play a central role in the vast majority of modern businesses.

slide-11
SLIDE 11

What is NOT a database?

The following are collections of data, and they can be “organized” but they are not considered databases. a file-system (e.g. the “C: drive” on your computer) a word-processing document a text-file that is written and edited by hand (e.g. using notepad, emacs) The difference lies in how databases are organized.

slide-12
SLIDE 12

Imagine you want to keep track of films that you have watched. A non-database solution is to use a plain text file (or a word processing document) But this could lead to many problems -- especially as the size of your “database” grew…

A bad “database”

slide-13
SLIDE 13

In this format, the data is not easily searched. e.g. What if you want to know which movies are directed by Ben Affleck (and not those in which he is the star)? It would be very difficult to transform this data into another format, e.g. to display it on a website. In this format, it is not easy to edit the data in the database, e.g. changing the rating from an “out of 5 stars” system to an “out of 10 stars” would involve a lot

  • f manual labour.

In this format, there is nothing that prevents errors from creeping in, e.g. the same film being added twice?

A bad “database”

slide-14
SLIDE 14

Database Management Systems (DBMS) such as Microsoft Access, MySQL, and Oracle can help you avoid these problems (but they don’t 100% prevent them). ...they also don’t work on their own. To properly take advantage of DBMS requires knowledge of database design... … and thus, knowledge of database design is central to a whole career path. Search google for “database analyst jobs” !

Databases to the rescue!

slide-15
SLIDE 15

Evolution of Data Models

Timeline

1960s 1970s 1980s 1990s 2000+

File-based Hierarchical Network

Relational

Object-

  • riented

Web-based

Entity-Relationship

slide-16
SLIDE 16

Database: Historical Roots

Manual File System

  • to keep track of data
  • used tagged file folders in a filing cabinet
  • organized according to expected use

e.g. file per customer

  • easy to create, but hard to
  • locate data
  • aggregate/summarize data
slide-17
SLIDE 17

Database: Historical Roots

Computerized File System

  • to accommodate the data growth and information need
  • manual file system structures were duplicated in the computer
  • Data Processing (DP) specialists wrote customized programs to:
  • write, delete, update data (i.e. management)
  • extract and present data in various formats (i.e. report)
slide-18
SLIDE 18

File System: Example

Database Systems: Design, Implementation, & Management: Rob & Coronel

slide-19
SLIDE 19

File System: Weakness

Weakness

“Islands of data” in scattered file systems.

Problems

Duplication - same data may be stored in multiple files Inconsistency -same data may be stored by different names in different format Rigidity -requires customized programming to implement any changes

cannot do ad-hoc queries

S511 Session 2, IU-SLIS 19

slide-20
SLIDE 20

Database System vs. File System

Database Systems: Design, Implementation, & Management: Rob & Coronel

slide-21
SLIDE 21

Network Database: Example

Database Systems: Design, Implementation, & Management: Rob & Coronel

slide-22
SLIDE 22

Database Design

slide-23
SLIDE 23

Relational and Non-Relational Databases

There are two broad classes of databases, relational and non-relational. We are going to focus on relational databases, which were introduced in the late 1970’s and remain the most widely used approach today.

slide-24
SLIDE 24

Tables, Records and Fields

In a relational database, data is organized into tables, where: Each row in a table represents a record -- a “thing” Each column in a table represents a field -- an “attribute” A collection of tables form a database. But this is all most easily seen with an

  • example. Here we see a single table that

contains the attributes of several students.

Field Record

slide-25
SLIDE 25

Relationships Between Tables

The structure of databases comes from relationships, which are connections between records in different tables.

Course

slide-26
SLIDE 26

How do relationships work?

There are two parts to a relationship, a primary key and a foreign key. The primary key is a unique way of identifying a record in the table Generally:

  • every table will have a primary key field
  • all records must have a value in the primary

key field

  • the primary key’s value must be unique

Primary Key

slide-27
SLIDE 27

Question Which field makes a good primary key in this table?

slide-28
SLIDE 28

Foreign keys

A foreign key refers to a primary key in another table. This creates a connection or relationship between the two records.

Primary Key Primary Key

Foreign Key Foreign Key

slide-29
SLIDE 29

Types of Relationships

There are three kinds of relationship that one can build using primary keys (PK) and foreign keys (FK) One to many

  • ne record (ie one row) in Table A can be related to multiple records in Table B

One to one

  • ne record in Table A can be related to exactly one record in Table B

Many to many multiple records in Table A can be related to multiple records in Table B

slide-30
SLIDE 30

One to many

One record in Table A can be related to multiple records in Table B Example: Each enrollment refers to a single

  • student. But each student can have several

enrollments. One record in the Students Table can be related to multiple records in the Enrolments Table (but not vice versa)

Foreign Key

Primary Key

1

Table A Table B

slide-31
SLIDE 31

One to one

One record in Table A is related to exactly

  • ne record in Table B

This relationship is relatively rare, as often it makes more sense for Table A and Table B to be merged into a single table. Sometimes this relationship is used when some data is costly to access and less frequently accessed.

STUDENTS ID (primary key) Full Name Date of Birth PHOTOS ID (primary key) ImageData

1 1

slide-32
SLIDE 32

Here is an example of the many-to-many relationship. The roles table can be thought of as saying: “Sarah Polley played the role of Ana in Dawn of the Dead” “Sarah Polley played the role of Elsa in Splice” “Ben Affleck played the role of Bruce Wayne in Batman vs. Superman” “Ving Rhames played the role of Kenneth Hall in Dawn of the Dead” Each actor can be associated with multiple films. Each film can be associated with multiple actors!

Many-to-many relationship

ACTORS ID First Name Last Name 1 Sarah Polley 2 Ving Rhames 3 Ben Affleck FILMS ID Title 1 Dawn of the Dead 2 Splice 3 Batman vs. Superman ROLES ID Role ActorID FilmID 1 Ana Clark 1 1 2 Elsa 1 2 3 Bruce Wayne 3 3 4 Kenneth Hall 2 1

slide-33
SLIDE 33

This is a relationship diagram, showing a database with 4 tables and the relationships between them EXERCISE

  • 1. What is the relationship between

actors and roles?

  • 2. What is the relationship between

roles and films?

  • 3. What is the relationship between

actors and photos?

  • 4. What is the relationship between

actors and films?

Relationship diagrams

ACTORS ID (primary key) Full Name Date of Birth FILMS ID (primary key) Title ROLES ID (primary key) Role Actor ID (foreign key) Film ID (foreign key)

1

∞ ∞

1

PHOTOS ID (primary key) ImageData

1 1

slide-34
SLIDE 34

Question Why is this relationship perhaps not a good idea?

ACTORS ID (primary key) Full Name Photo ID (foreign key) FILMS ID (primary key) Title ROLES ID (primary key) Role Actor ID (foreign key) Film ID (foreign key)

1

∞ ∞

1

PHOTOS ID (primary key) ImageData

1 1

slide-35
SLIDE 35

Question Why is this relationship perhaps not a good idea?

A: It is not uncommon for multiple actors to play the same role (e.g. young and old versions of the same role). Designing DB and the relationships can be hard!

ACTORS ID (primary key) Full Name Photo ID (foreign key) FILMS ID (primary key) Title ROLES ID (primary key) Role Actor ID (foreign key) Film ID (foreign key)

1

∞ ∞

1

PHOTOS ID (primary key) ImageData

1 1

slide-36
SLIDE 36

Referential Integrity

One of the advantages of using a DBMS is it can force you to keep relationships valid. A database with “referential integrity” is one in which “the references in the database all make sense” More formally, referential integrity requires all values of a foreign key field to be:

  • present in the related primary key field, OR
  • Null (ie. blank)

ROLES ID Role ActorID (FK) 1 Ana 1 2 Elsa 1 3 Bruce Wayne 3 4 Charlie Brown 4 5 Claire Underwood Null ACTORS ID (PK) First Name Last Name 1 Sarah Polley 2 Laurence Fishburne 3 Ben Affleck

slide-37
SLIDE 37

Review

A database is a collection of data that is systematically organized, so as to allow efficient addition, modification, removal and retrieval. A relational database is a collection of tables, where each row of the table is a record and each column is a field. Databases use foreign keys and primary keys to establish relationships between records on different tables. A database has referential integrity when all of the values in all foreign key fields point to the primary key of an existing record in the appropriate table (or are null).