32
Introduction Databases SQL Exercising Wrap Up A (short) Introduction to Databases (v.145 2015-04-22) Nicola Bernardini [email protected] “S.Cecilia” Conservatory - Rome April 22 2015 Service Systems Design Master Aalborg University in Copenhagen Copenhagen, Denmark Nicola Bernardini [email protected] Databases

A (short) introduction to Databases

Embed Size (px)

Citation preview

Page 1: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

A (short) Introduction to Databases(v.145 2015-04-22)

Nicola [email protected]

“S.Cecilia” Conservatory - Rome

April 22 2015Service Systems Design Master

Aalborg University in CopenhagenCopenhagen, Denmark

Nicola Bernardini [email protected] Databases

Page 2: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

Topics we will cover

Just a touch of history

Why do we need databases

What are they, anyway?

How do they work (in a very basic form)

Exercising database design

Nicola Bernardini [email protected] Databases

Page 3: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

Topics we will NOT cover

(but feel free to ask about them if you feel so inclined, by anymeans :-))

Advanced topics, such as:

search algorithmsdata indexingcomplex multi–table queriessophisticated database functions. . .

Nicola Bernardini [email protected] Databases

Page 4: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

What are computers for? (1)

Figure: A Leibnitz computingmachine

Back in the old days (I meanthe eighteenth century). . .

. . . computers were conceivedand built for calculus andcomputation

Mathematicians found out thatreality was quite morecomplicate than the abstractmodels they had conceived. . .

. . . so they invented “computingtechnologies”, that iscomputers

Nicola Bernardini [email protected] Databases

Page 5: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

What are computers for? (2)

Figure: An IBM 7094 (1962)

The last time computers wereused solely for computingpurposes was during World WarII

At the end of WW II,computing technology wasmature enough to enter theindustrial world. . .

. . . and indeed, the computingcapability of computers fadedinto the background

Nicola Bernardini [email protected] Databases

Page 6: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

What are computers for? (3)

Today, the most useful functionof computers is. . .

. . . their ability to store andorganize data

and this is where databaseskick in.

Computation is still used ofcourse, but it is no longer thecore function (it appears inseveral specific fields in whatare now called “numbercrunching applications”)

Nicola Bernardini [email protected] Databases

Page 7: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

So why do we need databases? (1)

int i = 42;

4238

11432

1923

972397249725

Ram Memory

Addresses

4238

11432

1923

972397249725

Ram Memory

Addresses

As you all know, computersstructure (and store)information in RAM memory

Data maps into memorystraight memory

Memory is characterized by acontent and an address(pointer)

An address can also beconsidered some form ofcontent

Nicola Bernardini [email protected] Databases

Page 8: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

So why do we need databases? (2)

struct birthday {

int day;

int month;

int year;

};

yearday

monthyear

monthday

972197249727

Ram Memory

Addresses

Ram Memory

Addresses

Furthermore, data in memorycan be structured

That is, it can be organized sothat complex structures arekept physically together in RAMmemory

In the past few decades, thesestructures have become object,in object–oriented programminglingo

Nicola Bernardini [email protected] Databases

Page 9: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

So why do we need databases? (3)

However, data in RAM memory is not persistent

When the computer shuts off, the data is gone

So: data needs to be persisted

Which translates into: databases

Nicola Bernardini [email protected] Databases

Page 10: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

How many different kinds of databases are there?

You can make databases (that is: save your data) with justabout anything

log filesline oriented delimited text(f. ex. Nicola;Bernardini;Rome;14/08/1956;+4512345678)spreadsheetsdisk file systemsproper relational database

only the last two do not save only data but also relations

Nicola Bernardini [email protected] Databases

Page 11: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

What are relations for? (1)

If you start collecting data you soon realize that

a) either you repeat parts of the data endlesslyb) or you have to save data relations along with the data itself

and here we get to the golden rule of databases, namely. . .

Nicola Bernardini [email protected] Databases

Page 12: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

The Golden Rule of Databases

DO NOT REPLICATEDATA.

Nicola Bernardini [email protected] Databases

Page 13: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

Rationale for the golden rule

You don’t want to replicate data because you might need tochange it in the future: if your data is replicated all over, youneed to change it all over (very error-prone)

while if you keep relationships instead, a single change will besufficient for each piece of datum you have

Nicola Bernardini [email protected] Databases

Page 14: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

What are relations for? (2)

This is what relations are for. For example:

If you have one or more telephone numbers for each person inyour agenda (which is likely, or make it e-mail addresses, orwhatever)You need to be able to express something like “a person canhave many telphone numbers” or like “this number belongs tothis person”Both sentences basically establish a one–way relation betweenpersons and phone numbers

Nicola Bernardini [email protected] Databases

Page 15: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

How do you represent relationships inside computers? (1)

struct person {

char first_name[256];

char last_name[256];

};

struct number {

struct person *owner; /* <- */

int number;

};

struct person persons[1000];

struct number numbers[10000];

When your data is stored inRAM. . .

. . . you represent relationshipsby memory pointers (also knownas references) (check the arrow)

