Summary of common methods for multi-table association deletion

Most developers may not be too unfamiliar with multi-table joint query, but for beginners, they often ignore primary and foreign key constraints when building tables, or have no experience in database programming, and will not use triggers and stored procedures. When it is necessary to delete multiple tables, it is nothing more than a multi-table joint query in the background, and the background determines whether there is an association.

Now we summarize several frequently used methods of multi-table joint deletion as follows, with specific examples:

1. Cascading delete (on delete set null, on delete cascade) is a type of foreign key association
Case 1 (network resource)
 create table a
(
id varchar(20) primary key,
password varchar(20) not null
)

create table b
(
id int identity(1,1) primary key,
name varchar(50) not null,
userId varchar(20),
foreign key (userId) references a(id) on delete cascade
)
Table B creates a foreign code userId corresponding to A's primary key ID, and declares cascade delete
Test data:
insert a values ​​('11',' aaa')
insert a values('23','aaa')
insert b values('da','11')
insert b values('das','11')
insert b values('ww','23' )
Delete the data with id '11' in table A, and find that the userId in table B is "11" is also automatically deleted by the database

delete a where id='11'

 

Case 2 (Network Resources)

 

create table dept_test
(deptno number(10) not null,
 deptname varchar2(30) not null,
 constraint pk_dept_test primary key(deptno));

create table emp_test
(empno number(10) not null,
 fname varchar2(20) ,
 lname varchar2(20) ,
 dept number(10) ,
 constraint pk_emp_test primary key(empno));

 

alter table emp_test
add constraint fk_emp_dept_test foreign key(dept) references dept_test(deptno) on delete set null;

 

insert into dept_test values(1,'Sales Department');
insert into dept_test values(2,'Finance Department');
insert into emp_test values ​​(2,'Mary',' Song',1);
insert into emp_test values (3,'Linda','Liu',2);
insert into emp_test values (4,'Linlin','Zhang',1);

 

delete from dept_test where deptno = 1;
1 row deleted.

 

SQL> select * from emp_test;
     EMPNO FNAME          LNAME                  DEPT
---------- -------------------- -------------------- ----------
     2 Mary           Song
     3 Linda          Liu                     2
     4 Linlin          Zhang

SQL> select * from dept_test;
    DEPTNO DEPTNAME
---------- ------------------------------
     2 财务部
It can be seen that the dept value corresponding to the two records with deptno=1 in dept_test referenced in emp_test has been set to empty.


 

SQL> truncate table emp_test;
Table truncated.

SQL> truncate table dept_test;
truncate table dept_test
               *
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
At this time, it is also forbidden to empty the dept_test table, and the drop is the same.


If you want to delete the parent table, there are two ways:

1. Drop the child table first, and then drop the parent table.

2. First delete the constraint alter table emp_test drop constraint fk_emp_dept_test;, and then drop the parent table.

2.采用存储过程(数据库开发)
 
A表:
AID   Aname                                 主健:AID
B表:
BID   BelongAID Bname               主健:BID,外健:BelongAID 
C表:
CID BelongBID Cname                  主健:CID,外健:BelongBID
D表:
DID BelongCID Dname                  主健:DID,外健:BelongCID
其中:
A表和B表通过A.AID和B.BelongAID  创建了外健关系
B表和C表通过B.BID和C.BelongBID  创建了外健关系
C表和D表通过C.CID和D.BelongCID  创建了外健关系


3.采用触发器(个人喜欢用的,当然要看实际需要)

删除Text 表中的一条记录的同时删除对应记录表 relog中的记录

create trigger Text_delete on Text for delete

as

begin

delete from relog where tid = (select rid from relog)

end


 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326307386&siteId=291194637