21
What is a database? Logiskt sammanhängande mängd av data, med en därtill hörande betydelse, strukturerad och försedd med data avsedda för ett visst ändamål, med en viss användargrupp i åtanke och återspeglande någon aspekt av världen. What is a database management system? A set of programs allowing a user to create and maintain databases. Introduction to databases

Introduction to databases

  • Upload
    tejano

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to databases. What is a database? Logiskt sammanhängande mängd av data, med en därtill hörande betydelse, strukturerad och försedd med data avsedda för ett visst ändamål, med en viss användargrupp i åtanke och återspeglande någon aspekt av världen. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to databases

What is a database?

Logiskt sammanhängande mängd av data, med en därtill hörande betydelse, strukturerad och försedd med data avsedda för ett visst ändamål, med en viss användargrupp i åtanke och återspeglande någon aspekt av världen.

What is a database management system?

A set of programs allowing a user to create and maintain databases.

Introduction to databases

Page 2: Introduction to databases

Database system

Application programs / Queries

Programs for query management

Programs for data management

Metadata Database

User / Programmer

DBMS

DBMS

Page 3: Introduction to databases

Why databases?

Persistence

Sharing

Data independence

Page 4: Introduction to databases

Hierarchical databasesNetwork databases

Relational databases

Types of databases

Page 5: Introduction to databases

