Let us go through some of the important details related to sequences.
For almost all the tables in relational databases we define primary key constraints.
Primary key is nothing but unique constraint with not null and there can be only one primary key in any given table.
Many times, we might not have appropriate column in the table which can be used as primary key. In those scenarios we will define a column which does not have any business relevant values. This is called as surrogate key.
Relational Database technologies provide sequences to support these surrogate primary keys.
In postgres we can define surrogate primary key for a given table as SERIAL
. Internally it will create a sequence.
We can also pre-create a sequence and use it to populate multiple tables.
Even if we do not specify the column and value as part of the insert statement, a sequence generated number will be populated in that column.
Typically, the sequence generated number will be incremented by 1. We can change it by specifying a constant value using INCREMENT BY
.
Here are some of the properties that can be set for a sequence. Most of them are self explanatory.
STARTÂ WITH
RESTARTÂ WITH
MINVALUE
MAXVALUE
CACHE
We can use functions such as nextval
 and currval
 to explicitly generate sequence numbers and also to get current sequence number in the current session.
We might have to use RESTART WITH
 to reset the sequences after the underlying tables are populated with values in surrogate key.
%%sql
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
user_first_name VARCHAR(30) NOT NULL,
user_last_name VARCHAR(30) NOT NULL,
user_email_id VARCHAR(50) NOT NULL,
user_email_validated BOOLEAN,
user_password VARCHAR(200),
user_role VARCHAR(1),
is_active BOOLEAN,
created_dt DATE DEFAULT CURRENT_DATE
)
sequence_catalog | sequence_schema | sequence_name | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option |
---|---|---|---|---|---|---|---|---|---|---|---|
itversity_retail_db | public | users_user_id_seq | integer | 32 | 2 | 0 | 1 | 1 | 2147483647 | 1 | NO |
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | created_dt |
---|---|---|---|---|---|---|---|---|
2 | Donald | Duck | donald@duck.com | None | None | None | None | 2020-11-23 |
3 | Mickey | Mouse | mickey@mouse.com | None | None | U | True | 2020-11-23 |
%%sql
INSERT INTO users
(user_first_name, user_last_name, user_email_id, user_password, user_role, is_active)
VALUES
('Gordan', 'Bradock', 'gbradock0@barnesandnoble.com', 'h9LAz7p7ub', 'U', true),
('Tobe', 'Lyness', 'tlyness1@paginegialle.it', 'oEofndp', 'U', true),
('Addie', 'Mesias', 'amesias2@twitpic.com', 'ih7Y69u56', 'U', true)
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | created_dt |
---|---|---|---|---|---|---|---|---|
2 | Donald | Duck | donald@duck.com | None | None | None | None | 2020-11-23 |
3 | Mickey | Mouse | mickey@mouse.com | None | None | U | True | 2020-11-23 |
4 | Gordan | Bradock | gbradock0@barnesandnoble.com | None | h9LAz7p7ub | U | True | 2020-11-23 |
5 | Tobe | Lyness | tlyness1@paginegialle.it | None | oEofndp | U | True | 2020-11-23 |
6 | Addie | Mesias | amesias2@twitpic.com | None | ih7Y69u56 | U | True | 2020-11-23 |
%%sql
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
user_first_name VARCHAR(30) NOT NULL,
user_last_name VARCHAR(30) NOT NULL,
user_email_id VARCHAR(50) NOT NULL,
user_email_validated BOOLEAN,
user_password VARCHAR(200),
user_role VARCHAR(1),
is_active BOOLEAN,
created_dt DATE DEFAULT CURRENT_DATE
)
%%sql
INSERT INTO users
(user_id, user_first_name, user_last_name, user_email_id, user_password, user_role, is_active)
VALUES
(3, 'Gordan', 'Bradock', 'gbradock0@barnesandnoble.com', 'h9LAz7p7ub', 'U', true),
(4, 'Tobe', 'Lyness', 'tlyness1@paginegialle.it', 'oEofndp', 'U', true),
(5, 'Addie', 'Mesias', 'amesias2@twitpic.com', 'ih7Y69u56', 'U', true)
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | created_dt |
---|---|---|---|---|---|---|---|---|
1 | Donald | Duck | donald@duck.com | None | None | None | None | 2020-11-23 |
2 | Mickey | Mouse | mickey@mouse.com | None | None | U | True | 2020-11-23 |
3 | Gordan | Bradock | gbradock0@barnesandnoble.com | None | h9LAz7p7ub | U | True | 2020-11-23 |
4 | Tobe | Lyness | tlyness1@paginegialle.it | None | oEofndp | U | True | 2020-11-23 |
5 | Addie | Mesias | amesias2@twitpic.com | None | ih7Y69u56 | U | True | 2020-11-23 |
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | created_dt |
---|---|---|---|---|---|---|---|---|
1 | Donald | Duck | donald@duck.com | None | None | None | None | 2020-11-23 |
2 | Mickey | Mouse | mickey@mouse.com | None | None | U | True | 2020-11-23 |
3 | Gordan | Bradock | gbradock0@barnesandnoble.com | None | h9LAz7p7ub | U | True | 2020-11-23 |
4 | Tobe | Lyness | tlyness1@paginegialle.it | None | oEofndp | U | True | 2020-11-23 |
5 | Addie | Mesias | amesias2@twitpic.com | None | ih7Y69u56 | U | True | 2020-11-23 |
6 | Scott | Tiger | scott@tiger.com | None | None | None | None | 2020-11-23 |
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | created_dt |
---|---|---|---|---|---|---|---|---|
1 | Donald | Duck | donald@duck.com | None | None | None | None | 2020-11-23 |
2 | Mickey | Mouse | mickey@mouse.com | None | None | U | True | 2020-11-23 |
3 | Gordan | Bradock | gbradock0@barnesandnoble.com | None | h9LAz7p7ub | U | True | 2020-11-23 |
4 | Tobe | Lyness | tlyness1@paginegialle.it | None | oEofndp | U | True | 2020-11-23 |
5 | Addie | Mesias | amesias2@twitpic.com | None | ih7Y69u56 | U | True | 2020-11-23 |
6 | Scott | Tiger | scott@tiger.com | None | None | None | None | 2020-11-23 |
user_id | user_first_name | user_last_name | user_email_id | user_email_validated | user_password | user_role | is_active | created_dt |
---|---|---|---|---|---|---|---|---|
1 | Donald | Duck | donald@duck.com | None | None | None | None | 2020-11-23 |
2 | Mickey | Mouse | mickey@mouse.com | None | None | U | True | 2020-11-23 |
3 | Gordan | Bradock | gbradock0@barnesandnoble.com | None | h9LAz7p7ub | U | True | 2020-11-23 |
4 | Tobe | Lyness | tlyness1@paginegialle.it | None | oEofndp | U | True | 2020-11-23 |
5 | Addie | Mesias | amesias2@twitpic.com | None | ih7Y69u56 | U | True | 2020-11-23 |
6 | Scott | Tiger | scott@tiger.com | None | None | None | None | 2020-11-23 |
7 | Matt | Clarke | matt@clarke.com | None | None | None | None | 2020-11-23 |