DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed...

21
DATABASES (bases for your data)

Transcript of DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed...

Page 1: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

DATABASES

(bases for your data)

Page 2: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of databases. Databases may be stored on a computer and examined using a program. These programs are often called `databases', but more strictly are database management systems (DMS).

source: http://www.ucl.ac.uk/archaeology/cisp/database/manual/node1.html

Page 3: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Just as a card index or catalog has to be constructed carefully in order to be useful, so must a database on a computer. Similarly, just as there are many ways that a printed catalog can be organized, there are many ways, or models, by which a computerized database may be organized. One of the most common and powerful models is the `relational' model, and programs which use this model are known as relational database management systems (RDMS).

Page 4: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Computer-based databases are usually organized into one or more tables. A table stores data in a format similar to a published table and consists of a series of rows and columns. Just as a published table will have a title at the top of each column, so each column in a database table will have a name, often called a field name. Each row in a table will represent one example of the type of object about which data has been collected.

Page 5: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Every row in a table in a relational database must be unique, there must not be two identical rows. One or more columns are therefore designated the primary key (sometimes called the unique identifier) for the items contained within it.

Foreign keys are columns in a table which provide a link to another table.

Page 6: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Primary key color taste smell temperature

1 blue metallic phosphorous cold

2 red none none warm

3 none earthy putrid warm

4 white chalky none warm

Example: database: water, table: watersamples

Page 7: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

A common and powerful method for organizing data for computerization is the relational data model. The crafting of a relational database requires one to organize the data and break it into several different tables. This process of breaking data down into a series of tables is called normalization and is the first and most important step in designing a relational database.

Page 8: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Normalization is the process of identifying entities and their attributes, and defining the relationship between the entities.

There are three types of relationship between entities: one-to-one, one-to-many, and many-to-many.

Page 9: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

One-to-one relationship:This is the case where there is, for any one entity, only one example of another related entity.

One-to-many relationship:This is the case where there is, for any one entity, many examples of another entity. Here the information about each entity must be stored in separate tables.

Many-to-many relationship:This is the case where an entity can have many examples of another entity but this second entity can also have many examples of the first. This type of relationship necessitates the use of a third table, creating two one-to-many relationships

Page 10: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

PostgreSQL

PostgreSQL is an object-relational database management system (ORDBMS) based on POSTGRES, developed at the University of California at Berkeley Computer Science Department. POSTGRES pioneered many concepts that only became available in some commercial database systems much later.

It is a large project. Convince yourself of this fact:http://www.postgresql.org/docs/9.3/interactive/index.html

Page 11: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

PostgreSQL

All database system share two main features: the ability to allow one to enter data into the system and to get / select data from the system.

Page 12: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Discovery:

psql -d databasename -U username

databasename=#

\l :List databases\d :List tables in database\d table-name :Describe tableselect * from table-name :List table contents

Page 13: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Before you can select data, you have to have data (and data containers.) The main data container of a database is the table.

Create a table in PSQL:

CREATE TABLE mytable ( id serial NOT NULL PRIMARY KEY, color VARCHAR (25), smell VARCHAR (25) );

Page 14: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Insert values into a table:

INSERT INTO mytable (color, smell) VALUES('red', 'gross');

Page 15: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

PostgreSQL

Select data - general form:SELECT color, smell, … FROM mytable

Select all entries from a table:SELECT * FROM mytable;

Select only entries that are unique:SELECT DISTINCT color FROM mytable;

Select and order:SELECT DISTINCT ON color FROM mytableORDER BY datetime;

Page 16: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of
Page 17: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

CREATE TABLE weather(location varchar(40) NOT NULL,sample_collected timestamp NOT NULL,database_entered timestamp default now(),.....);

location | character varying(40) | not null sample_collected | timestamp without time zone | not null database_entered | timestamp without time zone | default now() barometer | double precision rain | double precision rainrate | double precision outhumidity | integer outtemp | double precision windgust | double precision windgustdir | double precision windspeed | double precision

Page 18: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Page 19: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

Page 20: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases

CREATE TABLE weather(location varchar(40) NOT NULL,sample_collected timestamp NOT NULL,database_entered timestamp default now(),.....);

location | character varying(40) | not null sample_collected | timestamp without time zone | not null database_entered | timestamp without time zone | default now() barometer | double precision rain | double precision rainrate | double precision outhumidity | integer outtemp | double precision windgust | double precision windgustdir | double precision windspeed | double precision

Page 21: DATABASES - RTS.RESEARCH A database is structured collection of data. Thus, card indices, printed catalogs of archaeological artefacts and telephone directories are all examples of

Databases