SQLite
 sql >> Teknologi Basis Data >  >> RDS >> SQLite

Bagaimana cara menggunakan kunci asing di sqlite?

Seperti yang disarankan selvin, gunakan pada kaskade penghapusan

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

Tabel1

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

Kemudian

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

Tabel2

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"); 

Tabel setelah entri

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  

Hapus

sqlite> delete from table1 where id=1;

Tabel setelah dihapus

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


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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. tidak dapat menyalin database menggunakan kelas SQLiteAssetHelper

  2. java, Menggunakan variabel Statis dengan benar untuk mencegah kebuntuan - Sinkronisasi

  3. Android - Kursor diMapReady (Penanda)

  4. Cara menutup kursor dengan benar di android

  5. Pulihkan Database SQLite