What is difference between foreign key and reference key?

前端 未结 9 520
無奈伤痛
無奈伤痛 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: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)
    );
    

提交回复
热议问题