简体   繁体   English

使用 REFERENCES 时,可以在 PostgreSQL 中省略 FOREIGN KEY 吗?

[英]Can FOREIGN KEY be omitted in PostgreSQL when using REFERENCES?

I'm wondering if there's any (maybe subtle) difference between these two SQL statements:我想知道这两个 SQL 语句之间是否有任何(可能是细微的)区别:

CREATE TABLE profiles (
    profile_id SERIAL PRIMARY KEY NOT NULL,
    bio TEXT,
    user_id INTEGER NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users(user_id)
);

and

CREATE TABLE profiles (
    profile_id SERIAL PRIMARY KEY NOT NULL,
    bio TEXT,
    user_id INTEGER NOT NULL REFERENCES users(user_id) 
);

I've noticed that when I create a table in Postico with the first notation, but look at the DDL of the created profiles table later, the FOREIGN KEY is removed and I end up with the shorter second notation.我注意到当我用第一个符号在 Postico 中创建一个表时,但稍后查看创建的profiles表的 DDL, FOREIGN KEY被删除,我最终得到较短的第二个符号。

Create table with FOREIGN KEY :使用FOREIGN KEY创建表

DDL view doesn't show FOREIGN KEY : DDL 视图不显示FOREIGN KEY

So, I'm wondering (and seeking confirmation) that the two statements are in fact 100% equivalent or if there are some subtle differences in what they do to the DB.所以,我想知道(并寻求确认)这两个语句实际上是 100% 等效的,或者它们对数据库的作用是否存在一些细微的差异。

Any pointer to official resources (and maybe also how that differs from MySQL) would be appreciated.任何指向官方资源的指针(也许还有它与 MySQL 的不同之处)将不胜感激。

The two samples you show do the same thing, just with a different syntax.您展示的两个示例执行相同的操作,只是使用了不同的语法。

The first method is called table constraint , the second column constraint , but the latter name is somewhat misleading because the constraint is on the table as well.第一种方法称为table 约束,第二个column constraint ,但后一个名称有些误导,因为约束也在表上。

The main difference is that the column constraint syntax is shorter, but cannot be used for all constraints: if you have for example a primary key that contains two columns, you have to write it in the table constraint syntax.主要区别在于列约束语法较短,但不能用于所有约束:例如,如果您有一个包含两列的主键,则必须将其写入表约束语法中。

In PostgreSQL, you define a foreign key through a foreign key constraint.在 PostgreSQL 中,您通过外键约束定义外键。 A foreign key constraint indicates that values in a column or a group of columns in the child table match with the values in a column or a group of columns of the parent table.外键约束表示子表中的一列或一组列中的值与父表中的一列或一组列中的值匹配。 We say that a foreign key constraint maintains referential integrity between child and parent tables.我们说外键约束维护子表和父表之间的参照完整性。

This may explain to you better or you can read about Foreign Keys documentation . 可能会更好地向您解释,或者您可以阅读有关外键的文档

DDL view doesn't show FOREIGN KEY DDL 视图不显示外键

DDL view created by unknown third-party tool in not an argument.由未知第三方工具创建的 DDL 视图不是参数。

See fiddle .小提琴 Foreign key exists in both cases.两种情况下都存在外键。 Moreover, I do not see the result difference for both DDL queries.此外,我没有看到两个 DDL 查询的结果差异。

PS.附注。 As a recommendation - always specify the constraint name explicitly.作为建议 - 始终明确指定约束名称。 What if you need to delete it?如果需要删除怎么办? It is problematic without the constraint name...没有约束名称是有问题的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM