serial data type usage

Поиск
Список
Период
Сортировка
От EXT-Rothermel, Peter M
Тема serial data type usage
Дата
Msg-id 8D9E4E8445BD14478121CC9B027B518AF59099@XCH-NW-11V2.nw.nos.boeing.com
обсуждение исходный текст
Ответы Re: serial data type usage  (Alan Hodgson <ahodgson@simkin.ca>)
Re: serial data type usage  (Berend Tober <btober@ct.metrocast.net>)
Список 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?





В списке pgsql-general по дате отправления:

Предыдущее
От: "Scott Marlowe"
Дата:
Сообщение: Re: Equivalent for AUTOINCREMENT?
Следующее
От: Jonathan Guthrie
Дата:
Сообщение: Re: I'm no longer puzzled by a foreign key constraint problem