且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sqlite中使用外键?

更新时间:2023-01-29 17:08:35

正如selvin建议使用删除级联



http:// www .sqlite.org / foreignkeys.html



Table1

  CREATE TABLE table1(
id PRIMARY KEY
,entry1 text,entry2 text
);

然后

 插入到table1值(1,aaaa,aaaaa); 
插入到table1值(2,bbbb,bbbbb);

Table2

  CREATE TABLE table2(
id int引用table1(id)ON DELETE CASCADE,条目x文本,常量文本
);


插入到table2值(1,aaaa,aaaaa);
插入到table2值(1,baaaa,baaaaaaa);
插入到table2值(1,caaaa,caaaaaaa)
插入到table2值(2,bbbb,bbbbb);

入场后的表格

 源码> select * from table1; 
id entry1 entry2
---------- ---------- ----------
1 aaaa aaaaa
2 bbbb bbbbb
sqlite>从table2中选择*
id entryx常量
---------- ---------- ----------
1 aaaa aaaaa
1 baaaa baaaaaaa
1 caaaa caaaaaaa
2 bbbb bbbbb

删除

  sqlite>从table1中删除id = 1; 

删除后的表格

 源码>从table2中选择* 
id entryx常量
---------- ---------- ----------
2 bbbb bbbbb


sqlite> select * from table1;
id entry1 entry2
---------- ---------- ----------
2 bbbb bbbbb


I have two tables in sqlite, which are "connected" with an id. The app using this tables is running on Android OS.

Table 1;:

|id| entry 1| entry2|
|1 | aaaaaa | aaaaa |
|2 | bbbbbb | bbbbb |

Table 2:

|id| entryx| constant|
|1 | aaaaa | aaaaaaaa|
|1 | baaaa | baaaaaaa|
|1 | caaaa | caaaaaaa|
|2 | ababa | baabaaba|

At the moment I delete entries with the following query, by using a loop:

do{
    db.delete("Table 1","id='"+cid+"'",null);
    db.delete("Table 2","id='"+cid+"'",null);
  }
while(getNextID());

I want to use a foreign key, which allows me to delete the entry in table 1 and all entries in table 2 are deleted as well. Besides, I have to consider that the data in table 1 are inserted, before they are inserted in table 2. How can I use a foreign key to do this? The table is using id as int and it is the primary key, the same is used for the second table.

As selvin suggested use on delete cascade

http://www.sqlite.org/foreignkeys.html

Table1

CREATE TABLE table1 (
  id PRIMARY KEY  
 ,entry1 text,entry2 text
);

Then

insert into table1 values(1,"aaaa","aaaaa");
insert into table1 values(2,"bbbb","bbbbb");

Table2

CREATE TABLE table2(
  id int references table1(id) ON DELETE CASCADE, entryx text, constant text
);


insert into table2 values(1,"aaaa","aaaaa");
insert into table2 values(1," baaaa ","baaaaaaa");
insert into table2 values(1,"  caaaa ","caaaaaaa")
insert into table2 values(2,"bbbb","bbbbb"); 

Tables after entry

sqlite> select * from table1;
id          entry1      entry2    
----------  ----------  ----------
1           aaaa        aaaaa     
2           bbbb        bbbbb     
sqlite> select * from table2;
id          entryx      constant  
----------  ----------  ----------
1           aaaa        aaaaa      
1           baaaa       baaaaaaa  
1           caaaa       caaaaaaa  
2           bbbb        bbbbb  

Delete

sqlite> delete from table1 where id=1;

Tables after delete

sqlite> select * from table2;
id          entryx      constant  
----------  ----------  ----------
2           bbbb        bbbbb     


sqlite> select * from table1;
id          entry1      entry2    
----------  ----------  ----------
2           bbbb        bbbbb