Unix Technical Forum

Squences with letters aswell as numbers

This is a discussion on Squences with letters aswell as numbers within the pgsql Novice forums, part of the PostgreSQL category; --> At the moment I'm using this kind of sequence to provide a primary key for some of my tables ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 10:38 PM
NubeY
 
Posts: n/a
Default Squences with letters aswell as numbers

At the moment I'm using this kind of sequence to provide a primary key
for some of my tables

create sequence group_seq;
select setval('group_seq', (select max(group_ID) from groups));

However I'd like to create a sequence that has this kind of output

g1
g2
g3
g4
g5

where g doesn't change

and

u1
u2
u3

where u doesn't change

is there are simple way of doing this?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 10:38 PM
Michael Fuhr
 
Posts: n/a
Default Re: Squences with letters aswell as numbers

On Tue, Feb 28, 2006 at 08:46:45AM -0800, NubeY wrote:
> create sequence group_seq;
> select setval('group_seq', (select max(group_ID) from groups));
>
> However I'd like to create a sequence that has this kind of output
>
> g1
> g2
> g3
> g4
> g5
>
> where g doesn't change


Any reason you can't append the sequence value to a string as in
the following example?

CREATE SEQUENCE foo_seq;

CREATE TABLE foo (
id text PRIMARY KEY DEFAULT 'f' || nextval('foo_seq'),
val text NOT NULL
);

INSERT INTO foo (val) VALUES ('a');
INSERT INTO foo (val) VALUES ('b');
INSERT INTO foo (val) VALUES ('c');

SELECT * FROM foo;
id | val
----+-----
f1 | a
f2 | b
f3 | c
(3 rows)

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 10:39 PM
NubeY
 
Posts: n/a
Default Re: Squences with letters aswell as numbers

Thankyou you're a lifesaver.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:14 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com