What is difference between foreign key and reference key?

前端 未结 9 522
無奈伤痛
無奈伤痛 2020-12-14 06:13

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 tr

相关标签:
9条回答
  • 2020-12-14 06:47

    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.

    0 讨论(0)
  • 2020-12-14 06:49

    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)
    );
    
    0 讨论(0)
  • 2020-12-14 06:49

    The Reference Key is the primary key that is referenced in the other table. On the other hand, Foreign Key is how you link the second table to the primary tables Primary Key (or Reference Key).

    0 讨论(0)
提交回复
热议问题