A relation scheme is a set of attributes Example: PERSON(SS#, Name, Age, Salary)

Every attribute has a domain Example: Name has the domain String, Age has the domain Integer

A tuple for a relation scheme gives a value to each attribute in the scheme Example: (778899, John Smith, 26, 13000)The value for each attribute must be in the domain

A relation is a set of tuples

Relational databases

Page 6: Introduction to databases

SS# Name Age Salary

650101-2288 Eva Svensson 33 25000750203-3133 Per Jonsson 23 20000500107-5532 Sven Olsson 47 25000800515-0044 Pia Eriksson 17 18000

PERSON

Relationscheme

Relation

A relation can be viewed as a table without duplicates

Relational databases

Page 7: Introduction to databases

school pupil

course

teacher subject

library borrower

loan

book

school ((sNr), address)pupil ((eNr), firstName, surName)teacher ((lNr), firstName, surName)subject ((äNamn), courseBook)course ((sNr, eNr, lNr, cName)

library ((bName), address)borrower ((lNr), firstName, surName)book ((ISBN), title)loan ((bName, lNr, ISBN, date)

Reality

Subset

Model

Database structure

From reality to database

Page 8: Introduction to databases

Why design?

Why not a simple table?

ISBN Title Author Library Address Borrower BorrowerAdd Date

12345 Mitt liv Pelle Stora Storgatan 19 Kalle Karlavägen 12 891102 23456 Ditt liv Lisa Stora Storgatan 19 Kalle Karlavägen 12 890723 12346 Vårt liv Pelle Stora Storgatan 19 Vera Verdandig 3 890809 12347 Vilket liv! Lisa Stora Storgatan 19 Mona Månvägen 7 891011 23412 Mitt liv Lisa Stora Storgatan 19 Kalle Karlavägen 12 891112121212 Stickning Johan Stora Storgatan 19 Kalle Karlavägen 12 890909 1212 Matlagning Eva Stora Storgatan 19 Vera Verdandig 3 891010 100 Matematik Vera Stora Storgatan 19 Mona Månvägen 7 891102

Library borrower

loan

book

library ((bNamn), address)borrower ((lNr), firstName, surName)book ((ISBN), title)loan ((bNamn, lNr, ISBN, date)

Database design

Page 9: Introduction to databases

An unnormalised relation scheme

Name Regno Share Salary Model

Per Eriksson ABC123 50 25000 VolvoEva Olsson ABC123 50 18000 VolvoPer Eriksson DEF456 100 25000 MercedesPer Eriksson GHI789 50 25000 ToyotaPia Johnsson GHI789 50 30000 ToyotaPia Johnsson BCD321 100 30000 FordBo PerssonCDE654100 18000Volvo

Page 10: Introduction to databases

Problems with unnormalised schemes

Redundancy

Update anomalies

Page 11: Introduction to databases

Functional dependencies

A functional dependency means that one attribute uniquely determines another attribute.

Example: Name --> Salary

This functional dependency means that if two tuples have the same value on Name, then they must have the same value on Salary.

Page 12: Introduction to databases

Functional dependencies

A B C D E

x y z w xy y y x y

z y z w z

x x w z y

w z x w y

z x x y w

Which ones of the functional dependencies are satisfied by the relation above?A --> B AB --> C CD --> B CD --> E

Page 13: Introduction to databases

Keys

A key in a relation scheme is an attribute (or a minimal set of attributes) that functionally determines all the other attributes in the scheme.

Thus, a key uniquely identifies a tuple in a relation.

What is the key in this relation scheme?

MOVIE(Film, Theatre, Time, Price)

Page 14: Introduction to databases

First normal form

A relation scheme is in first normal form if all attribute values are atomic.

SS# Surname First name750101-0032 Svensson Gunnar, Sven550401-0044 OlssonKarin, EvaSS# Surname First name750101-0032 Svensson Gunnar750101-0032 Svensson Sven550401-0044 OlssonKarin550401-0044 Olsson Eva

Not 1NF

1NF

What arethe keys?

Page 15: Introduction to databases

Second normal form

A relation scheme is in second normal form if every attribute is functionally dependent on the whloe key.

Name Regno Salary

Per Eriksson ABC123 25000Per Eriksson DEF456 25000Pia Johnsson GHI789 30000Pia Johnsson BCD321 30000

Name Regno

Per Eriksson ABC123Per Eriksson DEF456Pia Johnsson GHI789Pia Johnsson BCD321

Name Salary

Per Eriksson 25000Pia Johnsson 30000

Page 16: Introduction to databases

Third normal form

A relation scheme is in third normal form if

each attribute is functionally dependent on the key, the whole key, and nothing else than the key.

Page 17: Introduction to databases

Third normal form

If an attribute does not satisfy the condition for 3NF, it is removed from the relation scheme. It will form a new relation scheme together with the attributes it is functionally dependent on.

Example:PERSON(SS#, Name, Country, Number_of_inhabitants)SS# --> Name, CountryCountry --> Number_of_inhabitants

Number_of_inhabitants does not satisfy the condition for 3NF. It is removed and will form a new relation schema together with Country:

PERSON(SS#, Name, Country)COUNTRY(Country, Number_of_inhabitants)

Page 18: Introduction to databases

Third normal form

Decompose the following relation scheme to relation schemes in 3NF.

BOOK(Copy#, SS#, Date-of-loan, Return-date,

Library, Library_address, Person_address, Title)

A copy of a book with a title is borrowed by a person, who has an address, at a date and is returned at another date. The copy of the book resides at a library with an address.

Page 19: Introduction to databases

school pupil

course

teacher subject

library borrower

loan

book

school ((sNr), address)pupil ((eNr), firstName, surName)teacher ((lNr), firstName, surName)subject ((äNamn), courseBook)course ((sNr, eNr, lNr, cName)

library ((bName), address)borrower ((lNr), firstName, surName)book ((ISBN), title)loan ((bName, lNr, ISBN, date)

Reality

Subset

Model

Database structure

From reality to database

Page 20: Introduction to databases

From conceptual schema to database

Main steps

1. Every object type becomes a relation scheme

2. Every 1-1 and 1-m attribute of an object type becomes an attribute in the corresponding relation scheme

3. Every m-m attribute becomes a relation scheme - the attributes in this scheme are the key attributes in the associated relation schemes

Page 21: Introduction to databases

From conceptual schema to database

PERSON CAR

String Integer StringString

owns (m,m,p,p)regno

modelagename