2e normaalvorm

PostgreSQL-databases maken

Darryl Reeves

Industry Assistant Professor, New York University

Voorbeeld: studieboeken op school

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
);
PostgreSQL-databases maken

Voorbeeld: studieboeken op school

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
PostgreSQL-databases maken

Voorbeeld: inconsistentie door url-bijwerking

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
PostgreSQL-databases maken

Voorbeeld: inconsistentie door url-bijwerking

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
PostgreSQL-databases maken

Voorbeeld: uitgever toevoegen zonder studieboek

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
PostgreSQL-databases maken

Voorbeeld: uitgever toevoegen zonder studieboek

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 ??
PostgreSQL-databases maken

Voorbeeld: een studieboek verwijderen

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
PostgreSQL-databases maken

Voorbeeld: een studieboek verwijderen

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
  • Uitgever hoort in aparte tabel
  • Data-anomalieën bij invoegen en verwijderen
PostgreSQL-databases maken

Voldoen aan 2e normaalvorm (2NF)

  • 1NF is voldaan
  • Alle niet-sleutelkolommen hangen af van de PRIMARY KEY van de tabel
PostgreSQL-databases maken

Voorbeeld: studieboeken en uitgevers 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
);
PostgreSQL-databases maken

Voorbeeld: studieboeken en uitgevers 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)
);
PostgreSQL-databases maken

Voorbeeld: studieboeken en uitgevers 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)
);
PostgreSQL-databases maken

Laten we oefenen!

PostgreSQL-databases maken

Preparing Video For Download...