What is difference between foreign key and referen

2019-02-02 21:48发布

I am very confused about those two terms. Are they the same or different?

Some books and people say they are the same and others say they are different.

I tried but couldn't find a conclusive answer.

8条回答
劳资没心,怎么记你
2楼-- · 2019-02-02 22:27

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.

查看更多
地球回转人心会变
3楼-- · 2019-02-02 22:30

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

查看更多
我只想做你的唯一
4楼-- · 2019-02-02 22:34

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.

查看更多
劳资没心,怎么记你
5楼-- · 2019-02-02 22:36

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)
);
查看更多
等我变得足够好
6楼-- · 2019-02-02 22:40

"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?

查看更多
Ridiculous、
7楼-- · 2019-02-02 22:42

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.

查看更多
登录 后发表回答