Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

serial problem when changing table

Status
Not open for further replies.

MrTom

Programmer
Mar 13, 2001
383
GB
i wanted to change a table by changing a date type to an int. i did this by dropping the table and then recreating it (using phppgadmin).
even tho the original table was dropped it wouldn't allow me to create the serial id again as it said that 'MyTable_id_seq' already existed.
changing the name of 'id' to 'tid' let it recreate the table. where can i delete the original sequence thingy.
i'm still new to postgres so i don't really understand what goes on when i create a serial.

thanks.
 
The serial datatype is really just a workaround for autonumber fields. Creating a field as a serial datatype is really just the same as doing the following:

create sequence tablename_fieldname_seq
alter tablename alter fieldname set default nextval(tablename_fieldname_seq)

A sequence is just a single-column table that stores a number. There are various functions (currval, setval, nextval) that allow you to manipulate the value in the sequence.

Your specific problem is that when you drop the table, it doesn't automatically drop the sequence (use "DROP SEQUENCE"). so either drop the sequence before trying to recreate the serial field, or just create it as an integer field and do the "alter tablename alter fieldname" SQL command above.
 
Hi MrTom,

In addition to what postwick told you, you can delete the sequence in phpPgAdmin by clicking on "All Databases", and then selecting "sequence" from the database that owned the table that was deleted.
Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top