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.