DBAsupport.com Forums - Powered by vBulletin
Results 1 to 5 of 5

Thread: alter table

  1. #1
    Join Date
    Sep 2004
    Posts
    51

    alter table

    I'm having trouble figuring how to add to a table a column that references another table.

    this works:
    alter table timesheets add timesheets_pay_period_id NUMBER


    this doesn't:
    alter table timesheets add timesheets_pay_period_id NUMBER REFERENCES timesheets_pay_periods(timesheets_pay_period_id)

    I've moved the NUMBER to the end, but it didn't work either.

    thanks in advance

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    ...a column that references another table

    A foreign key perhaps?

    If this is the case,
    1- Add your column.
    2- Create your FK constraint.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Sep 2004
    Posts
    51
    I'm trying to do it using these directions:
    http://www.infogoal.com/sql/sql-add-foreignkey.htm

    I'm not making much progress as I've redone the line several different ways with no success.

    alter table timesheets add timesheets_pay_period_id foreign key (timesheets_pay_period_id) REFERENCES timesheets_pay_periods(timesheets_pay_period_id)

    No real idea how this should look. Am I supposed to create a named foreign key then add it? I've not had to do that before.

    At this point, I might was well create another identical table, migrate the data, drop the old table; recreate old table with this one alteration, migrate the data back and delete the tmp table.

    daniel

  4. #4
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Code:
    -- add new column
    alter table timesheets add(timesheets_pay_period_id number)
    ;
    -- create FK constraint
    alter table timesheets 
       add constraint fk_timesheets_pay_period_id 
       foreign key (timesheets_pay_period_id) 
       references timesheets_pay_periods(timesheets_pay_period_id)
    ;
    Could you please exec code above and post session log?
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  5. #5
    Join Date
    Sep 2004
    Posts
    51
    That worked great - thank you.

Posting Permissions

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


Click Here to Expand Forum to Full Width