我可以执行基于案例的外键命令(级联或设置为空)吗?

Can I do a case-based foreign key command (either cascade or set null)?

我想在用户删除父项 table 中的项目时为用户提供选项,以级联删除或将 null 设置为子项 table 中引用外键,在 SQLITE 中可能吗?或者我必须在我的代码中这样做吗? 这些是我目前在创建两个 table 时使用的命令,目前在删除类别时,分配给该类别的项目被设置为空,基本上我想让用户选择删除所有他们,最好在 SQL 代码内。

    await database.execute(
      "CREATE TABLE $TABLE_CATEGORIES ("
      "$COLUMN_CATEGORYID INTEGER PRIMARY KEY,"
      "$COLUMN_CATEGORYNAME TEXT"
      ")",
    );
    await database.execute(
      "CREATE TABLE $TABLE_GROCERIES ("
      "$COLUMN_ID INTEGER PRIMARY KEY,"
      "$COLUMN_NAME TEXT,"
      "$COLUMN_PPU REAL,"
      "$COLUMN_BASE REAL,"
      "$COLUMN_STOCK REAL,"
      "$COLUMN_CATEGORYID INTEGER,"
      "FOREIGN KEY($COLUMN_CATEGORYID) REFERENCES $TABLE_CATEGORIES($COLUMN_CATEGORYID) ON DELETE SET NULL"
      ")",
    );

您需要在您的代码中解决这个问题。您只能将数据库配置为“SET NULL”或“CASCADE DELETE”。如果这是我的任务,我会使用“ON DELETE SET NULL”并允许用户通过触发第二个 sql 查询来删除子行,例如 DELETE FROM child_table WHERE parent_id = $parentid