mysql用一个数据库不同的表,如何使其中一个列和两个表产生关联?比如表 a 和 表 b,都有一个  id列如何能够方便的是a,b两个表通过id列进行关联比如在a表中插入一行,相应b也多了一行查找啊,删除,修改也可以实现?

解决方案 »

  1.   

    当然是用触发器了,不过这里是mssqlserver板块,mysql不知道怎么创建呵呵
      

  2.   

    可参考:e.g.
    -- 1. create tables :Drop table if exists t_a;
    create table t_a 
    (
     id mediumint(9) not null default '0',
    data varchar(50) default null,
    primary key (id)
    ) ;Drop table if exists t_b;
    create table t_b 
    (
     id mediumint(9) not null default '0',
    data varchar(50) default null,
    primary key (id)
    );-- 2 . create trigger:Drop trigger if exists tg_T_a_Insert;
    delimiter //
    create trigger tg_T_a_Insert
    after insert  on t_a
    for each row
    begin
    insert into t_b (id,data) values (new.id,new.data);
    end;
    //
    delimiter ;-- 3.  Insert:
    insert into t_a (id,data) values (1,'data1'),(2,'data2');-- 4.  Query:
    select * from t_a;
    select * from t_b;写进sql.sql文件
    mysql> source d:/sql.sql
    Query OK, 0 rows affected (0.06 sec)Query OK, 0 rows affected (0.28 sec)Query OK, 0 rows affected (0.03 sec)Query OK, 0 rows affected (0.19 sec)Query OK, 0 rows affected, 1 warning (0.00 sec)Query OK, 0 rows affected (0.03 sec)Query OK, 2 rows affected (0.06 sec)
    Records: 2  Duplicates: 0  Warnings: 0+----+-------+
    | id | data  |
    +----+-------+
    |  1 | data1 |
    |  2 | data2 |
    +----+-------+
    2 rows in set (0.00 sec)+----+-------+
    | id | data  |
    +----+-------+
    |  1 | data1 |
    |  2 | data2 |
    +----+-------+
    2 rows in set (0.00 sec)