本文介绍了在同一个表SQLite上创建外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用SQLite(根据我的研究需要),并且我讨论了SQLite的一些限制,我想知道:SQLite不能在同一个表上创建外键?例如。这是我的代码:

  CREATE TABLE类别

名称varchar(20),
parent_category varchar(20)NULL,
PRIMARY KEY(name),
FOREIGN KEY parent_category_fk(parent_category)参考类别(名称)

/ pre>

但是当我尝试在SQLiteStudio中执行SQL时,它给了我一个外键错误。



解决方案

问题在于您的FK子句的语法错误。它应该是:
$ b $ pre $ FOREIGN KEY(parent_category)参考类别(名称)

如果您想命名FK约束,您可以使用 CONSTRAINT 关键字的前缀,像这样:
$ b $ pre $ CONSTRAINT parent_category_fk FOREIGN KEY(parent_category)参考类别(名称)


Recently I started using SQLite (as required for my study) and I came accross a couple of restrictions of SQLite and I was wondering: can't SQLite create foreign keys on the same table? E.g. this is my code:

CREATE TABLE Categories
(
    name varchar(20),
    parent_category varchar(20) NULL,
    PRIMARY KEY(name),
    FOREIGN KEY parent_category_fk(parent_category) REFERENCES Categories(name)
)

But it gives me an error for the foreign key when I try to execute the SQL in SQLiteStudio.

Does anyone know why this isn't working?

解决方案

The problem is that you have the wrong syntax for the FK clause. It should be:

FOREIGN KEY (parent_category) REFERENCES Categories(name)

If you want to name the FK constraint, you do that with a prefix of the CONSTRAINT keyword, like this:

CONSTRAINT parent_category_fk FOREIGN KEY (parent_category) REFERENCES Categories(name)

这篇关于在同一个表SQLite上创建外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:01
查看更多