Page 1 of 1

i need an advice on postgres keys

Posted: Sun Apr 23, 2006 10:20 pm
by pedrotuga
hey all. i usualy use mysql, this time i need to develop a website using postgres.

Usually i set the key to autoincrement in mysql. It appears that postgres doesnt have such a thing.

How do u deal with inserts? do you query the db to get the highest key value and then increment it with php?

what other method can i use?
thx.

Posted: Mon Apr 24, 2006 6:07 am
by timvw
Postgresql does have such a feature, it's called "sequence". (Now look that up in their manual)

Posted: Mon Apr 24, 2006 10:18 am
by pedrotuga
mmm... i see.. its a tricky approach i think. Mysql way is more simplifyed IMO.

I set the keys to serial and it will create an autosequence then i cn use next() corrent() and prev().

It took me quite some time searching and readind docs about this... postgres is not so well documented as mysql at all.

Posted: Mon Apr 24, 2006 12:33 pm
by pickle
This was interesting: http://www.faqts.com/knowledge_base/vie ... 204/fid/16

I've done it before but this looks familiar. I don't use nextval() or anything on my queries. I believe it actually auto-increments without needing to send it a custom function.

Posted: Mon Apr 24, 2006 2:01 pm
by timvw
Anyway, i meant SERIAL ;)

Code: Select all

CREATE TABLE joined (
  joined_id SERIAL, 
  firstname CHAR(20) NOT NULL, 
  lastname CHAR(30) NOT NULL, 
  PRIMARY KEY(joined_id)
)

Posted: Mon Apr 24, 2006 2:43 pm
by Chris Corbyn
timvw wrote:Anyway, i meant SERIAL ;)

Code: Select all

CREATE TABLE joined (
  joined_id SERIAL, 
  firstname CHAR(20) NOT NULL, 
  lastname CHAR(30) NOT NULL, 
  PRIMARY KEY(joined_id)
)
I did wonder ;) I thought I was going mad for a minute earlier.... don't often have to question a remark from your good self :P

Posted: Mon Apr 24, 2006 3:08 pm
by timvw
<off-topic>
You no hablo pgsql ;) The first post was hasty, by heart @ work during lunchbreak.. The second was when i got home and looked in my code..
</off-topic>

I did remember that i searched the web for "postgresql autoincrement sequence" once and that it didn't take long to find what i needed...