sqlite中的递归外键 [英] Recursive Foreign Keys in sqlite

查看:85
本文介绍了sqlite中的递归外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sqlite中是否允许自引用或递归外键?是否有特殊的语法来实现这一目标?到目前为止,我已经尝试了以下无效: FOREIGN KEY(ParentPrimaryKeyId)REFERENCES ThisTableName(PrimaryKeyId)

Are self-referencing or recursive foreign keys allowed in sqlite? Is there a special syntax to accomplish this? So far I've tried the following to no avail: FOREIGN KEY(ParentPrimaryKeyId) REFERENCES ThisTableName(PrimaryKeyId)

作为参考,我的目标是iOS 4中的sqlite 3.6.22。

For reference, I'm targeting sqlite 3.6.22 in iOS 4.

推荐答案

是的,sqlite支持自引用外键,例如:

Yes sqlite supports self-referencing foreign keys, for example:

sqlite> PRAGMA foreign_keys = ON;
sqlite> CREATE TABLE SomeTable (
   ...>     id INTEGER PRIMARY KEY AUTOINCREMENT,
   ...>     parent_id INTEGER, 
   ...>     FOREIGN KEY(parent_id) REFERENCES SomeTable(id));
sqlite> INSERT INTO SomeTable (parent_id) VALUES (234324);
Error: foreign key constraint failed
sqlite> INSERT INTO SomeTable (parent_id) VALUES (NULL);
sqlite> SELECT * FROM SomeTable;
1|
sqlite> INSERT INTO SomeTable (parent_id) VALUES (1);
sqlite> SELECT * FROM SomeTable;
1|
2|1
sqlite> 

这篇关于sqlite中的递归外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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