Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL Serial Type

In PostgreSQL, a database table can be created by defining column datatype as SERIAL. It is used to define a column of the table as an auto-increment column.

Syntax
CREATE TABLE <table_name> (
    <column_name> SERIAL
);

When you assign a SERIAL data type to any column, PostgreSQL will perform following

  • Create a sequence object and assign the next value generated by the sequence as a default value to a column. The default name of sequence would be <table_name>_<column_name>_seq.
  • Add NOT NULL constraint to the SERIAL type column of the table. The sequence will always generate an integer value and assign it to the column whenever data is inserted into the table, hence column will always be NOT NULL.
  • The column will be the owner of the sequence. If the column is dropped or the table is deleted, the sequence will be automatically dropped.
NameStorageByte range
SMALLSERIAL2 bytes1 to 32,767
SERIAL4 bytes1 to 2,147,483,647
BIGSERIAL8 bytes1 to 9,223,372,036,854,775,807

Let's create the CARS table with a primary key column of SERIAL type.

Example: Create Serial Type Column
CREATE TABLE CARS(
   id SERIAL PRIMARY KEY,
   brand VARCHAR NOT NULL
);

When you execute the above statement, it will create index cars_id_seq as bellow

Now let's try to insert data into the CARS table and see how the values of the id column are inserted. To assign sequence generated value to the id column of the CARS table when you insert a row into the table, either you ignore that column or use the DEFAULT keyword for that column, as shown below.

Example: Insert Values for Serial Column
INSERT INTO CARS(brand)
VALUES('Fiat');

INSERT INTO CARS(id, brand)
VALUES(DEFAULT, 'Honda');

As you can see, two rows were inserted into the table with the id column populated as 1 and 2.

You can check the current value of the sequence using the sequence manipulation function currval() function.

The above statement returns 2 as the current value of the sequence as the last id value is 2.

Multiple rows can be inserted into the table, in that case, it will keep on assigning the next value of the sequence to the id column sequentially.

Example: Insert Serial Values
INSERT INTO CARS(brand)
VALUES 
	('Hyundai'),
	('Jeep'),
	('Tesla');

Thus, you can use sequence as the SERIAL type with the table.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.