Qt SQlite實現級聯刪除

2021-08-13 04:03:35 字數 1275 閱讀 2401

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include "qsqldatabase"

#include "qdebug.h"

#include "qsqlquery"

mainwindow::mainwindow(qwidget *parent) :

qmainwindow(parent),

ui(new ui::mainwindow)

qsqlquery query(db);

if(!query.exec("pragma foreign_keys = on"))//使外來鍵功能生效

// -- 建立測試主表. id 是主鍵.

query.exec("create table test_main ("

"id int not null,"

"value varchar(10),"

"primary key(id)"

")");

//-- 插入測試主表資料.

query.exec("insert into test_main(id, value) values (1, 'one')");

query.exec("insert into test_main(id, value) values (2, 'two')");

//-- 建立測試子表. ( 注意, 這裡要有乙個 on delete cascade )

query.exec("create table test_sub ("

"id int primary key,"

"main_id int references test_main(id) on delete cascade,"

"value varchar(10)"

")");

//-- 插入測試子表資料.

query.exec("insert into test_sub(id, main_id, value) values (1, 1, 'oneone')");

query.exec("insert into test_sub(id, main_id, value) values (2, 2, 'twotwo')");

//-- 測試刪除主表.

query.exec("delete from test_main where id = 1");

}mainwindow::~mainwindow()

Mybatis 級聯刪除的實現

需求描述 今日需求是刪除資源時同時刪除與該資源繫結的角色資料,有兩張表,資源表 角色與資源繫結表,級聯刪除的時候有兩種方法 建立表時直接建立約束,當父表刪除資料時資料庫會自動去刪除子表中的資料,通過 實現級聯刪除,先刪除子表資料,然後刪除父表中的資料。通過資料庫實現 可以參考博文 這種方式假如我們要...

oracle 級聯刪除

1 查詢外來鍵及父表 select a.constraint name 外鍵名,a.table name 子表,b.table name 父表 from user constraints a,user constraints b where a.constraint type r and b.con...

EFCodeFirst級聯刪除

預設情況下codefirst會在外鍵約束中設定 刪除規則 為級聯 不會預設設定 更新規則 為級聯 當僅定義了導航屬性如 public virtual manager manager 而沒有顯示定義外來鍵如 public int managerid codefirst不會設定 刪除規則 為級聯 在顯示...