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

Instruction

Perfect! Now, what about deleting a NOT NULL constraint? Our table definition currently looks like this:

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

We now want to get rid of NOT NULL constraint on the amount column. To do that, we can use the following code:

ALTER TABLE loan_application
ALTER COLUMN amount DROP NOT NULL;

The code is very similar to the one we used to add a the NOT NULL constraint. Instead of SET NOT NULL, we simply use DROP NOT NULL. From this moment on, new rows can have a NULL value in the amount column.

Exercise

The NGO table looks like this now:

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

The situation has changed again. The NGO wants to add future campaigns that have no start date to the table. Remove the NOT NULL constraint from the start_date column.