Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Identity columns
Sequences
Summary

Instruction

As we previously stated, GENERATED ALWAYS AS IDENTITY makes it impossible to insert our own values into a given column. Luckily, we can change that. Take a look:

CREATE TABLE movie (
  id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  title varchar(64),
  year int,
  genre varchar(20)
);

This time, instead of ALWAYS we wrote BY DEFAULT. As a result, the database will only generate consecutive numbers when we don't provide a specific value ourselves. Let's check that.

Exercise

Try to add a new row to the movie table. It's a movie called Dancing Tonight from 2018 and the genre is musical. Do NOT specify the id column.

Will the id be auto-generated?