FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Made Mistake with SQL table, need help

   
Author Topic: Made Mistake with SQL table, need help
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
Basically I have a table of user accounts, my problem is I forgot to put a column with ID numbers at the front, without having to redo everything is there an easy painless way of inserting a column at the front?
IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
First, columns in SQL databases have no necessary order. They're usually implemented to appear in a consistent order, but that's for the user's convenience. There is no requirement.

So, you don't need to add a column at the 'front', just add a column.

Is there any data in the table, yet? That will affect the exact procedure you'll want to use. As will the RDBMS you are using. But basically, you're going to want to use the ADD COLUMN variant of ALTER TABLE.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I'm using postgre, but eh, I wanted ID's at the front. Right now my table looks like this:

code:
    username    |            email             | password  | public_name |  location  |      homepage      |  interests  | icq_num | msn_messenger | occupation 
----------------+------------------------------+-----------+-------------+------------+--------------------+-------------+---------+---------------+------------
Raenir | raenir.tir@gmail.com | eve | | | | | | |
Drizzt | drizzt.tir@gmail.com | 2 | | | | | | |
Nebuchadrezzar | Nebuchadrezzar.tir@gmail.com | 3 | | | | | | |
God | God.tir@gmail.com | 4 | | | | | | |
Petey | Petey.tir@gmail.com | 5 | | | | | | |
Blayne | Blayne.tir@gmail.com | 6 | | | | | | |
Blayne1 | Blayne1.tir@gmail.com | 0 | | | | | | |
Ryuujin | ryuujin.dragongod@gmail.com | dragongod | | | | | | |
Artemis | artemis@fowl.com | dragon | Fowl | | | | | |
Ashkore | ashkore@stormrune.com | 123456 | Ashkore | Tagnik'Zur | www.tagnik-zur.com | Sword Saint | Weiqi | n/a | n/a
Danomite | danomite2k4@gmail.com | 123456 | Daniel | Quebec | novasearch | Student | Games | |
(11 rows)

Here she blows.
IP: Logged | Report this post to a Moderator
TomDavidson
Member
Member # 124

 - posted      Profile for TomDavidson   Email TomDavidson         Edit/Delete Post 
Couldn't you just specify the order of the columns in your SELECT statement?
Posts: 37449 | Registered: May 1999  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I like my tables aesthetically pleasing with minimal effort.
IP: Logged | Report this post to a Moderator
Bokonon
Member
Member # 480

 - posted      Profile for Bokonon           Edit/Delete Post 
Yes, yes he could.

EDIT: At this point, Blayne, you may just want to settle on "functional capable".

-Bok

Posts: 7021 | Registered: Nov 1999  |  IP: Logged | Report this post to a Moderator
scifibum
Member
Member # 7625

 - posted      Profile for scifibum   Email scifibum         Edit/Delete Post 
If you just HAVE to make this table over again with the Id column at the front, I think you should probably try the "Create table as" statement. In the example below I've assumed you want ID numbers generated automatically and you have already created a sequence called "serial" to generate the values:

create table new_tablename AS
select nextval('serial') AS ID, orig_table.*
from orig_table;
alter table new_tablename alter ID SET DEFAULT nextval('serial');

I don't think this will inherit any of the constraints on the original table so you'd have to add those in addition to running SQL like the above.

Posts: 4287 | Registered: Mar 2005  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
so far my sequence is generated in my perl script.
IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
quote:
so far my sequence is generated in my perl script.
*shudder* To do this, you either have to lock the table across db calls - with all the complications that entails - or will have duplicate sequences eventually. Assuming you add proper constraints, the duplicates won't make their way into your table. But you're still asking for trouble.

Postgres has a feature to make assigning sequences to PK fields effortless. Use the DB, young padawan. Don't fight it.

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
scifibum
Member
Member # 7625

 - posted      Profile for scifibum   Email scifibum         Edit/Delete Post 
I tend to write unnecessary code too.
Posts: 4287 | Registered: Mar 2005  |  IP: Logged | Report this post to a Moderator
HollowEarth
Member
Member # 2586

 - posted      Profile for HollowEarth   Email HollowEarth         Edit/Delete Post 
Uh with only eleven rows, just dump the table and recreate and insert the rows? You could do this by hand even.
Posts: 1621 | Registered: Oct 2001  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2