Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2011
    Posts
    14

    Unanswered: can foreign key and its reference name be same

    Hi All,

    Can name of foreign key and it reference be same.
    For example in reference_TABLE table given below instead of name 'ref_name' shall is use it with the same
    name as in sample_ONE i.e SampleName.
    i.e can i have it as given in "Section: Can i have " given below.
    Please note that 'sample_ONE is same in both sections only reference_TABLE has changed.


    Section: Actual
    --------------
    create table sample_ONE(
    SampleName varchar(100),
    PRIMARY KEY (Name),
    );

    create table reference_TABLE(
    ref_name varchar(100),
    id int(11) unsigned,
    PRIMARY KEY (id),
    FOREIGN KEY (ref_name) REFERENCES sample_ONE(SampleName),
    );
    ----------------


    Section: Can i have
    --------------
    create table sample_ONE(
    SampleName varchar(100),
    PRIMARY KEY (Name),
    );

    create table reference_TABLE(
    SampleName varchar(100),
    id int(11) unsigned,
    PRIMARY KEY (id),
    FOREIGN KEY (SampleName) REFERENCES sample_ONE(SampleName),
    );
    ____________________

    Thanks,

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by newbielgn View Post
    an name of foreign key and it reference be same.
    what happened when you tested it?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jul 2011
    Posts
    14
    Apologies for some syntax errors in my initial post.Please check commands below in which the syntax errors were corrected.

    Both examples given below worked without any error when executed

    Scenario-1:
    ----------------
    create table sample_ONE (
    SampleName varchar(100),
    PRIMARY KEY (SampleName)
    );

    create table reference_TABLE (
    ref_name varchar(100),
    id int(11) unsigned,
    PRIMARY KEY (id),
    FOREIGN KEY (ref_name) REFERENCES sample_ONE (SampleName)
    );
    ------------------


    Scenario-2:
    ----------------
    create table sample_ONE (
    SampleName varchar(100),
    PRIMARY KEY (SampleName)
    );

    create table reference_TABLE(
    SampleName varchar(100),
    id int(11) unsigned,
    PRIMARY KEY (id),
    FOREIGN KEY (SampleName) REFERENCES sample_ONE(SampleName)
    );
    ----------------
    Last edited by newbielgn; 08-08-11 at 10:19.

  4. #4
    Join Date
    Mar 2004
    Posts
    480
    Quote Originally Posted by newbielgn View Post
    Both examples given below worked without any error when executed
    Sounds like it works then.

  5. #5
    Join Date
    Jul 2011
    Posts
    14
    yes, it works.

Posting Permissions

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