serial data type usage

From: "EXT-Rothermel, Peter M" <Peter(dot)M(dot)Rothermel(at)boeing(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: serial data type usage
Date: 2008-11-06 19:44:38
Message-ID: 8D9E4E8445BD14478121CC9B027B518AF59099@XCH-NW-11V2.nw.nos.boeing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a table where I would like the primary key to be generated during
the insert.

Here is a simplified example:

CREATE TABLE employee_type
{
tname varchar(10) PRIMARY KEY,
id_prefix char(1) ;
...
}

tname | id_prefix
--------------+----------
worker | W
manager | M
executive | E

CREATE TABLE employee {
id varchar(10) PRIMARY KEY,
type varchar(10) REFERENCES employee_type
...
}

When an employee of type 'worker' is inserted the id generated will have
a prefix "W" followed by a 6-digit number. W000001, W000002 ..
When the employee type is 'manager' the employee id is M000001, M000002
...
When the employee type is 'executive' the employee id is E000001,
E000002 ...

The sequences for each employee type are separate.

CREATE SEQUENCE worker_seqnum MINVALUE 1 MAXVALUE 999999 ;
CREATE SEQUENCE manger_seqnum MINVALUE 1 MAXVALUE 999999 ;
CREATE SEQUENCE executive_seqnum MINVALUE 1 MAXVALUE 999999 ;

I have thought about using the serial data type for the employee.id but
I also want to automate the prepending of the { W, M, E } prefix.

Any suggestions?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2008-11-06 19:57:08 Re: Equivalent for AUTOINCREMENT?
Previous Message Michelle Konzack 2008-11-06 19:42:49 Re: Equivalent for AUTOINCREMENT?