Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Defining columns with DEFAULT
6. DEFAULT and sequences – 1
Adding DEFAULT to existing tables
Summary

Instruction

Excellent! Now we have a small surprise for you. Do you remember sequences? We can actually link them to columns – let's try it! We'll use the DEFAULT constraint. Here it is with PostgreSQL syntax:

CREATE TABLE board_game (
  id integer DEFAULT nextval('my_sequence') PRIMARY KEY
);

Here is the standard SQL syntax:

CREATE TABLE board_game (
  id integer DEFAULT NEXT VALUE FOR my_sequence PRIMARY KEY
);

Not too complicated, is it?

Exercise

We'll build a DEFAULT together with a sequence. First, let's create a sequence. A sequence with the name my_sequence which starts at 10 and adds 2 each time would look like this:

CREATE SEQUENCE my_sequence
START WITH 10
INCREMENT BY 2;

Your job is to create a sequence called id_seq, which starts at 1 and increments by 1.