Solved

'auto_increment' in PostgreSQL

Posted on 2003-11-09
3
2,542 Views
Last Modified: 2008-01-16
Hi

I'm working on converting a PHP/MySQL application to a PHP/PostgreSQL app.

I need help with the auto_increment.

I've read through the source code of a few scripts that support MySQL, and PostgreSQL, and noticed that they overcame the problem using Sequences.

The problem is that when I insert a row, from a dump, that has all the values present(including the id), the sequence isn't incremented, so then when I try to insert a row, without stating the id, it trys to use the first of the sequence which is the same as a value already in the db, which results in an error, in havig two values for the same primary key.

Is there any way for me to set a default of the highest number already in the database + 1?

While I'm here, and this isn't strictly part of the above question, but I'd like to know if this will be a problem with other DB systems, such as Oracle, Access, SQL Server, etc

I think this is a fairly simple question. If that proves not to be the case I'll up the point value.
0
Comment
Question by:aolXFT
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
tncbbthositg earned 150 total points
Comment Utility
Alter the column to be the PRIMARY KEY (this is the same as UNIQUE NOT NULL).  

You also do not use auto_increment in PostgreSQL, you use serial, serial4 or serial8.

Another alternative is to define the column as:
 yourcol integer DEFAULT nextval('yourtable_yourcol_seq') NOT NULL


You might also check out:
http://www.cat.org.au/catskills/begin4/tutorial/webdatabases.html
http://www.postgresql.org/docs/pdf/7.3/tutorial-7.3.2-A4.pdf
http://www.postgresql.org/docs/7.3/static/functions-sequence.html
http://www.postgresql.org/docs/7.3/static/sql-createsequence.html
http://www.postgresql.org/docs/7.3/static/datatype.html#DATATYPE-SERIAL

Good luck,
TNC
0
 
LVL 6

Author Comment

by:aolXFT
Comment Utility
I think that Serial is just what I need.

What exactly are serial, serial4 and serial8.

I'll on a very slow internet connection at the minute,

I'll check them out, and get back in about half an hour.
0
 
LVL 6

Author Comment

by:aolXFT
Comment Utility
I really think from looking at the docs that serial is what I need, although I haven't had a chance to test it.

I'll post back here if it's not.

If it's not, then what I do need will surely be very similar, so I'll give you your points now.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now