Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
NOT NULL constraint
2. NOT NULL recap – defining a column as NOT NULL
Summary

Instruction

Good. As you probably remember, a NOT NULL constraint can be added when we define a table:

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

The NOT NULL constraint on the amount column means that whenever we add a new row to the table, we must provide a value for amount. The database won't accept a NULL.

Keep in mind that any column without the NOT NULL constraint accepts NULL by default. You can (but don't have to) emphasize that fact by the NULL keyword after the column definition. For example, we could have defined the customer_id column like this with the same meaning:

customer_id integer NULL

Exercise

A nongovernmental organization (NGO) wants our help in managing their human rights campaigns. Create a table named campaign that has the following columns:

  1. id – An integer and the primary key.
  2. name – A text up to 32 characters.
  3. the start_date – A date column that must never be NULL.
  4. budget – A decimal number up to 9999.99 (two digits after the decimal point).