Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Modifying columns
Modifying identities
9. Adding auto-generation of identities
Modifying sequences
Summary

Instruction

Perfect! By the same token, you can also add auto-generation to a column without auto-generation for an identity. First, look at the structure of the following table:

CREATE TABLE president(
  id integer PRIMARY KEY,
  full_name varchar(32),
  election_date date
);

Let's say we want to add auto-generation for the column id:

ALTER TABLE president ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY;

The code above will add auto-generation of an identity to our existing column. Note that instead of ADD GENERATED ALWAYS you can use ADD GENERATED BY DEFAULT.

Exercise

The agency has an opposite problem with another table. They've created a table named stage in the following way:

CREATE TABLE stage (
  id integer PRIMARY KEY,
  name varchar(64),
  rating decimal(2, 1),
  city varchar(32)
);

Your task is to add auto-generation by default as identity to the id column.