but what happens when youwant to persist the relationship?(== save it to disk)

this (and some more) is whatrelational databases are for

Nicola Bernardini [email protected] Databases

Page 16: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

How do you represent relationships inside computers? (2)

Before we go on, a question for you.

How would you create a persistent telephone book. . .with a filesystem?

Nicola Bernardini [email protected] Databases

Page 17: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

How do you represent relationships inside computers? (3)

Current relational database can do much more than that

Within databases, you

add a progressive number to each data structurethis number is guaranteed (by the database engine) to beuniqueit is called the primary key

Primary keys act like persistent pointers

Nicola Bernardini [email protected] Databases

Page 18: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

How do you represent relationships inside computers? (4)

Relationships are expressed with further indexes, called foreignkeys

Theoreticians have established that with only three kinds ofrelationships you can describe any type of relation among data

These relationships are. . .

Nicola Bernardini [email protected] Databases

Page 19: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

How do you represent relationships inside computers? (5)

one–to–one (f.ex: a husband has only one wife)

one–to–many (f.ex: a football team has many players)

many–to–many (f.ex: a student has many subjects)

Nicola Bernardini [email protected] Databases

Page 20: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

One–to–one relationships

you establish one–to–onerelationships by:

creating a first table with anauto–generated primary keycreating a second table with a(not auto–generated) foreignkey which points to aninstance of the first table

Nicola Bernardini [email protected] Databases

Page 21: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

One–to–many relationships

you establish one–to–manyrelationships by:

creating a first table with anauto–generated primary keycreating a second table withan auto–generated primarykey and a foreign key whichpoints to an instance of thefirst table

Nicola Bernardini [email protected] Databases

Page 22: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

Many–to–many relationships

you establish many–to–many relationships by:

creating two separate tables with an auto–generated primarykeyscreating a third table with two foreign keys each pointing toone instance of one of the first two tables

Nicola Bernardini [email protected] Databases

Page 23: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up Introduction Types Relations

A real–world example

Nicola Bernardini [email protected] Databases

Page 24: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up SELECT

A structured query language (SQL) (1)

Of course, structuring data is not enough to be able to use itefficiently

You also need to have a way to create, browse, view, update,delete data

Such a language exists: it is called SQL (spelled: sequel –which stands precisely for Structured Query Language)

With minor changes, SQL can be used over a multitude ofdifferent databases (sqlite, MySql, PostgreSQL, Oracle,Informix, etc.)

Nicola Bernardini [email protected] Databases

Page 25: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up SELECT

A structured query language (SQL) (2)

SQL is a text–based language: you type some instruction on adatabase console and the database will reply with an answer

Of course it can also be scripted and put into a file

SQL has several commonly used statements:

“SELECT”, “INSERT”, “UPDATE”, “DELETE”,“CREATE” and “DROP”

“SELECT” is the most commonly used, as it is the statementthat allows you to make queries (it does not change thedatabase)

Nicola Bernardini [email protected] Databases

Page 26: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up SELECT

The “SELECT” statement (1)

Here is what the “SELECT” statement looks like:

select "column1"

[,"column2",etc]

from "tablename"

[where "condition"];

Nicola Bernardini [email protected] Databases

Page 27: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up SELECT

The “SELECT” statement (2)

Some “SELECT” examples:

select first, last, city from employees where

first like ’Er%’; find all first names that begin with ‘Er’and display first name, last name and city of operationselect * from employees where first = ’Eric’; findall first names that match the name ‘Eric’ exactly and displayall data from themselect last, city, age from employees where age >

30; find all employees which are older than 30 years of ageand display last name, city and ageselect last, city, age from employees where (age >

30) and (last like ’%s’); find all employees which areolder than 30 years of age and have a last name that ends in‘s’ and display last name, city and age. . . let’s do some examples live. . .

Nicola Bernardini [email protected] Databases

Page 28: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

Live examples

Please download the employees.sqlite from Moodle

If you have a Mac or a Linux laptop, you may open a terminaland type sqlite3 employees.sqlite in the folder whereyou downloaded the db

if you have Windows, you may find a pre–compiled binary athttps://www.sqlite.org/download.html

Nicola Bernardini [email protected] Databases

Page 29: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

Walk-through exercise

Let’s imagine reverse–engineering. . . FaceBook at leastpartially :-)

Nicola Bernardini [email protected] Databases

Page 30: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

DIY Exercise (30 minutes)

1 Pick your own data set and/or application

2 Design it in terms of a relational database:

1 Establish tables2 Establish relationships among tables, making sure that no data

is replicated

3 Discuss your design

Nicola Bernardini [email protected] Databases

Page 31: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

What have we covered

1 What are databases and why do they exist at all

2 How they are conceived

3 What are data relationships

4 An introduction to the SQL language

5 How to design proper database structures (with someexercising)

Nicola Bernardini [email protected] Databases

Page 32: A (short) introduction to Databases

Introduction Databases SQL Exercising Wrap Up

THANK YOU!

Nicola Bernardini [email protected] Databases