Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
NOT NULL constraint
4. Adding NOT NULL to existing tables
Summary

Instruction

Good! We can already define a column as NOT NULL, but how about adding a NOT NULL constraint to existing tables? Let's say we still have the same loan_application table:

CREATE TABLE loan_application (
  id integer PRIMARY KEY,
  customer_id integer,
  amount decimal(10, 2) NOT NULL,
  loan_period_months integer
);

We need another NOT NULL constraint, this time on the loan_period_months column. Take a look:

ALTER TABLE loan_application
ALTER COLUMN loan_period_months SET NOT NULL;

As you can see, adding a NOT NULL constraint is a bit different than adding a primary key. We need to use the ALTER COLUMN syntax along with the SET NOT NULL instruction. This operation will only succeed if all existing values in the column are not null.

Exercise

The NGO the campaign table looks like this now:

CREATE TABLE campaign (
  id integer PRIMARY KEY,
  name varchar(32),
  start_date date NOT NULL,
  budget decimal(10, 2)
);

Your task is to add a the NOT NULL constraint to the budget column.