What is difference between foreign key and reference key?

对着背影说爱祢 提交于 2019-11-30 02:48:47

I am supposing that you are talking about using the REFERENCES where the FOREIGN KEY keyword is not used, eg.

author_id INTEGER REFERENCES author(id)

... instead of ...

author_id INTEGER,
FOREIGN KEY(author_id) REFERENCES author(id)

The answer is, that it is simply shorthand syntax for the same thing. The main concern when altering between the two should be readability.

"Reference key" isn't a normal technical term in relational modeling or in SQL implementation in US English.

A foreign key "references" a key in some other table; could that be where the confusion comes from?

You don't really call something a reference key... They are the same thing... you might see the word references used for example in sqlite: you might use syntax like this to start a db of authors and books. This lets you show that one author can have many books. This tells the db that the books.author_id (defined a couple of lines up) references author.id

CREATE TABLE 'author' (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    firstname varchar(255)
    lastname varchar(255)
);

CREATE TABLE 'books' (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    author_id INTEGER,
    title varchar(255),
    published date,
    FOREIGN KEY(author_id) REFERENCES author(id)
);

A foreign key must refer to a primary key. When using REFERENCES constraint simply, then it isn't necessary that the referenced key be a primary key.

Perhaps you are using the term "reference key" somewhat loosely?

A foreign key value in one row is said to "reference" the row that contains the corresponding key value. Note the word "reference" in the prior sentence is a verb, so we may say we have a referencing foreign key value and a referenced key value.

Although it is the key values, rather than the table key constraint, that is being referenced, I suppose loosely speaking we could say "referenced key" to mean the rows that comprise the values that may potentially be referenced. I then see how "referenced key" could become "referenced key" but not belie its origin.

A foreign key "references" a key in some other table. That key in some other table is called Referenced key. You'll probably hear a lot about this if you're using Graphic feature on phpmyadmin.

There are 2 ways to declare a foreign key(s):

  1. if the foreign key is a SINGLE attribute:
    REFERENCES ()

  2. if foreign keys are a LIST of attributes

FOREIGN KEY () REFERENCES

The only and most important difference between the two keywords 'FOREIGN KEY" and "REFERENCES" keywords is though both of them make the data to be child data of the parent table, the "FOREIGN KEY" is used to create a table level constraint whereas REFERENCES keyword can be used to create column level constraint only. Column level constraints can be created only while creating the table only. But table level constraints can be added using ALTER TABLE command.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!