Databases

Table of Contents

Introduction

database: collection of data with

Why not in files? There's no query language, a weak logical structure, no efficient access, almost no protection from data loss, no parallel access control.

ANSI SPARC architecture stores data in three levels:

  1. View level: application programs hide details of data types. Hide information for privacy/security
  2. Logical level ('conceptual schema'): describes data and relations among data
  3. Physical level: how data is stored, in disk pages, index structures, whatever else.

Relational databases

view and logical levels are data representations in relations/tables

a row is a tuple record. the order of the elements of the tuples doesn't matter.

a database instance is a 'snapshot' of a database at a certain point in time.

the database schema is the structure of the database - the relations and constraints.

constraints:

create an entity relationship model in UML:

Entity relationship model

then translate that into relations

Database management system

database management system (DBMS) allows:

multiple users, concurrent access. transactions have ACID properties:

DBMS have data independence and duplication avoidance.

SQL: Structured Query Language

SQL is declarative data manipulation language. The user says which conditions the retrieved data has to fulfill.

It's more concise than imperative languages, thus easier to maintain and cheaper to develop programs in it.

Users usually don't have to think about efficiency, the DBMS will manage that.

Creating a table with constraints:

CREATE TABLE solved (
    id INT AUTO_INCREMENT,
    name VARCHAR(40) NOT NULL,
    homework NUMERIC(2) NOT NULL,
    points NUMERIC(2) NOT NULL CHECK (points <= 10),
    PRIMARY KEY (id)
);

Creating a view:

CREATE VIEW solved_homework AS
    SELECT id, name, homework FROM solved;