Solved

'auto_increment' in PostgreSQL

Posted on 2003-11-09
3
2,595 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
tncbbthositg earned 150 total points
ID: 9715095
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
ID: 9717168
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
ID: 9717509
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 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