lis651 lecture 5 multiple arrays and databases thomas krichel 2010-02-19
TRANSCRIPT
databases
• Databases are collection of data with some organization to them.
• The classic example is the relational database. • But not all database need to be relational
databases.
relational databases
• A relational database is a set of tables. There may be relations between the tables.
• Each table has a number of record. Each record has a number of fields.
• When the database is being set up, we fix – the size of each field – relationships between tables
example: Movie database
ID | title | director | date
M1 | Gone with the wind | F. Ford Coppola | 1963
M2 | Room with a view | Coppola, F Ford | 1985
M3 | High Noon | Woody Allan | 1974
M4 | Star Wars | Steve Spielberg | 1993
M5 | Alien | Allen, Woody | 1987
M6 | Blowing in the Wind| Spielberg, Steven | 1962
• Single table• No relations between tables, of course
problem with this database
• All data wrong, but this is just for illustration.• Name covered inconsistently. There is no way to
find films by Woody Allan without having to go through all spelling variations.
• Mistakes are difficult to correct. We have to wade through all records, a masochist’s pleasure.
Better movie database
ID | title | director | year
M1| Gone with the wind | D1 | 1963
M2| Room with a view | D1 | 1985
M3| High Noon | D2 | 1974
M4 | Star Wars | D3 | 1993
M5| Alien | D2 | 1987
M6| Blowing in the Wind | D3 | 1962
ID | director name | birth year
D1 | Ford Coppola, Francis| 1942
D2 | Allan, Woody | 1957
D3 | Spielberg, Steven | 1942
Relational database
• We have a one to many relationship between directors and film– Each film has one director– Each director has produced many films
• Here it becomes possible for the computer– To know which films have been directed by Woody
Allen– To find which films have been directed by a director
born in 1942
Many-to-many relationships
• Each film has one director, but many actors star in it. Relationship between actors and films is a many to many relationship.
• Here are a few actorsID | sex | actor name | birth year
A1 | f | Brigitte Bardot | 1972
A2 | m | George Clooney | 1927
A3 | f | Marilyn Monroe | 1934
Actor/Movie table
actor id | movie id
A1 | M4
A2 | M3
A3 | M2
A1 | M5
A1 | M3
A2 | M6
A3 | M4
… as many lines as required
Many-to-many relationships
• Each film has one director, but many actors star in it. Relationship between actors and films is a many to many relationship.
• Here are a few actorsID | sex | actor name | birth year
A1 | f | Brigitte Bardot | 1972
A2 | m | George Clooney | 1927
A3 | f | Marilyn Monroe | 1934
Many-to-many relationships
• Each film has one director, but many actors star in it. Relationship between actors and films is a many to many relationship.
• Here are a few actorsID | sex | actor name | birth year
A1 | f | Brigitte Bardot | 1972
A2 | m | George Clooney | 1927
A3 | f | Marilyn Monroe | 1934
Actor/Movie table
actor id | movie id
A1 | M4
A2 | M3
A3 | M2
A1 | M5
A1 | M3
A2 | M6
A3 | M4
… as many lines as required
databases in libraries
• Relational databases dominate the world of structured data
• But not so popular in libraries– Slow on very large databases (such as catalogs)
– Library data has nasty ad-hoc relationships, e.g.• Translation of the first edition of a book
• CD supplement that comes with the print version
Difficult to deal with in a system where all relations and field have to be set up at the start, can not be changed easily later.
databases in web sites• Lots of active web sites are driven by relational
databases. All large active web sites are.• The design of a active web site first involves
looking at databases.• In a shop situation, we have the following tables
– customers– products– orders– orders_products
for multiple to multiple relationship between orders and products.
SQL
• SQL, also pronounced sequel, stands for "structured query language".
• It is a standard language for querying database. • In database speak a query is anything one can do
to a database.
mySQL
• They are a very successful, open-source vendor of SQL software.
• Their product is basically freely available. • We will learn the mySQL dialect of SQL.
phpmyadmin
• phpmyadmin is a set of PHP scripts that create a general purpose interface to work with a mySQL database.
• It is written in PHP. • It lives at http://wotan.liu.edu/phpmyadmin.• You need an account. This is not your wotan
account, but a permission to use a database on the mySQL server running at wotan.
arrays and tables
• Arrays seem to cause the most confusion in student understanding.
• An array is just a way for PHP to address multiple things as one variable.
• Arrays can be of multiple dimensions. • This is typically the case when the array
represents the contents of a table.
example
• Here is an example tablename type brewer rating price
Bruch Festbock dark Bruch good 1.18
Balitka 8 wheat Baltika good 0.88
Budweiser light A.-B. lousy 0.99
• typically, records appear in lines and fields in columns.
arrays and tables
• Arrays seem to cause the most confusion in student understanding.
• An array is just a way for PHP to address multiple things as one variable.
• Arrays can be of multiple dimensions. • This is typically the case when the array
represents the contents of a table.
example
• Here is an example tablename type brewer rating price
Bruch Festbock dark Bruch good 1.18
Balitka 8 wheat Baltika good 0.88
Budweiser light A.-B. lousy 0.99
• typically, records appear in lines and fields in columns.
one way to set out the table
$beers[0]['name']='Bruch Landbock';
$beers[0]['type']='dark';
$beers[0]['brewer']='Bruch‘;
….
$beers[2]['price']=0.99;
• Here, records are a numeric array. Fields are string arrays that are members of the numeric array.
• What instruction would improve the rating of Budweiser?
another way …
$beer=array( 'name'=> 'Bruch Landbock', type=>'dark', 'brewer'=> 'Bruch', 'rating'=>'good', price=>'1.18');
$beers[]=$beer;
…
$beer=array( 'name'=>'Budweiser', type=>'light', 'brewer'=>'A.-B.', 'rating'=>'lousy', price=>0.99);
$beers[]=$beer;
• This will give the same array as before.
yet another way, as a matrix
$names=array('Bruch Landbock', 'Baltika 8','Budweiser');
$types=array( 'dark', 'wheat', 'light' );
$brewers=array( 'Bruch', 'Baltika', 'A.-B.');
$ratings=array( 'good', 'good', 'lousy');
$prices=array(1.18,0.88,0.99);
$beers=array( $names, $types,$brewers,$ratings, $prices);
• What instruction would improve the rating of Budweiser?
another way to set out the table
$names=array('Bruch Landbock', 'Baltika 8','Budweiser');
$types=array( 'dark', 'wheat', 'light' );
$brewers=array( 'Bruch', 'Baltika', 'A.-B.');
$ratings=array( 'good', 'good', 'lousy');
$prices=array(1.18,0.88,0.99);
$beers=array( 'name'=>$names, 'type'=>$types, 'brewer'=> $brewers, 'rating'=>$ratings, 'price'=>$prices);
• What instruction would improve the rating of Budweiser?
another way to set out the table
$names=array('Bruch Landbock', 'Baltika 8','Budweiser');
$types=array( 'dark', 'wheat', 'light' );
$brewers=array( 'Bruch', 'Baltika', 'A.-B.');
$ratings=array( 'good', 'good', 'lousy');
$prices=array(1.18,0.88,0.99);
$beers=array( 'name'=>$names, 'type'=>$types, 'brewer'=> $brewers, 'rating'=>$ratings, 'price'=>$prices);
• What instruction would improve the rating of Budweiser?
multiple arrays• Example
$products[0]['name']="Grosswald Pilsener";
$products[0]['price']=1.56;
$products[1]['name']="Grosswald Export";
$products[1]['price']=1.34;
$products[2]['name']="Bruch Landbier";
$products[2]['price']=1.22;