If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > can foreign key and its reference name be same

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-08-11, 08:26
newbielgn newbielgn is offline
Registered User
 
Join Date: Jul 2011
Posts: 14
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,
Reply With Quote
  #2 (permalink)  
Old 08-08-11, 08:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by newbielgn View Post
an name of foreign key and it reference be same.
what happened when you tested it?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-08-11, 09:16
newbielgn newbielgn is offline
Registered User
 
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 09:19.
Reply With Quote
  #4 (permalink)  
Old 08-08-11, 10:58
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
Quote:
Originally Posted by newbielgn View Post
Both examples given below worked without any error when executed
Sounds like it works then.
Reply With Quote
  #5 (permalink)  
Old 08-08-11, 11:19
newbielgn newbielgn is offline
Registered User
 
Join Date: Jul 2011
Posts: 14
yes, it works.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On