两张表,一张是用户帐号表,一张是用户信息表,两张表是一对一关系,我想让他们做级联删除
也就是我删除用户帐号表的一条信息,会自动删除信息表的对应一条信息
同样在信息表删除一条信息也自动在帐号表删除对应的一条信息这语句要怎么写啊

解决方案 »

  1.   

    做一个存储过程,
    CREATE PROCEDURE A()
    BEGIN
         declare exit handler for sqlexception rollback;    
    BEGIN TRANSACTION;
         delete from A where A.userid=100;
         delete from B where B.usetid=100;
    COMMIT; 
    end;
      

  2.   

    例子如下。 (当前目前楼主没有要求同步增加,权要求同步删除)
    create table tx1 (id int primary key,col int) engine=innodb;
    create table tx2 (id int primary key,fid int,foreign key (fid) references tx1(id) on delete cascade) engine=innodb;insert into tx1 values (1,1),(2,2);
    insert into tx2 values (11,1),(12,2);
    mysql> create table tx1 (id int primary key,col int) engine=innodb;
    Query OK, 0 rows affected (0.06 sec)mysql>  create table tx2 (id int primary key,fid int,foreign key (fid) reference
    s tx1(id) on delete cascade) engine=innodb;
    Query OK, 0 rows affected (0.14 sec)mysql> select * from tx1;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |    1 |
    |  2 |    2 |
    +----+------+
    2 rows in set (0.00 sec)mysql> select * from tx2;
    +----+------+
    | id | fid  |
    +----+------+
    | 11 |    1 |
    | 12 |    2 |
    +----+------+
    2 rows in set (0.00 sec)mysql> delete from tx1 where id=2;
    Query OK, 1 row affected (0.03 sec)mysql> select * from tx1;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |    1 |
    +----+------+
    1 row in set (0.00 sec)mysql> select * from tx2;
    +----+------+
    | id | fid  |
    +----+------+
    | 11 |    1 |
    +----+------+
    1 row in set (0.00 sec)mysql>