HardBirch

How to Create/Alter Table which with Foreign Key For Android

时间:13-02-19 栏目:android学习笔记 作者:魔豆先生 评论:0 点击: 4,326 次

Q: I have the following table,  How do I add a foreign key constraint on parent_id

CREATE TABLE child( id INTEGER PRIMARY KEY, parent_id INTEGER, description TEXT);

 

A:Although the SQL-92 syntax to add a foreign key to your table would be as follows:

ALTER TABLE child ADD CONSTRAINT fk_child_parent FOREIGN KEY (parent_id) REFERENCES parent(id);

SQLite doesn't support the ADD CONSTRAINT variant of the ALTER TABLE command (sqlite.org: SQL Features That SQLite Does Not Implement).

Therefore, the only way to add a foreign key in sqlite 3.6.1 is during CREATE TABLE as follows:

CREATE TABLE child ( id INTEGER PRIMARY KEY, parent_id INTEGER, description TEXT, FOREIGN KEY (parent_id) REFERENCES parent(id) );

Unfortunately you will have to save the existing data to a temporary table, drop the old table, create the new table with the FK constraint, then copy the data back in from the temporary table. (sqlite.org - FAQ: Q11)

 

 

声明: 本文由( 魔豆先生 )原创编译,转载请保留链接: How to Create/Alter Table which with Foreign Key For Android

How to Create/Alter Table which with Foreign Key For Android:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