1
我試圖級聯刪除,以便當我從myFooTable
刪除記錄時,myFooBarLinkTable
和myBarTable
中的所有關聯記錄也被刪除。下面是我當前數據庫模式嘗試的簡化版本。當我從myFooTable
刪除記錄時,myFooBarTable
中的關聯記錄被刪除;但myBarTable
中的記錄仍然存在。在android/sqlite中跨多個表的級聯刪除
public class MyDatabaseManager extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE myFooTable (fooID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"fooText1 TEXT," +
"fooText2 TEXT " +
");");
db.execSQL("CREATE TABLE myBarTable (barID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"barText1 TEXT, " +
"barText2 TEXT);");
db.execSQL("CREATE TABLE myFooBarLinkTable (fooBarLinkID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"fooLinkID REFERENCES myFooTable(fooID) ON DELETE CASCADE, " +
"barLinkID REFERENCES myBarTable(barID) ON DELETE CASCADE);");
是否有理由更喜歡使用觸發器或手動刪除? –
否;無論什麼最適合你。 (您必須以任何方式編寫'DELETE'命令。) –