Description

A database is a set of related information. A database system (DBMS) is a collection of interrelated data together with a set of programs that allow users to access and modify that data. A major purpose of a database system is to give users an abstract view of the data, hiding the complexity of how data is stored and maintained.

Where Databases Are Used

  • Enterprise information: sales (customers, products, purchases), accounting (payments, receipts, assets), human resources (employees, salaries, payroll taxes).
  • Manufacturing: production, inventory, orders, supply chain.
  • Banking and finance: customer information, accounts, loans, card transactions, real-time market data.
  • Universities: registration, grades.
  • Airlines: reservations, schedules.
  • Telecommunication: records of calls, texts and data usage, monthly bills, prepaid balances.
  • Web services: online retailers (order tracking, recommendations), online advertisements.
  • Document databases and navigation systems (places of interest, roads, train and bus routes).

Purpose of Database Systems

Early database applications were built directly on top of file systems, which caused:

  1. Data redundancy and inconsistency: the same data is stored in multiple files and formats.
  2. Difficulty in accessing data: a new program must be written for every new task.
  3. Data isolation: multiple files and formats.
  4. Integrity problems: constraints (e.g., balance > 0) are buried in program code and are hard to change.
  5. Atomicity problems: a failure may leave the database with partial updates (e.g., a funds transfer that only completes half-way).
  6. Concurrent-access anomalies: two users reading a balance of 100 and each withdrawing 50 at the same time can corrupt the result.
  7. Security problems: hard to give users access to some, but not all, data.

Database systems offer solutions to all of the above.

View of Data

  • Data model: a collection of conceptual tools for describing data, data relationships, data semantics and consistency constraints.
  • Data abstraction: hides the complexity of data structures from users through several levels.

Data Models

  • Relational model
  • Entity-Relationship model (mainly for database design)
  • Object-based models (object-oriented, object-relational)
  • Semi-structured model (XML)
  • Older models: network model, hierarchical model

The Relational Model

  • Proposed by Dr. E. F. Codd in 1970 in the paper "A Relational Model of Data for Large Shared Data Banks".
  • Data is represented as sets of tables (rows and columns).
  • Instead of pointers to navigate between related entities, redundant data (keys) is used to link records in different tables.

Levels of Abstraction

LevelDescribes
Physical levelHow a record (e.g., an instructor) is physically stored
Logical levelWhat data is stored and the relationships among the data
View levelWhat parts of the data each application/user sees; views can hide information (e.g., salary) for security

Instances and Schemas

  • Logical schema: the overall logical structure of the database (analogous to the type of a variable in a program).
  • Physical schema: the overall physical structure of the database.
  • Instance: the actual content of the database at a particular moment (analogous to the value of a variable).

Physical Data Independence

The ability to modify the physical schema without changing the logical schema. Applications depend on the logical schema, so interfaces between levels should be well defined so that changes in one part do not seriously affect others.

Database Languages

Data Definition Language (DDL)

Specifies the database schema. The DDL compiler stores the result in the data dictionary, which holds metadata (data about data): the schema, integrity constraints (e.g., primary keys) and authorization (who can access what).

CREATE TABLE instructor (
    ID        CHAR(5),
    name      VARCHAR(20),
    dept_name VARCHAR(20),
    salary    NUMERIC(8,2)
);

CREATE TABLE corporation (
    corp_id SMALLINT,
    name    VARCHAR(30),
    CONSTRAINT pk_corporation PRIMARY KEY (corp_id)
);

Data Manipulation Language (DML)

Language for accessing and updating data. Also called a query language (the retrieval part).

  • Procedural DML: the user specifies what data is needed and how to get it.
  • Declarative (non-procedural) DML: the user specifies what data is needed without saying how. Easier to learn and use. SQL is declarative.
INSERT INTO corporation (corp_id, name)
VALUES (27, 'Acme Paper Corporation');

SELECT name FROM corporation WHERE corp_id = 27;

SQL and Application Programs

A SQL query takes one or more tables as input and always returns a single table.

SELECT name FROM instructor WHERE dept_name = 'Comp. Sci.';

SQL cannot read user input, draw on a display, or use the network. Those actions are written in a host language (C/C++, Java, Python) with embedded SQL, using either language extensions or an API such as ODBC/JDBC.

Database Design

  • Logical design: decide on the schema. Business decision: which attributes to record. Computer science decision: which relation schemas to create and how to distribute attributes among them.
  • Physical design: decide on the physical layout of the database.

Database Engine (Architecture)

A database system is split into three functional components:

1. Storage Manager

The interface between low-level stored data and application programs/queries. Responsibilities: interaction with the OS file manager, efficient storing, retrieving and updating of data. Components: authorization and integrity manager, transaction manager, file manager, buffer manager.

Data structures it implements: data files (the database itself), data dictionary (metadata), indices (pointers for fast access to values).

2. Query Processor

  • DDL interpreter: interprets DDL statements and records definitions in the data dictionary.
  • DML compiler: translates DML into an evaluation plan of low-level instructions; performs query optimization (picks the lowest-cost plan).
  • Query evaluation engine: executes the low-level instructions.

Query processing steps: 1) parsing and translation → 2) optimization → 3) evaluation.

3. Transaction Management

A transaction is a collection of operations that performs a single logical function. The transaction manager keeps the database consistent despite power failures, OS crashes and transaction failures; the concurrency-control manager controls interaction between concurrent transactions.

BEGIN;
UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice';
-- ... other statements ...
COMMIT;      -- or ROLLBACK; to undo everything

DBMS Products

MySQL, MS SQL Server (Microsoft), Oracle DB (Oracle), DB2 (IBM), PostgreSQL.

Why PostgreSQL?

PostgreSQL is an enterprise-class relational DBMS, on par with the best proprietary systems.

  • Open source with a large community
  • Cross-platform and fast
  • Stored procedures and functions in many languages (C, SQL, Python, JS, Ruby, etc.)
  • Many built-in data types, and you can define your own
  • NoSQL features (JSON, hstore)

Management tools: psql, DataGrip, phpPgAdmin, pgAdmin. Download: postgresql.org/download and jetbrains.com/datagrip (free for students).

Conclusion

A DBMS replaces ad-hoc file processing with a system that provides abstraction, integrity, atomicity, concurrency control and security, organized as a storage manager, a query processor and a transaction manager.