2ª Forma Normale

Creare database PostgreSQL

Darryl Reeves

Industry Assistant Professor, New York University

Esempio: libri di testo scolastici

CREATE TABLE textbook (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    publisher_name VARCHAR(100) NOT NULL,
    publisher_site VARCHAR(50),
    quantity SMALLINT NOT NULL DEFAULT 0
);
Creare database PostgreSQL

Esempio: libri di testo scolastici

id title publisher_name publisher_site quantity
23 Introductory Algebra: 1st Edition ABC Publishing www.abc.com 32
74 Calculus Foundations ABC Publishing www.abc.com 27
112 Statistical Concepts Martin House www.mh.com 22
Creare database PostgreSQL

Esempio: incoerenza aggiornando l'URL

id title publisher_name publisher_site quantity
23 Introductory Algebra: 1st Edition ABC Publishing www.abc.com 32
74 Calculus Foundations ABC Publishing www.abc.com 27
112 Statistical Concepts Martin House www.mh.com 22
Creare database PostgreSQL

Esempio: incoerenza aggiornando l'URL

id title publisher_name publisher_site quantity
23 Introductory Algebra: 1st Edition ABC Publishing www.newabc.com 32
74 Calculus Foundations ABC Publishing www.abc.com 27
112 Statistical Concepts Martin House www.mh.com 22
Creare database PostgreSQL

Esempio: aggiungere un editore senza libro

id title publisher_name publisher_site quantity
23 Introductory Algebra: 1st Edition ABC Publishing www.abc.com 32
74 Calculus Foundations ABC Publishing www.abc.com 27
112 Statistical Concepts Martin House www.mh.com 22
Creare database PostgreSQL

Esempio: aggiungere un editore senza libro

id title publisher_name publisher_site quantity
23 Introductory Algebra: 1st Edition ABC Publishing www.abc.com 32
74 Calculus Foundations ABC Publishing www.abc.com 27
112 Statistical Concepts Martin House www.mh.com 22
?? ?? New Horizons www.nhorizon.com ??
Creare database PostgreSQL

Esempio: rimuovere un libro

id title publisher_name publisher_site quantity
23 Introductory Algebra: 1st Edition ABC Publishing www.abc.com 32
74 Calculus Foundations ABC Publishing www.abc.com 27
112 Statistical Concepts Martin House www.mh.com 22
Creare database PostgreSQL

Esempio: rimuovere un libro

id title publisher_name publisher_site quantity
23 Introductory Algebra: 1st Edition ABC Publishing www.abc.com 32
74 Calculus Foundations ABC Publishing www.abc.com 27
  • L'editore richiede una tabella separata
  • Anomalie nei dati da inserimenti ed eliminazioni
Creare database PostgreSQL

Soddisfare la 2ª Forma Normale (2NF)

  • 1NF è soddisfatta
  • Tutte le colonne non chiave dipendono dal PRIMARY KEY della tabella
Creare database PostgreSQL

Esempio: libri di testo ed editori in 2NF

CREATE TABLE textbook (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    publisher_name VARCHAR(100) NOT NULL,
    publisher_site VARCHAR(50),
    quantity SMALLINT NOT NULL DEFAULT 0
);
Creare database PostgreSQL

Esempio: libri di testo ed editori in 2NF

CREATE TABLE textbook (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    quantity SMALLINT NOT NULL DEFAULT 0,
);
CREATE TABLE publisher (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    site VARCHAR(50)
);
Creare database PostgreSQL

Esempio: libri di testo ed editori in 2NF

CREATE TABLE textbook (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    quantity SMALLINT NOT NULL DEFAULT 0,
    publisher_id INTEGER REFERENCES publisher(id)
);
CREATE TABLE publisher (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    site VARCHAR(50)
);
Creare database PostgreSQL

Esercitiamoci!

Creare database PostgreSQL

Preparing Video For Download...