SQL: Creating Databases and Tables - NOT NULL Constraint

NOT NULL Constraint

The NOT NULL constraint enforces that a column must not accept NULL values.

Example: NOT NULL Constraint

-- Create Table with NOT NULL Constraints
CREATE TABLE learn_null (
first_name VARCHAR (50),
sales integer NOT NULL
);
## data frame with 0 columns and 0 rows
-- INSERT, should result in an error
INSERT INTO learn_null(first_name) VALUES ('John');
## NULL
-- INSERT, correct
INSERT INTO learn_null(first_name, sales) VALUES ('John', 12);
## data frame with 0 columns and 0 rows
-- SELECT
SELECT * FROM learn_null;
##   first_name sales
## 1       John    12