PV168

Databases I


What is a Database

A database is an organized collection of structured information, or data, typically stored electronically in a computer system.

Multiple database types:


Why we need Databases


JDBC – Java Database Connectivity


JDBC URI

Examples:

H2 Database JDBC URI for specific modes:


SQL - Structured Query Language

Types:


Create a new Department Table

-- create a new Department table
CREATE TABLE IF NOT EXISTS Department
(
    id        BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    number    VARCHAR(10) NOT NULL UNIQUE,
    name      VARCHAR(50) NOT NULL,
    createdAt TIMESTAMP   NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Create a new Employee Table

-- create a new Emplotee table
CREATE TABLE IF NOT EXISTS Employee
(
    id           BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    firstName    VARCHAR(150) NOT NULL,
    lastName     VARCHAR(150) NOT NULL,
    birthDate    DATE         NOT NULL,
    departmentId BIGINT REFERENCES Department (id),
    createdAt    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Primary Key


Relationships

Are used for collections or compositions:


Foreign Key

FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY of another table.


Constraints and Default Indexes

Constraints:

Index is automatically created for:


Cascades


Information Schema


Migrations


Logging


Why Logging


What to log


How to log