Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
UNIQUE
NOT NULL
CHECK
DEFAULT
30. DEFAULT - Sequences
Summary

Instruction

Alright, excellent. Now we have a small surprise for you. Do you remember sequences from the previous part of our course? We promised we would tell you how to link them to columns - and here we go! We use the DEFAULT constraint:

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

Not very complicated, right?

Exercise

Ok, first of all we need a sequence. Just to remind you: 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;

Now, create a sequence called id_seq which starts at 1 and increments by 1.

Stuck? Here's a hint!

Type

CREATE SEQUENCE id_seq START WITH 1 INCREMENT BY 1;