Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2005
    Posts
    4

    Unanswered: Add multiple columns with ALTER TABLE

    I have created a table with one column. I want to add columns to the table so I have tried:
    ALTER TABLE tablename ADD COLUMN column1 real, column2 real

    I get a syntax error. I can add one column without problems. Can I use the ALTER TABLE command to add two or more columns?

    Thanks for the help.

  2. #2
    Join Date
    Feb 2005
    Location
    Colorado Springs
    Posts
    222
    You're right. Add column allows one column entry, including the field name, type, and any constraints that may be applied to it.

  3. #3
    Join Date
    Nov 2003
    Location
    Canada
    Posts
    15
    Quote Originally Posted by ByteRyder52
    You're right. Add column allows one column entry, including the field name, type, and any constraints that may be applied to it.
    That is incorrect, byterider. You can add multiple columns/constraints/etc with a single alter statement.

    For example, the following code will create a table with 1 column and then add 2 more columns using a single alter table.
    Code:
    CREATE TABLE test( col1 int );
    ALTER TABLE test ADD COLUMN col2 int, ADD COLUMN col3 int;
    gjc, you just need to put 'ADD COLUMN' for each separate column you want to add.
    See the Postgres docs! (rtfm)

  4. #4
    Join Date
    Feb 2005
    Location
    Colorado Springs
    Posts
    222
    Slush,
    You're right, my bad. The documentation, which I read before posting, states ADD COLUMN allows "a column" to be added, not mentioning multiple columns, nor does it provide syntax for adding multiple columns. I ran a couple of tests before my prior posting and did not hit on the correct syntax, so was unable to do the multiple column add. Given my read of the documentation and the testing I did, I posted my prior response.

  5. #5
    Join Date
    Oct 2005
    Posts
    4
    Thanks for all of the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •