Let us explore Serial Datatype of PostgreSQL

Niladri.Biswas
Posted by in PostgreSQL category on for Beginner level | Points: 250 | Views : 2546 red flag

In this article, we will explore Serial Datatype of PostgreSQL

Introduction

Identity column helps us to generate sequential numbers.In PostgreSQL we can use the Serial datatype for generating the identity column.This article will focus on that.

Straight to Experiment

CREATE TABLE tblIndentity
(
  EmpID Serial NOT NULL,
  EmpName Character Varying(20)
);

NOTICE:  CREATE TABLE will create implicit sequence "tblindentity_empid_seq" for serial column "tblindentity.empid"

Query returned successfully with no result in 12 ms.

Polulate some data into the table

Insert Into tblIndentity(EmpName) Values('Name1');
Insert Into tblIndentity(EmpName) Values('Name2');
Insert Into tblIndentity(EmpName) Values('Name3');
Insert Into tblIndentity(EmpName) Values('Name4');
Insert Into tblIndentity(EmpName) Values('Name5');

Project the records

Select * 
From tblIndentity

--Result
Empid	Empname
-----	--------
1	Name1
2	Name2
3	Name3
4	Name4
5	Name5

In Sql Server we use the Identity for doing so.

N.B.~ From Denali CTP1, we can even use Sequence for generating identity values

Conclusion

So in this article, we have seen as how Serial datatype helps in generating sequential numbers in PostgreSQL.Hope this will be helpful.Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)