Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay for the foreign key field to have a different name than the field it references in the other table?

Is it okay for the foreign key field to have a different name than the field it references in the other table? If yes , how can it refer to the other table ?

like image 343
siddharth Avatar asked Oct 19 '25 02:10

siddharth


1 Answers

Yes it is ok. Read sqlite foreign key document. It gives similar example. As you can see, column names are not same, but foreign key added nonetheless.

CREATE TABLE artist(
  artistid    INTEGER PRIMARY KEY, 
  artistname  TEXT
);
CREATE TABLE track(
  trackid     INTEGER, 
  trackname   TEXT, 
  trackartist INTEGER,
  FOREIGN KEY(trackartist) REFERENCES artist(artistid)
);
like image 82
Atilla Ozgur Avatar answered Oct 22 '25 05:10

Atilla Ozgur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!