Results 1 to 5 of 5

Thread: foreign key

  1. #1
    Join Date
    Oct 2003
    Posts
    3

    Question foreign key

    i have already created the table, and named the primary key for it. how would i go back and add a foreign key.
    thanks
    magcutie08@yahoo.com
    Last edited by magical01; 10-26-2003 at 12:01 PM.

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    create table x (id int constraint x_pk primary key nonclustered, name char(10), deptid int)
    create table y (id int constraint y_pk primary key nonclustered, deptname char(10))

    alter table x add constraint x_y_fk foreign key (deptid) references y([id])

  3. #3
    Join Date
    Oct 2003
    Posts
    3
    where did you get the x_y_fk foreign key part from is it just the names of the tables?

  4. #4
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    it is the name of the constraint. if you are not giving system will create the name automatically

  5. #5
    Join Date
    Oct 2003
    Posts
    3

    Smile

    got it to work thanks

Posting Permissions

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