What is the difference between REFERENCES with, or without a FOREIGN KEY

南楼画角 提交于 2019-12-23 10:57:31

问题


In regards to SQLite, What is the difference between REFERENCES with, or without a FOREIGN KEY?

What is the difference between this

CREATE TABLE players_set ( 
    _id            INTEGER PRIMARY KEY AUTOINCREMENT
                           NOT NULL,
    player_id      INTEGER REFERENCES players ( _id ) ON DELETE CASCADE,
    players_set_id INTEGER REFERENCES players_set_names ( _id ) 
);

and this:

CREATE TABLE players_set ( 
    _id            INTEGER PRIMARY KEY AUTOINCREMENT
                           NOT NULL,
    player_id INTEGER,
    players_set_id INTEGER REFERENCES players_set_names ( _id ),
    FOREIGN KEY (player_id) REFERENCES players ( _id ) ON DELETE CASCADE        
);

I found this other question: Difference between using REFERENCES with and without FOREIGN KEY?

I read the documentation, it didn't make it clear for me though.

To be precise, I'm using SQLite on Android (hence the tag). I don't know if that makes any difference, but if it has its own quirks, it would be very useful for me to find out about.


回答1:


The FOREIGN KEY syntax is more flexible than defining it inline in the column definition (e.g., it allows you to define a composite foreign key, where the combination of two or more fields should exist in the referencing columns).

In your case, there is no difference between the two DDL statements. One could say that the inline definition of foreign keys is nothing more than syntactic sugaring.



来源:https://stackoverflow.com/questions/21647710/what-is-the-difference-between-references-with-or-without-a-foreign-key

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