De not-null- en unique-constraints

Introductie tot relationele databases in SQL

Timo Grossenbacher

Data Journalist

De not-null-constraint

  • Sta NULL-waarden in een bepaalde kolom niet toe
  • Moet kloppen voor de huidige staat
  • Moet kloppen voor elke toekomstige staat
Introductie tot relationele databases in SQL

Wat betekent NULL?

  • onbekend
  • bestaat niet
  • niet van toepassing
  • ...
Introductie tot relationele databases in SQL

Wat betekent NULL? Een voorbeeld

CREATE TABLE students (
 ssn integer not null,
 lastname varchar(64) not null,
 home_phone integer,
 office_phone integer
);
NULL != NULL
Introductie tot relationele databases in SQL

Een not-null-constraint toevoegen of verwijderen

Bij het maken van een tabel...

CREATE TABLE students (
 ssn integer not null,
 lastname varchar(64) not null,
 home_phone integer,
 office_phone integer
);

Na het aanmaken van de tabel...

ALTER TABLE students 
ALTER COLUMN home_phone 
SET NOT NULL;
ALTER TABLE students 
ALTER COLUMN ssn 
DROP NOT NULL;
Introductie tot relationele databases in SQL

De unique-constraint

  • Sta geen dubbele waarden in een kolom toe
  • Moet kloppen voor de huidige staat
  • Moet kloppen voor elke toekomstige staat

Introductie tot relationele databases in SQL

Unique-constraints toevoegen

CREATE TABLE table_name (
 column_name UNIQUE
);
ALTER TABLE table_name
ADD CONSTRAINT some_name UNIQUE(column_name);
Introductie tot relationele databases in SQL

Laten we dit toepassen op de database!

Introductie tot relationele databases in SQL

Preparing Video For Download...