使用或不使用外键的引用之间有什么区别 [英] What is the difference between REFERENCES with, or without a FOREIGN KEY

查看:146
本文介绍了使用或不使用外键的引用之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于SQLite,带或不带外键的引用之间有什么区别?

这有什么区别

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 ) 
);

和这个:

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        
);

我发现了另一个问题: 在带有和不带有FOREIGN KEY的情况下使用REFERENCES之间的区别吗?

我阅读了文档,但对于我来说却不清楚./p>

准确地说,我在Android上使用SQLite(因此有标记).我不知道这有什么区别,但是如果它有它自己的怪癖,那对我来说非常有用.

FOREIGN KEY语法比在列定义中内联定义它更灵活(例如,它允许您定义一个复合外键,其中组合引用列中应存在两个或更多字段.

在您的情况下,两个DDL语句之间没有区别.可以说外键的内联定义只不过是语法加糖.

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.

解决方案

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.

这篇关于使用或不使用外键的引用之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